UNPKG

nsn-util

Version:

NSN工具辅助组件

72 lines (58 loc) 1.62 kB
import * as AntdIcons from '@ant-design/icons'; import { NConst } from 'nsn-const'; import React from 'react'; import * as NStr from './NStr'; import * as NType from './NType'; 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 对象 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.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 ? NConst.EMPTY : _props$def; var _Icon = IconsMap.get(name); if (_Icon) { return React.createElement(_Icon, Object.assign({}, { style: style })); } if (NType.isString(def) && !NStr.isTrimEmpty(def)) { _Icon = IconsMap.get(def); if (_Icon) { return React.createElement(_Icon, Object.assign({}, { style: style })); } } return def; } return NConst.EMPTY; }; export { get, ALL };