@antdv/pro-field
Version:
原子信息组件,统一 ProForm、ProTable、ProList、Filter 等组件里面的字段定义。
281 lines (280 loc) • 9.92 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
import { intlMap as allIntlMap, useIntl } from "@antdv/pro-provider";
import { anyType, boolType, funcType, objectType, omit, oneOfType, stringType } from "@antdv/pro-utils";
import { InputNumber, Popover } from "ant-design-vue";
import { inputNumberProps } from "ant-design-vue/es/input-number";
import { computed, defineComponent } from "vue";
import { buildProFieldProp } from "../props.mjs";
const defaultMoneyIntl = new Intl.NumberFormat("zh-Hans-CN", {
currency: "CNY",
style: "currency"
});
const enMoneyIntl = {
style: "currency",
currency: "USD"
};
const ruMoneyIntl = {
style: "currency",
currency: "RUB"
};
const rsMoneyIntl = {
style: "currency",
currency: "RSD"
};
const msMoneyIntl = {
style: "currency",
currency: "MYR"
};
const ptMoneyIntl = {
style: "currency",
currency: "BRL"
};
const intlMap = {
"default": defaultMoneyIntl,
"zh-Hans-CN": {
currency: "CNY",
style: "currency"
},
"en-US": enMoneyIntl,
"ru-RU": ruMoneyIntl,
"ms-MY": msMoneyIntl,
"sr-RS": rsMoneyIntl,
"pt-BR": ptMoneyIntl
};
function getTextByLocale(locale, paramsText, precision, config, moneySymbol = "") {
let moneyText = paramsText == null ? void 0 : paramsText.toString().replaceAll(",", "");
if (typeof moneyText === "string") {
const parsedNum = Number(moneyText);
if (Number.isNaN(parsedNum)) return moneyText;
moneyText = parsedNum;
}
if (!moneyText && moneyText !== 0) return "";
let supportFormat = false;
try {
supportFormat = locale !== false && Intl.NumberFormat.supportedLocalesOf([locale.replace("_", "-")], {
localeMatcher: "lookup"
}).length > 0;
} catch (error) {
}
try {
const initNumberFormatter = new Intl.NumberFormat(supportFormat && locale !== false ? (locale == null ? void 0 : locale.replace("_", "-")) || "zh-Hans-CN" : "zh-Hans-CN", __spreadValues(__spreadProps(__spreadValues({}, intlMap[locale || "zh-Hans-CN"] || defaultMoneyIntl), {
maximumFractionDigits: precision
}), config));
const finalMoneyText = initNumberFormatter.format(moneyText);
const doubleSymbolFormat = (Text) => {
const match = Text.match(/\d+/);
if (match) {
const number = match[0];
return Text.slice(Text.indexOf(number));
} else {
return Text;
}
};
const pureMoneyText = doubleSymbolFormat(finalMoneyText);
const [operatorSymbol] = finalMoneyText || "";
if (["+", "-"].includes(operatorSymbol)) {
return `${moneySymbol || ""}${operatorSymbol}${pureMoneyText}`;
}
return `${moneySymbol || ""}${pureMoneyText}`;
} catch (error) {
return moneyText;
}
}
const DefaultPrecisionCont = 2;
const InputNumberPopoverProps = __spreadProps(__spreadValues({}, inputNumberProps()), {
open: Boolean,
contentRender: funcType(),
numberFormatOptions: anyType(),
numberPopoverRender: anyType()
});
const InputNumberPopover = defineComponent({
name: "InputNumberPopover",
props: InputNumberPopoverProps,
inheritAttrs: false,
emits: {
"update:value": (val) => true
},
setup(props, {
emit
}) {
return () => {
var _a;
const dom = (_a = props.contentRender) == null ? void 0 : _a.call(props, __spreadProps(__spreadValues({}, props), {
value: props.value
}));
return _createVNode(Popover, {
"placement": "topLeft",
"open": dom ? props.open : false,
"trigger": ["focus", "click"],
"content": dom,
"getPopupContainer": (triggerNode) => (triggerNode == null ? void 0 : triggerNode.parentElement) || document.body
}, {
default: () => [_createVNode(InputNumber, _mergeProps(omit(props, ["value", "onUpdate:value"]), {
"value": props.value,
"onUpdate:value": (val) => {
emit("update:value", val);
}
}), null)]
});
};
}
});
const fieldMoneyProps = buildProFieldProp({
value: oneOfType([Number, String]),
moneySymbol: boolType().def(void 0),
locale: stringType(),
/**
* 输入框内容为空的提示内容
*/
placeholder: stringType(),
/**
* 自定义 money 的 Symbol
*/
customSymbol: stringType(),
/**
* 自定义 Popover 的值,false 可以关闭他
*/
numberPopoverRender: oneOfType([Boolean, Function]).def(void 0),
/**
* NumberFormat 的配置,文档可以查看 mdn
*
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
*/
numberFormatOptions: objectType()
});
var stdin_default = defineComponent({
name: "FieldMoney",
props: fieldMoneyProps,
inheritAttrs: false,
emits: {
"update:value": (val) => true
},
setup(props, {
emit
}) {
let intl = useIntl();
const moneySymbol = computed(() => {
const customSymbol = props.customSymbol || props.fieldProps.customSymbol;
if (customSymbol) {
return customSymbol;
}
if (props.moneySymbol === false || props.fieldProps.moneySymbol === false) {
return void 0;
}
return intl.getMessage("moneySymbol", "\xA5");
});
return () => {
var _a;
const {
text,
value,
mode: type,
render,
renderFormItem,
fieldProps,
placeholder,
locale,
numberFormatOptions = fieldProps == null ? void 0 : fieldProps.numberFormatOptions,
numberPopoverRender = (fieldProps == null ? void 0 : fieldProps.numberPopoverRender) || false
} = props;
const textValue = text != null ? text : value;
const precision = (_a = fieldProps == null ? void 0 : fieldProps.precision) != null ? _a : DefaultPrecisionCont;
if (locale && allIntlMap[locale]) {
intl = allIntlMap[locale];
}
const placeholderValue = placeholder || intl.getMessage("tableForm.inputPlaceholder", "\u8BF7\u8F93\u5165");
const getFormateValue = (v) => {
const reg = new RegExp(`\\B(?=(\\d{${3 + Math.max(precision - DefaultPrecisionCont, 0)}})+(?!\\d))`, "g");
const [intStr, floatStr] = String(v).split(".");
const resultInt = intStr.replace(reg, ",");
let resultFloat = "";
if (floatStr && precision > 0) {
resultFloat = `.${floatStr.slice(0, precision === void 0 ? DefaultPrecisionCont : precision)}`;
}
return `${resultInt}${resultFloat}`;
};
if (type === "read") {
const dom = _createVNode("span", null, [getTextByLocale(locale || false, textValue, precision, numberFormatOptions != null ? numberFormatOptions : fieldProps.numberFormatOptions, moneySymbol.value)]);
if (render) {
return render(textValue, computed(() => __spreadValues({
mode: type
}, fieldProps)), dom);
}
return dom;
}
if (type === "edit" || type === "update") {
const dom = _createVNode(InputNumberPopover, _mergeProps({
"contentRender": (_props) => {
if (numberPopoverRender === false) return null;
if (!_props.value) return null;
const localeText = getTextByLocale(moneySymbol.value || locale || false, `${getFormateValue(_props.value)}`, precision, __spreadProps(__spreadValues({}, numberFormatOptions), {
notation: "compact"
}), moneySymbol.value);
if (typeof numberPopoverRender === "function") {
return numberPopoverRender == null ? void 0 : numberPopoverRender(_props, localeText);
}
return localeText;
},
"precision": precision,
"formatter": (v) => {
if (v && moneySymbol.value) {
return `${moneySymbol.value} ${getFormateValue(v)}`;
}
return v == null ? void 0 : v.toString();
},
"parser": (v) => {
if (moneySymbol.value && v) {
return v.replace(new RegExp(`\\${moneySymbol.value}\\s?|(,*)`, "g"), "");
}
return v;
},
"placeholder": placeholderValue
}, omit(fieldProps, ["numberFormatOptions", "precision", "numberPopoverRender", "customSymbol", "moneySymbol", "visible", "open"]), {
"value": value,
"onUpdate:value": (val) => {
emit("update:value", val);
},
"onBlur": fieldProps.onBlur ? (e) => {
var _a2;
let val = e.target.value;
if (moneySymbol.value && val) {
val = val.replace(new RegExp(`\\${moneySymbol.value}\\s?|(,*)`, "g"), "");
}
(_a2 = fieldProps.onBlur) == null ? void 0 : _a2.call(fieldProps, val);
} : void 0
}), null);
if (renderFormItem) {
return renderFormItem(textValue, computed(() => __spreadValues({
mode: type,
value
}, fieldProps)), dom);
}
return dom;
}
return null;
};
}
});
export {
stdin_default as default,
fieldMoneyProps
};