This commit is contained in:
Theluyuan 2022-12-30 16:14:33 +08:00
parent 61a685dfe2
commit 0511c97302

View File

@ -40,7 +40,7 @@ const props = withDefaults(defineProps<InputProps>(), {
password: false, password: false,
modelValue: "", modelValue: "",
size: "md", size: "md",
qfw: false qfw: false,
}); });
interface InputEmits { interface InputEmits {
@ -81,15 +81,14 @@ const formatMoney = function (s: string) {
t += l[i] + ""; // t += l[i] + ""; //
} }
} }
let z:any = (noNegative ? "" : "-") + t.split("").reverse().join(""); let z: any = (noNegative ? "" : "-") + t.split("").reverse().join("");
// z = isNaN(z) ? 0 : z; // z = isNaN(z) ? 0 : z;
if(r){ if (r) {
return z+ "." + r; return z + "." + r;
} else {
}else{
return isNaN(parseInt(z)) ? 0 : z; return isNaN(parseInt(z)) ? 0 : z;
} }
} };
watch( watch(
() => props.type, () => props.type,
@ -97,15 +96,15 @@ watch(
type.value = props.type; type.value = props.type;
} }
); );
const input:any = ref<Element>() const input: any = ref<Element>();
watch( watch(
() => props.modelValue, () => props.modelValue,
() => { () => {
console.log(input) console.log(input);
if(!(input.value == document.activeElement) && props.qfw){ if (!(input.value == document.activeElement) && props.qfw) {
currentValue.value = formatMoney(props.modelValue.toString()); currentValue.value = formatMoney(props.modelValue.toString());
return return;
} }
currentValue.value = String( currentValue.value = String(
props.modelValue == null ? "" : props.modelValue props.modelValue == null ? "" : props.modelValue
@ -119,7 +118,7 @@ const onInput = function (event: Event) {
emit("input", value); emit("input", value);
if (composing.value) return; if (composing.value) return;
if (props.qfw) { if (props.qfw) {
value = value.replace(/,/g, "") value = value.replace(/,/g, "");
} }
emit("update:modelValue", value); emit("update:modelValue", value);
}; };
@ -130,7 +129,7 @@ const onClear = () => {
}; };
const onFocus = (event: Event) => { const onFocus = (event: Event) => {
currentValue.value = new String(props.modelValue).replace(/,/g, "") currentValue.value = new String(props.modelValue).replace(/,/g, "");
emit("focus", event); emit("focus", event);
}; };
@ -138,7 +137,7 @@ const onChange = (event: Event) => {
const inputElement = event.target as HTMLInputElement; const inputElement = event.target as HTMLInputElement;
let value = inputElement.value; let value = inputElement.value;
if (props.qfw) { if (props.qfw) {
value = value.replace(/,/g, "") value = value.replace(/,/g, "");
} }
emit("change", value); emit("change", value);
}; };
@ -148,18 +147,16 @@ const onBlur = (event: Event) => {
onNumberBlur(event); onNumberBlur(event);
} }
if (props.qfw) { if (props.qfw) {
try { try {
let reg = /\d{1,3}(?=(\d{3})+$)/g; let reg = /\d{1,3}(?=(\d{3})+$)/g;
// new String(props.modelValue).replace(reg, '$&,'); // new String(props.modelValue).replace(reg, '$&,');
console.log("添加,", formatMoney(props.modelValue.toString())) console.log("添加,", formatMoney(props.modelValue.toString()));
currentValue.value = formatMoney(props.modelValue.toString()); currentValue.value = formatMoney(props.modelValue.toString());
} catch { } catch {
currentValue.value = "输入错误" currentValue.value = "输入错误";
} }
} }
emit("blur", event); emit("blur", event);
}; };
@ -172,7 +169,7 @@ const onNumberBlur = (event: Event) => {
if (props.min && props.min > Number(value)) value = props.min.toString(); if (props.min && props.min > Number(value)) value = props.min.toString();
} }
if (props.qfw) { if (props.qfw) {
value = value.replace(/,/g, "") value = value.replace(/,/g, "");
} }
emit("update:modelValue", value); emit("update:modelValue", value);
}; };
@ -210,22 +207,53 @@ const showPassword = () => {
<div class="layui-input-wrapper"> <div class="layui-input-wrapper">
<span class="layui-input-prefix" v-if="slots.prefix || props.prefixIcon"> <span class="layui-input-prefix" v-if="slots.prefix || props.prefixIcon">
<slot name="prefix" v-if="slots.prefix"></slot> <slot name="prefix" v-if="slots.prefix"></slot>
<lay-icon v-else :type="props.prefixIcon" class="layui-input-prefix-icon"></lay-icon> <lay-icon
v-else
:type="props.prefixIcon"
class="layui-input-prefix-icon"
></lay-icon>
</span> </span>
<input :type="type" :name="name" :disabled="disabled" :placeholder="placeholder" :autofocus="autofocus" <input
:autocomplete="autocomplete" :maxlength="maxlength" :max="max" :min="min" :readonly="readonly" :type="type"
:value="currentValue" @input="onInput" @change="onChange" @focus="onFocus" @blur="onBlur" :name="name"
@compositionstart="onCompositionstart" @compositionend="onCompositionend" ref="input" /> :disabled="disabled"
<span class="layui-input-password" @click="showPassword" v-if="password && hasContent"> :placeholder="placeholder"
:autofocus="autofocus"
:autocomplete="autocomplete"
:maxlength="maxlength"
:max="max"
:min="min"
:readonly="readonly"
:value="currentValue"
@input="onInput"
@change="onChange"
@focus="onFocus"
@blur="onBlur"
@compositionstart="onCompositionstart"
@compositionend="onCompositionend"
ref="input"
/>
<span
class="layui-input-password"
@click="showPassword"
v-if="password && hasContent"
>
<password-icon v-if="isPassword"></password-icon> <password-icon v-if="isPassword"></password-icon>
<un-password-icon v-else></un-password-icon> <un-password-icon v-else></un-password-icon>
</span> </span>
<span class="layui-input-clear" v-if="allowClear && hasContent && !disabled"> <span
class="layui-input-clear"
v-if="allowClear && hasContent && !disabled"
>
<lay-icon type="layui-icon-close-fill" @click.stop="onClear"></lay-icon> <lay-icon type="layui-icon-close-fill" @click.stop="onClear"></lay-icon>
</span> </span>
<span class="layui-input-suffix" v-if="slots.suffix || props.suffixIcon"> <span class="layui-input-suffix" v-if="slots.suffix || props.suffixIcon">
<slot name="suffix" v-if="slots.suffix"></slot> <slot name="suffix" v-if="slots.suffix"></slot>
<lay-icon v-else :type="props.suffixIcon" class="layui-input-suffix-icon"></lay-icon> <lay-icon
v-else
:type="props.suffixIcon"
class="layui-input-suffix-icon"
></lay-icon>
</span> </span>
</div> </div>
<div class="layui-input-append" v-if="slots.append"> <div class="layui-input-append" v-if="slots.append">