You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.6 KiB

import * as React from 'react';
import classNames from 'classnames';
6 years ago
import * as allIcons from '@ant-design/icons/lib/dist';
import ReactIcon from '@ant-design/icons-react';
import createFromIconfontCN from './IconFont';
import {
svgBaseProps, withThemeSuffix,
removeTypeTheme, getThemeFromTypeName,
} from './utils';
6 years ago
import warning from '../_util/warning';
6 years ago
import { getTwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
6 years ago
// Initial setting
ReactIcon.add(...Object.keys(allIcons).map((key) => (allIcons as any)[key]));
6 years ago
setTwoToneColor('#1890ff');
let defaultTheme: ThemeType = 'outlined';
let dangerousTheme: ThemeType | undefined = undefined;
6 years ago
export interface CustomIconComponentProps {
width: string | number;
height: string | number;
fill: string;
6 years ago
viewBox?: string;
6 years ago
className?: string;
style?: React.CSSProperties;
6 years ago
['aria-hidden']?: string;
6 years ago
}
export type ThemeType = 'filled' | 'outlined' | 'twoTone';
export interface IconProps {
6 years ago
type?: string;
className?: string;
theme?: ThemeType;
title?: string;
6 years ago
onClick?: React.MouseEventHandler<HTMLElement>;
component?: React.ComponentType<CustomIconComponentProps>;
6 years ago
twoToneColor?: string;
6 years ago
viewBox?: string;
spin?: boolean;
style?: React.CSSProperties;
prefixCls?: string;
}
export interface IconComponent<P> extends React.SFC<P> {
createFromIconfontCN?: typeof createFromIconfontCN;
getTwoToneColor?: typeof getTwoToneColor;
setTwoToneColor?: typeof setTwoToneColor;
unstable_ChangeThemeOfIconsDangerously?: typeof unstable_ChangeThemeOfIconsDangerously;
unstable_ChangeDefaultThemeOfIcons?: typeof unstable_ChangeDefaultThemeOfIcons;
}
const Icon: IconComponent<IconProps> = (props) => {
const {
6 years ago
// affect outter <i>...</i>
className,
6 years ago
// affect inner <svg>...</svg>
type,
6 years ago
component: Component,
viewBox,
spin,
6 years ago
// children
children,
// other
6 years ago
theme, // default to outlined
twoToneColor,
...restProps
} = props;
6 years ago
warning(
6 years ago
Boolean(type || Component || children),
6 years ago
'Icon should have `type` prop or `component` prop or `children`.',
);
const classString = classNames({
[`anticon`]: true,
[`anticon-${type}`]: Boolean(type),
}, className);
const svgClassString = classNames({
[`anticon-spin`]: !!spin || type === 'loading',
});
let innerNode;
6 years ago
// component > children > type
6 years ago
if (Component) {
const innerSvgProps: CustomIconComponentProps = {
6 years ago
...svgBaseProps,
className: svgClassString,
6 years ago
viewBox,
6 years ago
};
6 years ago
if (!viewBox) {
delete innerSvgProps.viewBox;
}
innerNode = (
<Component {...innerSvgProps} >
{children}
</Component>
6 years ago
);
}
if (children) {
6 years ago
warning(
Boolean(viewBox) || (
React.Children.count(children) === 1 &&
React.isValidElement(children) &&
React.Children.only(children).type === 'use'
),
6 years ago
'Make sure that you provide correct `viewBox`' +
' prop (default `0 0 1024 1024`) to the icon.',
6 years ago
);
const innerSvgProps: CustomIconComponentProps = {
6 years ago
...svgBaseProps,
className: svgClassString,
};
innerNode = (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
6 years ago
);
}
if (typeof type === 'string') {
let computedType = type;
if (theme) {
const themeInName = getThemeFromTypeName(type);
warning(!themeInName || theme === themeInName,
`The icon name '${type}' already specify a theme '${themeInName}',` +
` the 'theme' prop '${theme}' will be ignored.`);
}
computedType = withThemeSuffix(
removeTypeTheme(type),
dangerousTheme || theme || defaultTheme,
);
innerNode = (
<ReactIcon
className={svgClassString}
type={computedType}
primaryColor={twoToneColor}
/>
6 years ago
);
}
6 years ago
return (
<i {...restProps} className={classString}>
{innerNode}
</i>
);
8 years ago
};
function unstable_ChangeThemeOfIconsDangerously(theme?: ThemeType) {
warning(
false,
`You are using the unstable method 'Icon.unstable_ChangeThemeOfAllIconsDangerously', ` +
`make sure that all the icons with theme '${theme}' display correctly.`,
);
dangerousTheme = theme;
}
function unstable_ChangeDefaultThemeOfIcons(theme: ThemeType) {
warning(
false,
`You are using the unstable method 'Icon.unstable_ChangeDefaultThemeOfIcons', ` +
`make sure that all the icons with theme '${theme}' display correctly.`,
);
defaultTheme = theme;
}
Icon.createFromIconfontCN = createFromIconfontCN;
Icon.getTwoToneColor = getTwoToneColor;
Icon.setTwoToneColor = setTwoToneColor;
export default Icon;