This commit is contained in:
2022-12-30 16:14:41 +08:00
parent c5d893ba35
commit 7ab98231e8
14 changed files with 454 additions and 187 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -102,11 +102,37 @@ const _sfc_main = defineComponent({
});
const isPassword = computed(() => type.value == "password");
const composing = ref(false);
const formatMoney = function(s) {
var noNegative = true;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")) + "";
if (parseFloat(s) < 0) {
s = Math.abs(s) + "";
noNegative = false;
}
var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
var t = "";
for (let i = 0; i < l.length; i++) {
if (i % 3 == 2 && i != l.length - 1) {
t += l[i] + ",";
} else {
t += l[i] + "";
}
}
let z = (noNegative ? "" : "-") + t.split("").reverse().join("");
if (r) {
return z + "." + r;
} else {
return isNaN(parseInt(z)) ? 0 : z;
}
};
watch(() => props.type, () => {
type.value = props.type;
});
const input = ref();
watch(() => props.modelValue, () => {
if (props.qfw && currentValue.value.indexOf(",") != -1) {
console.log(input);
if (!(input.value == document.activeElement) && props.qfw) {
currentValue.value = formatMoney(props.modelValue.toString());
return;
}
currentValue.value = String(props.modelValue == null ? "" : props.modelValue);
@@ -142,6 +168,15 @@ const _sfc_main = defineComponent({
if (props.type === "number") {
onNumberBlur(event);
}
if (props.qfw) {
try {
let reg = /\d{1,3}(?=(\d{3})+$)/g;
console.log("\u6DFB\u52A0\uFF0C", formatMoney(props.modelValue.toString()));
currentValue.value = formatMoney(props.modelValue.toString());
} catch {
currentValue.value = "\u8F93\u5165\u9519\u8BEF";
}
}
emit("blur", event);
};
const onNumberBlur = (event) => {
@@ -157,7 +192,6 @@ const _sfc_main = defineComponent({
if (props.qfw) {
value = value.replace(/,/g, "");
}
currentValue.value = new String(props.modelValue).toLocaleString();
emit("update:modelValue", value);
};
const onCompositionstart = () => {
@@ -213,7 +247,9 @@ const _sfc_main = defineComponent({
onFocus,
onBlur,
onCompositionstart,
onCompositionend
onCompositionend,
ref_key: "input",
ref: input
}, null, 40, _hoisted_5),
__props.password && unref(hasContent) ? (openBlock(), createElementBlock("span", {
key: 1,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -29,7 +29,7 @@ export interface InputProps {
maxlength?: number;
max?: number;
min?: number;
qfw?:boolean;
qfw?: boolean;
}
const props = withDefaults(defineProps<InputProps>(), {
@@ -40,7 +40,7 @@ const props = withDefaults(defineProps<InputProps>(), {
password: false,
modelValue: "",
size: "md",
qfw: false
qfw: false,
});
interface InputEmits {
@@ -63,18 +63,48 @@ const hasContent = computed(() => (props.modelValue as string)?.length > 0);
const isPassword = computed(() => type.value == "password");
const composing = ref(false);
const formatMoney = function (s: string) {
var noNegative = true;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")) + "";
if (parseFloat(s) < 0) {
s = Math.abs(s) + "";
noNegative = false;
}
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
var t = "";
for (let i = 0; i < l.length; i++) {
if (i % 3 == 2 && i != l.length - 1) {
t += l[i] + ",";
} else {
t += l[i] + ""; //加上空格
}
}
let z: any = (noNegative ? "" : "-") + t.split("").reverse().join("");
// z = isNaN(z) ? 0 : z;
if (r) {
return z + "." + r;
} else {
return isNaN(parseInt(z)) ? 0 : z;
}
};
watch(
() => props.type,
() => {
type.value = props.type;
}
);
const input: any = ref<Element>();
watch(
() => props.modelValue,
() => {
if(props.qfw && currentValue.value.indexOf(",") != -1){
return
console.log(input);
if (!(input.value == document.activeElement) && props.qfw) {
currentValue.value = formatMoney(props.modelValue.toString());
return;
}
currentValue.value = String(
props.modelValue == null ? "" : props.modelValue
@@ -87,8 +117,8 @@ const onInput = function (event: Event) {
let value = inputElement.value;
emit("input", value);
if (composing.value) return;
if(props.qfw){
value = value.replace(/,/g,"")
if (props.qfw) {
value = value.replace(/,/g, "");
}
emit("update:modelValue", value);
};
@@ -99,15 +129,15 @@ const onClear = () => {
};
const onFocus = (event: Event) => {
currentValue.value = new String(props.modelValue).replace(/,/g,"")
currentValue.value = new String(props.modelValue).replace(/,/g, "");
emit("focus", event);
};
const onChange = (event: Event) => {
const inputElement = event.target as HTMLInputElement;
let value = inputElement.value;
if(props.qfw){
value = value.replace(/,/g,"")
if (props.qfw) {
value = value.replace(/,/g, "");
}
emit("change", value);
};
@@ -116,6 +146,17 @@ const onBlur = (event: Event) => {
if (props.type === "number") {
onNumberBlur(event);
}
if (props.qfw) {
try {
let reg = /\d{1,3}(?=(\d{3})+$)/g;
// new String(props.modelValue).replace(reg, '$&,');
console.log("添加,", formatMoney(props.modelValue.toString()));
currentValue.value = formatMoney(props.modelValue.toString());
} catch {
currentValue.value = "输入错误";
}
}
emit("blur", event);
};
@@ -127,10 +168,9 @@ const onNumberBlur = (event: Event) => {
if (props.max && props.max < Number(value)) value = props.max.toString();
if (props.min && props.min > Number(value)) value = props.min.toString();
}
if(props.qfw){
value = value.replace(/,/g,"")
if (props.qfw) {
value = value.replace(/,/g, "");
}
currentValue.value = new String(props.modelValue).toLocaleString()
emit("update:modelValue", value);
};
@@ -191,6 +231,7 @@ const showPassword = () => {
@blur="onBlur"
@compositionstart="onCompositionstart"
@compositionend="onCompositionend"
ref="input"
/>
<span
class="layui-input-password"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long