@arco-vue-pro-components/pro-components
Version:
基于@arco-design/web-vue组件的高级组件,包括pro-table
160 lines (159 loc) • 4.96 kB
JavaScript
import { createVNode, mergeProps } from "vue";
import { Progress, Avatar, Image } from "@arco-design/web-vue";
import dayjs from "dayjs";
import ProSelect from "../../pro-select/index.js";
const moneyIntl = new Intl.NumberFormat("zh-Hans-CN", {
currency: "CNY",
style: "currency",
minimumFractionDigits: 2
});
const enMoneyIntl = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
const ruMoneyIntl = new Intl.NumberFormat("ru-RU", {
style: "currency",
currency: "RUB"
});
const msMoneyIntl = new Intl.NumberFormat("ms-MY", {
style: "currency",
currency: "MYR"
});
function showUnitColumn(value, unit) {
let str = "";
if (value == null) {
return str;
}
str = `${value}`;
if (!unit) {
return str;
}
str = `${value}${unit}`;
return str;
}
const defaultRenderTextByObject = (text, value) => {
if (value.type === "progress") {
return createVNode(Progress, {
"size": "small",
"percent": parseFloat(text),
"status": value.status || getProgressStatus(parseFloat(text))
}, null);
}
if (value.type === "money") {
if (value.locale === "en_US") {
return enMoneyIntl.format(text);
}
if (value.locale === "ru_RU") {
return ruMoneyIntl.format(text);
}
if (value.locale === "ms_MY") {
return msMoneyIntl.format(text);
}
return moneyIntl.format(text);
}
if (value.type === "percent") {
return text ? showUnitColumn(text, "%") : "-";
}
return text;
};
const defaultRenderText = (text, valueType, rowIndex, item, columnEmptyText, itemColumn, columnKey) => {
var _a;
if (typeof valueType === "function" && item) {
const value = valueType(item);
if (typeof value === "string") {
return defaultRenderText(text, value, rowIndex, item, columnEmptyText, itemColumn, columnKey);
}
if (typeof value === "object") {
return defaultRenderTextByObject(text, value);
}
}
if (valueType === "select" && ((_a = itemColumn == null ? void 0 : itemColumn.fieldProps) == null ? void 0 : _a.request)) {
return createVNode(ProSelect, mergeProps({
"cacheForSwr": true,
"mode": "read",
"columnKey": columnKey,
"modelValue": text
}, itemColumn == null ? void 0 : itemColumn.fieldProps), null);
}
if (valueType === "money" && (text || text === 0)) {
if (typeof text === "string") {
return moneyIntl.format(parseFloat(text));
}
return moneyIntl.format(text);
}
if (typeof text === "string" || typeof text === "number") {
if (valueType === "date" && text) {
return dayjs(text).format("YYYY-MM-DD");
}
if (valueType === "dateTime" && text) {
return dayjs(text).format("YYYY-MM-DD HH:mm:ss");
}
if (valueType === "time" && text) {
return dayjs(text).format("HH:mm:ss");
}
}
if (valueType === "dateRange" && text && Array.isArray(text) && text.length === 2) {
const [startText, endText] = text;
return createVNode("div", null, [createVNode("div", null, [startText ? dayjs(startText).format("YYYY-MM-DD") : "-"]), createVNode("div", null, [endText ? dayjs(endText).format("YYYY-MM-DD") : "-"])]);
}
if (valueType === "dateTimeRange" && text && Array.isArray(text) && text.length === 2) {
const [startText, endText] = text;
return createVNode("div", null, [createVNode("div", null, [startText ? dayjs(startText).format("YYYY-MM-DD HH:mm:ss") : "-"]), createVNode("div", null, [endText ? dayjs(endText).format("YYYY-MM-DD HH:mm:ss") : "-"])]);
}
if (valueType === "progress") {
const status = getProgressStatus(parseFloat(text));
return createVNode(Progress, {
"size": "small",
"percent": parseFloat(text),
"animation": status === "normal",
"status": status
}, null);
}
if (valueType === "percent") {
return text ? showUnitColumn(text, "%") : columnEmptyText;
}
if (valueType === "avatar" && typeof text === "string") {
return createVNode(Avatar, {
"imageUrl": text,
"size": 22,
"shape": "circle"
}, null);
}
if (valueType === "image") {
return createVNode(Image, {
"src": text,
"width": 32
}, null);
}
if (valueType === "code" && text) {
return createVNode("pre", {
"style": {
padding: 16,
overflow: "auto",
fontSize: "85%",
lineHeight: 1.45,
backgroundColor: "#f6f8fa",
borderRadius: 3
}
}, [createVNode("code", null, [text])]);
}
if (columnEmptyText) {
if (typeof text !== "boolean" && typeof text !== "number" && !text) {
return typeof columnEmptyText === "string" ? columnEmptyText : "-";
}
}
return text;
};
function getProgressStatus(text) {
if (typeof text !== "number") {
return "danger";
}
if (text === 100) {
return "success";
}
if (text < 0) {
return "danger";
}
return "normal";
}
export { defaultRenderText as default, getProgressStatus, showUnitColumn };