2024-03-21 16:45:03 +08:00
|
|
|
import Icon from '@ant-design/icons';
|
|
|
|
|
import { IconComponentProps } from '@ant-design/icons/lib/components/Icon';
|
2024-02-27 19:05:50 +08:00
|
|
|
|
2024-03-21 16:45:03 +08:00
|
|
|
const importAll = (requireContext: __WebpackModuleApi.RequireContext) => {
|
|
|
|
|
const list = requireContext.keys().map((key) => {
|
|
|
|
|
const name = key.replace(/\.\/(.*)\.\w+$/, '$1');
|
|
|
|
|
return { name, value: requireContext(key) };
|
|
|
|
|
});
|
|
|
|
|
return list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let routeList: { name: string; value: string }[] = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
routeList = importAll(require.context('@/assets/svg', true, /\.svg$/));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(error);
|
|
|
|
|
routeList = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IProps extends IconComponentProps {
|
2024-02-27 19:05:50 +08:00
|
|
|
name: string;
|
2024-03-21 16:45:03 +08:00
|
|
|
width: string | number;
|
2024-04-08 19:13:45 +08:00
|
|
|
height?: string | number;
|
2024-02-27 19:05:50 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-08 19:13:45 +08:00
|
|
|
const SvgIcon = ({ name, width, height, ...restProps }: IProps) => {
|
2024-03-21 16:45:03 +08:00
|
|
|
const ListItem = routeList.find((item) => item.name === name);
|
|
|
|
|
return (
|
|
|
|
|
<Icon
|
2024-04-08 19:13:45 +08:00
|
|
|
component={() => (
|
|
|
|
|
<img src={ListItem?.value} alt="" width={width} height={height} />
|
|
|
|
|
)}
|
2024-03-21 16:45:03 +08:00
|
|
|
{...(restProps as any)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2024-02-27 19:05:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SvgIcon;
|