@pit-front-end/components
Version:
湖南创智艾泰克科技有限公司
106 lines (105 loc) • 4 kB
JavaScript
import { defineComponent as V, ref as N, reactive as w, watch as C, nextTick as f, resolveComponent as x, createBlock as y, openBlock as v, withKeys as I, createSlots as k, withCtx as h, createTextVNode as g } from "vue";
import { moneyInputEmits as T, moneyInputProps as $ } from "./money-input.js";
import { useMoneyInput as B } from "./use-money-input.js";
const E = /* @__PURE__ */ V({
name: "PitMoneyInput",
inheritAttrs: !1,
__name: "money-input",
props: $,
emits: T,
setup(b, { emit: r }) {
const o = b, m = N(), { modelValue: d } = B(o), a = w({
moneyValue: d.value
});
C(
() => d.value,
(e) => {
a.moneyValue = e, s.formatNumber(a.moneyValue);
}
);
const s = {
// 处理精度:使用Math.pow跟Math.floor再结合toFixed
decimalFun2(e, t, l) {
if (!e) return "0";
if (l == 1)
e = Number((e * 1).toFixed(t));
else {
let n = Math.pow(10, t);
e = Math.floor(e * n) / n;
}
return (e * 1).toFixed(t);
},
amountThousandsConversion(e) {
if (e === "" || e === null || e === ".00" || e === void 0)
return "";
if (e === "-")
return "-";
let t = "";
Number(e) < 0 && (e = (e + "").substring(1), t = "-"), e = e + "";
let l = e.split(".")[0].split("").reverse(), n = "", p = "";
e.indexOf(".") > -1 && (e.split(".")[1] !== null && e.split(".")[1] !== void 0 ? (e.split(".")[1].length > o.bit && (e = s.decimalFun2(Number(e), o.bit, 2)), o.bit > 0 ? n = "." + e.split(".")[1] : n = "") : n = "");
for (let i = 0; i < l.length; i++)
p += l[i] + ((i + 1) % 3 === 0 && i + 1 !== l.length ? "," : "");
let u = t + "" + p.split("").reverse().join("") + n;
return o.showComma || (u = u.replace(/,/g, "")), u;
},
formatNumber(e) {
const t = m.value.$el.getElementsByTagName("input")[0];
r("change", e), e = e + "";
const l = t.selectionStart, n = (e.slice(0, l).match(/,/g) || []).length, p = e.replace(/,/g, ""), u = s.amountThousandsConversion(
// eslint-disable-next-line no-useless-escape
p.replace(/[^\d\.-]/g, "")
), i = (u.slice(0, l).match(/,/g) || []).length, c = l + i - n;
f(() => {
a.moneyValue = u, f(() => {
t.selectionStart = c, t.selectionEnd = c;
});
});
},
moneyChange() {
const e = a.moneyValue ? (a.moneyValue + "").replace(/,/g, "").replace(new RegExp(/(\d+)(\.)(\d*)(\2*)(\d*)/g), "$1$2$3$5") : "";
let t = Object.is(Number(e), NaN) || e === "" ? "" : e;
o.min == 0 && !isNaN(Number(t)) && Number(t) < o.min ? (r("update:modelValue", 0), r("blur", 0)) : (r("update:modelValue", t), r("blur", t));
},
clear() {
r("update:modelValue", "");
}
};
return (e, t) => {
const l = x("el-input");
return v(), y(l, {
ref_key: "moneyRef",
ref: m,
modelValue: a.moneyValue,
"onUpdate:modelValue": t[0] || (t[0] = (n) => a.moneyValue = n),
modelModifiers: { trim: !0 },
placeholder: o.placeholder,
maxlength: o.maxlength,
"show-word-limit": o.showWordLimit,
disabled: o.disabled,
onInput: t[1] || (t[1] = (n) => s.formatNumber(a.moneyValue)),
onKeyup: I(s.moneyChange, ["enter"]),
onClear: s.clear,
onBlur: s.moneyChange
}, k({ _: 2 }, [
e.template === "append" ? {
name: "append",
fn: h(() => [
g(" 元 ")
]),
key: "0"
} : void 0,
e.template === "tenThousandYuan" ? {
name: "append",
fn: h(() => [
g("万元")
]),
key: "1"
} : void 0
]), 1032, ["modelValue", "placeholder", "maxlength", "show-word-limit", "disabled", "onKeyup", "onClear", "onBlur"]);
};
}
});
export {
E as default
};