-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathIconBase.tsx
94 lines (78 loc) · 2.52 KB
/
IconBase.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as React from 'react';
import type { AbstractNode, IconDefinition } from '@ant-design/icons-svg/lib/types';
import { generate, getSecondaryColor, isIconDefinition, warning, useInsertStyles } from '../utils';
export interface IconProps {
icon: IconDefinition;
className?: string;
onClick?: React.MouseEventHandler<SVGSVGElement>;
color?: string;
style?: React.CSSProperties;
primaryColor?: string; // only for two-tone
secondaryColor?: string; // only for two-tone
focusable?: string;
}
export interface TwoToneColorPaletteSetter {
primaryColor: string;
secondaryColor?: string;
}
export interface TwoToneColorPalette extends TwoToneColorPaletteSetter {
calculated?: boolean; // marker for calculation
}
const twoToneColorPalette: TwoToneColorPalette = {
primaryColor: '#333',
secondaryColor: '#E6E6E6',
calculated: false,
};
function setTwoToneColors({ primaryColor, secondaryColor }: TwoToneColorPaletteSetter) {
twoToneColorPalette.primaryColor = primaryColor;
twoToneColorPalette.secondaryColor = secondaryColor || getSecondaryColor(primaryColor);
twoToneColorPalette.calculated = !!secondaryColor;
}
function getTwoToneColors(): TwoToneColorPalette {
return {
...twoToneColorPalette,
};
}
interface IconBaseComponent<P> extends React.FC<P> {
getTwoToneColors: typeof getTwoToneColors;
setTwoToneColors: typeof setTwoToneColors;
}
const IconBase: IconBaseComponent<IconProps> = (props) => {
const { icon, className, onClick, style, primaryColor, secondaryColor, ...restProps } = props;
const svgRef = React.useRef<HTMLElement>();
let colors: TwoToneColorPalette = twoToneColorPalette;
if (primaryColor) {
colors = {
primaryColor,
secondaryColor: secondaryColor || getSecondaryColor(primaryColor),
};
}
useInsertStyles(svgRef);
warning(isIconDefinition(icon), `icon should be icon definiton, but got ${icon}`);
if (!isIconDefinition(icon)) {
return null;
}
let target = icon;
if (target && typeof target.icon === 'function') {
target = {
...target,
icon: target.icon(colors.primaryColor, colors.secondaryColor),
};
}
return generate(target.icon as AbstractNode, `svg-${target.name}`, {
className,
onClick,
style,
'data-icon': target.name,
width: '1em',
height: '1em',
fill: 'currentColor',
'aria-hidden': 'true',
...restProps,
ref: svgRef,
});
};
IconBase.displayName = 'IconReact';
IconBase.getTwoToneColors = getTwoToneColors;
IconBase.setTwoToneColors = setTwoToneColors;
export default IconBase;