UNPKG

@ant-design/pro-utils

Version:
158 lines (157 loc) 4.3 kB
import _typeof from "@babel/runtime/helpers/esm/typeof"; import { Badge, Space } from 'antd'; import React from 'react'; import { jsx as _jsx } from "react/jsx-runtime"; /** * 获取类型的 type * * @param obj */ function getType(obj) { // @ts-ignore var type = Object.prototype.toString.call(obj).match(/^\[object (.*)\]$/)[1].toLowerCase(); if (type === 'string' && _typeof(obj) === 'object') return 'object'; // Let "new String('')" return 'object' if (obj === null) return 'null'; // PhantomJS has type "DOMWindow" for null if (obj === undefined) return 'undefined'; // PhantomJS has type "DOMWindow" for undefined return type; } export var ProFieldBadgeColor = function ProFieldBadgeColor(_ref) { var color = _ref.color, children = _ref.children; return /*#__PURE__*/_jsx(Badge, { color: color, text: children }); }; export var objectToMap = function objectToMap(value) { if (getType(value) === 'map') { return value; } return new Map(Object.entries(value || {})); }; var TableStatus = { Success: function Success(_ref2) { var children = _ref2.children; return /*#__PURE__*/_jsx(Badge, { status: "success", text: children }); }, Error: function Error(_ref3) { var children = _ref3.children; return /*#__PURE__*/_jsx(Badge, { status: "error", text: children }); }, Default: function Default(_ref4) { var children = _ref4.children; return /*#__PURE__*/_jsx(Badge, { status: "default", text: children }); }, Processing: function Processing(_ref5) { var children = _ref5.children; return /*#__PURE__*/_jsx(Badge, { status: "processing", text: children }); }, Warning: function Warning(_ref6) { var children = _ref6.children; return /*#__PURE__*/_jsx(Badge, { status: "warning", text: children }); }, success: function success(_ref7) { var children = _ref7.children; return /*#__PURE__*/_jsx(Badge, { status: "success", text: children }); }, error: function error(_ref8) { var children = _ref8.children; return /*#__PURE__*/_jsx(Badge, { status: "error", text: children }); }, default: function _default(_ref9) { var children = _ref9.children; return /*#__PURE__*/_jsx(Badge, { status: "default", text: children }); }, processing: function processing(_ref10) { var children = _ref10.children; return /*#__PURE__*/_jsx(Badge, { status: "processing", text: children }); }, warning: function warning(_ref11) { var children = _ref11.children; return /*#__PURE__*/_jsx(Badge, { status: "warning", text: children }); } }; /** * 转化 text 和 valueEnum 通过 type 来添加 Status * * @param text * @param valueEnum * @param pure 纯净模式,不增加 status */ export var proFieldParsingText = function proFieldParsingText(text, valueEnumParams, key) { if (Array.isArray(text)) { return /*#__PURE__*/_jsx(Space, { split: ",", size: 2, wrap: true, children: text.map(function (value, index) { return ( // @ts-ignore proFieldParsingText(value, valueEnumParams, index) ); }) }, key); } var valueEnum = objectToMap(valueEnumParams); if (!valueEnum.has(text) && !valueEnum.has("".concat(text))) { // @ts-ignore return (text === null || text === void 0 ? void 0 : text.label) || text; } var domText = valueEnum.get(text) || valueEnum.get("".concat(text)); if (!domText) { // @ts-ignore return /*#__PURE__*/_jsx(React.Fragment, { children: (text === null || text === void 0 ? void 0 : text.label) || text }, key); } var status = domText.status, color = domText.color; var Status = TableStatus[status || 'Init']; // 如果类型存在优先使用类型 if (Status) { return /*#__PURE__*/_jsx(Status, { children: domText.text }, key); } // 如果不存在使用颜色 if (color) { return /*#__PURE__*/_jsx(ProFieldBadgeColor, { color: color, children: domText.text }, key); } // 什么都没有使用 text return /*#__PURE__*/_jsx(React.Fragment, { children: domText.text || domText }, key); };