yanzhen-design-vue
Version:
117 lines (116 loc) • 3.79 kB
JavaScript
import { getCurrentInstance, ref, useAttrs, computed, mergeProps } from "vue";
import { unref, useProxyProps } from "@yanzhen-libs/utils-vue";
import { pick, isFunction, toNumber } from "lodash-es";
import "../../../hooks/index.mjs";
import { isEmptyValue } from "@yanzhen-libs/utils";
import { AntInputNumber, inputNumberEmits } from "./input-number.mjs";
import { AntInput } from "./input.mjs";
import { useProgress } from "../../../hooks/use-progress/index.mjs";
function useInputNumber(props, emit, context) {
const instance = getCurrentInstance();
const _ref = ref(null);
const attrs = useAttrs();
const { progressBarNum, showProgress, convertSuffix } = useProgress(props);
const nativeRef = computed(() => props.native);
const nativeTagRef = computed(() => props.nativeTag);
const nativeConfig = computed(() => {
if (unref(nativeRef)) {
const nativeProps = pick(props, ...Object.keys(AntInputNumber.props));
const cSuffix = unref(convertSuffix);
if (cSuffix) {
nativeProps.suffix = cSuffix;
}
if (props.showThousan) {
nativeProps.formatter = (value) => {
if (value) {
let val = value.toString();
const numStrList = val.split(".");
const strlist = numStrList.map((str, index) => {
if (index === 1) return str;
return str.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
val = strlist.join(".");
return val;
}
return "";
};
nativeProps.parser = (value) => {
return value.replace(/\$\s?|(,*)/g, "");
};
}
return nativeProps;
}
return pick(props, ...Object.keys(AntInput.props));
});
const nativeProxyProps = useProxyProps(nativeConfig, inputNumberEmits, {
emit
// filter: key => {
// switch (key as string) {
// case 'onUpdate:value':
// case 'onUpdate:beginTime':
// case 'onUpdate:endTime':
// return false;
// }
// return true;
// },
});
const propsRef = computed(() => {
return mergeProps(unref(attrs), unref(nativeProxyProps));
});
const textRef = computed(() => {
const txt = unref(propsRef).value;
if (isEmptyValue(txt)) return "";
const nativeTagFormatter = props.nativeTagFormatter;
if (isFunction(nativeTagFormatter)) {
return nativeTagFormatter(txt, props, instance);
}
return txt;
});
function digitUppercase(n) {
const fraction = ["角", "分"];
const digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
const unit = [
["元", "万", "亿"],
["", "拾", "佰", "仟"]
];
const head = n < 0 ? "负" : "";
n = Math.abs(n);
let s = "";
for (const [i, element] of fraction.entries()) {
s += (digit[Math.floor(n * 10 * 10 ** i) % 10] + element).replace(/零./, "");
}
s = s || "整";
n = Math.floor(n);
for (let i1 = 0; i1 < unit[0].length && n > 0; i1++) {
let p = "";
for (let j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i1] + s;
}
return head + s.replace(/(零.)*零元/, "元").replace(/(零.)+/g, "零").replace(/^整$/, "零元整");
}
const upperMoney = computed(() => {
const value = propsRef.value.value;
const numValue = toNumber(value);
if (!Number.isNaN(numValue)) {
return digitUppercase(numValue);
}
return void 0;
});
return {
_ref,
propsRef,
nativeRef,
nativeTagRef,
progressBarNum,
showProgress,
digitUppercase,
upperMoney,
textRef
};
}
export {
useInputNumber
};