UNPKG

@pit-front-end/components

Version:

湖南创智艾泰克科技有限公司

130 lines (129 loc) 4.58 kB
import { defineComponent as g, reactive as F, onMounted as b, resolveComponent as x, createBlock as I, openBlock as V, mergeProps as C, unref as w, isRef as M, createSlots as $, withCtx as N, createTextVNode as L, toDisplayString as y } from "vue"; import { inputFormatterEmits as P, inputFormatterProps as _ } from "./input-formatter.js"; import { useInputFormatter as v } from "./use-input-formatter.js"; const j = /* @__PURE__ */ g({ name: "PitInputFormatter", inheritAttrs: !1, __name: "input-formatter", props: _, emits: P, setup(h, { emit: m }) { const r = h, n = F({ inlineFormatter: () => { }, describeInline: { int: !0, fixed: 0, max: Math.pow(10, 10), min: -Math.pow(10, 10), maxLen: 10, minLen: 0, negative: !0 } }), { modelValue: u } = v(r); b(() => { a.initFormatter(), Object.assign(n.describeInline, r.describe); }); const a = { upFirstStr(e) { return e ? e[0].toUpperCase() + e.slice(1) : ""; }, // 初始化formatter函数,formatter、formatterPreset必须要有一个,否则默认不做处理 initFormatter() { if (!r.formatterPreset) return; if (r.formatter) { n.inlineFormatter = r.formatter; return; } const e = a["formatter" + a.upFirstStr(r.formatterPreset)]; if (!e) { n.inlineFormatter = () => { console.warn( `this is no formatter${a.upFirstStr(r.formatterPreset)}` ), console.warn("you maybe want this. 米: distance、温度:temperature"); }; return; } n.inlineFormatter = e; }, // 限制数量 formatterNumberLimit(e) { let { int: i, negative: p, min: l, max: d, fixed: s, maxLen: c } = n.describeInline, t = "" + e; if (t = t.replace(/^0\d+/, "").replace(/[^\d.-]/g, "").replace(/^\./g, "").replace(/\.{2,}/g, ".").replace(".", "$#$").replace(/\./g, "").replace("$#$", "."), p ? t = t.slice(0, 1) + t.slice(1).replace(/-/g, "") : t = t.replace(/-/g, ""), s) { const o = new RegExp(`^(-)*(\\d+)\\.(\\d{0,${s}}).*$`); t = t.replace(o, "$1$2.$3"); } if (!s && i && (t = t.replace(/\./gi, "")), c) { let [o, f] = [0, 0]; t.match(/\./) && (o += 1, f = s), t.match(/^-/) ? (o += 1, l = -Math.pow(10, c - f - o) + 1) : d = Math.pow(10, c - f - o) - 1; } return !isNaN(parseFloat(t)) && t > d && (r.maxMinChange && (t = d), r.maxMinChange || (t = n.oldValued)), !isNaN(parseFloat(t)) && t < l && (r.maxMinChange && (t = l), r.maxMinChange || (t = n.oldValued)), t; }, // 格式化(距离) formatterDistance(e) { return n.describeInline.fixed = 3, a.formatterNumberLimit(e); }, // 格式化温度 formatterTemperature(e) { return Object.assign(n.describeInline, { fixed: 3, max: 99, min: -99 }), a.formatterNumberLimit(e); }, // 格式换人员 formatterPeople(e) { return Object.assign(n.describeInline, { int: !0, negative: !1, max: Math.pow(10, 8), min: -Math.pow(10, 8) }), a.formatterNumberLimit(e); }, // 格式化(空), formatterNull(e) { return e; }, handleBlur(e) { e && e.currentTarget, m("blur", r.modelValue); }, handleClear() { m("clear", r.modelValue); }, handleFocus(e) { n.valued = e && e.currentTarget || "", m("focus", r.modelValue); }, handleInput(e) { n.oldValued = r.modelValue; const i = n.inlineFormatter(e, n.describeInline) || ""; m("update:modelValue", i); } }; return (e, i) => { const p = x("el-input"); return V(), I(p, C({ modelValue: w(u), "onUpdate:modelValue": i[0] || (i[0] = (l) => M(u) ? u.value = l : null), disabled: r.disabled }, e.$attrs, { placeholder: r.placeholder, readonly: r.readonly, onBlur: a.handleBlur, onClear: a.handleClear, onInput: a.handleInput, onFocus: a.handleFocus }), $({ _: 2 }, [ r.plain ? void 0 : { name: "append", fn: N(() => [ L(y(r.unit), 1) ]), key: "0" } ]), 1040, ["modelValue", "disabled", "placeholder", "readonly", "onBlur", "onClear", "onInput", "onFocus"]); }; } }); export { j as default };