collect-system/packages/iconer/src/components/svg-icon.tsx

40 lines
1.2 KiB
TypeScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { IconName } from "../generated/icon-names";
import { useLazySvgImport } from "../utils/useLazySvgImport";
interface IProps {
name: IconName;
className?: string,
svgProp?: React.SVGProps<SVGSVGElement>;
size?: 'small' | 'middle' | 'large' | number;
}
const sizeMap: { [key: string]: number } = {
small: 16,
middle: 24,
large: 32
};
function Icon(props: IProps) {
const { name, svgProp, className = '', size = 'middle' } = props;
const { loading, Svg } = useLazySvgImport(name);
const finalSize = typeof size === 'number' ? size : sizeMap[size] || sizeMap.middle;
const svgStyle = {
width: finalSize,
height: finalSize,
...(svgProp?.style) // 如果svgProp中包含style则合并样式
};
return (
<>
{loading && (
<div className={` ${className}`} style={{
borderRadius: "100%",
height: 24,
width: 24
}}></div>
)}
{Svg && (
<span className={className}> <Svg style={svgStyle} {...svgProp} /></span>
)}
</>
);
}
export default Icon;