2022-01-20 15:16:12 +08:00
|
|
|
import * as antIcons from '@ant-design/icons';
|
2022-01-20 18:19:59 +08:00
|
|
|
import AntdIcon, { createFromIconfontCN } from '@ant-design/icons';
|
2022-01-20 15:16:12 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
2022-01-20 18:19:59 +08:00
|
|
|
let IconFont: any;
|
2022-01-20 15:16:12 +08:00
|
|
|
|
|
|
|
export const icons = new Map<string, any>();
|
|
|
|
|
|
|
|
export function registerIcon(type: string, icon: any = IconFont) {
|
|
|
|
icons.set(type.toLowerCase(), icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasIcon(type: string) {
|
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return icons.has(type.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function registerIcons(components) {
|
|
|
|
Object.keys(components).forEach((type) => {
|
|
|
|
registerIcon(type, components[type]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(antIcons).forEach((name) => {
|
|
|
|
if (name.endsWith('Outlined')) {
|
|
|
|
registerIcon(name, antIcons[name]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IconProps {
|
|
|
|
type: string;
|
2022-01-20 18:19:59 +08:00
|
|
|
component?: any;
|
2022-01-20 15:16:12 +08:00
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2022-01-20 18:19:59 +08:00
|
|
|
export const Icon = (props: IconProps) => {
|
|
|
|
const { type = '', component, ...restProps } = props;
|
|
|
|
if (component) {
|
|
|
|
return <AntdIcon component={component} {...restProps} />;
|
|
|
|
}
|
2022-01-20 15:16:12 +08:00
|
|
|
if (type && icons.has(type.toLowerCase())) {
|
|
|
|
const IconComponent = icons.get(type.toLowerCase());
|
|
|
|
return <IconComponent {...restProps} />;
|
|
|
|
}
|
2022-01-20 18:19:59 +08:00
|
|
|
if (type && IconFont) {
|
|
|
|
return <IconFont type={type} />;
|
|
|
|
}
|
2022-01-20 15:16:12 +08:00
|
|
|
return null;
|
2022-01-20 18:19:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Icon.createFromIconfontCN = (options) => {
|
|
|
|
IconFont = createFromIconfontCN(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
Icon.register = (icons?: any) => {
|
|
|
|
registerIcons(icons);
|
|
|
|
};
|
2022-01-20 15:16:12 +08:00
|
|
|
|
|
|
|
export default Icon;
|