UNPKG

nsn-util

Version:

NSN工具辅助组件

92 lines (68 loc) 3.34 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.ALL = exports.get = void 0; var AntdIcons = _interopRequireWildcard(require("@ant-design/icons")); var _nsnConst = require("nsn-const"); var _react = _interopRequireDefault(require("react")); var NStr = _interopRequireWildcard(require("./NStr")); var NType = _interopRequireWildcard(require("./NType")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var isIcon = function isIcon() { var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return key.endsWith('Outlined'); }; // 涵盖了一些非 icon 的函数 var Icons = AntdIcons; /** * 图标键值对 */ var ALL = {}; // 移除 非 icon 对象 exports.ALL = ALL; var IconsMap = new Map(); Object.keys(Icons).forEach(function (key) { if (isIcon(key)) { IconsMap.set(key, Icons[key]); ALL[key] = Icons[key]; } }); /** * 获取图标 * @param name 图标名称 * @param def 获取不到图标时,默认返回的对象 * @param style 图标样式 */ var get = function get(props) { if (NType.isString(props)) { var Icon = IconsMap.get(props); if (Icon) { return _react.default.createElement(Icon, null); } } else if (props) { var name = props.name, _props$style = props.style, style = _props$style === void 0 ? {} : _props$style, _props$def = props.def, def = _props$def === void 0 ? _nsnConst.NConst.EMPTY : _props$def; var _Icon = IconsMap.get(name); if (_Icon) { return _react.default.createElement(_Icon, Object.assign({}, { style: style })); } if (NType.isString(def) && !NStr.isTrimEmpty(def)) { _Icon = IconsMap.get(def); if (_Icon) { return _react.default.createElement(_Icon, Object.assign({}, { style: style })); } } return def; } return _nsnConst.NConst.EMPTY; }; exports.get = get;