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.

160 lines
4.0 KiB

import * as React from 'react';
import classNames from 'classnames';
import * as allIcons from '@ant-design/icons';
import ReactIcon from '@ant-design/icons-react';
import createFromIconfontCN from './IconFont';
import { svgBaseProps, withThemeSuffix } 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');
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;
svgStyle?: React.CSSProperties;
svgClassName?: string;
prefixCls?: string;
}
6 years ago
const Icon: React.SFC<IconProps> = (props) => {
const {
6 years ago
// affect outter <i>...</i>
6 years ago
title,
className,
6 years ago
onClick,
style,
// affect inner <svg>...</svg>
type,
6 years ago
component: Component,
viewBox,
spin,
svgClassName,
svgStyle = {},
6 years ago
// children
children,
// other
6 years ago
theme, // default to outlined
twoToneColor,
} = 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',
}, svgClassName);
6 years ago
// component > children > type
6 years ago
if (Component) {
const innerSvgProps: CustomIconComponentProps = {
6 years ago
...svgBaseProps,
className: svgClassString,
style: svgStyle,
6 years ago
viewBox,
6 years ago
};
6 years ago
if (!viewBox) {
delete innerSvgProps.viewBox;
}
return (
<i className={classString} title={title} style={style} onClick={onClick}>
6 years ago
<Component {...innerSvgProps} >
{children}
</Component>
</i>
6 years ago
);
}
if (children) {
6 years ago
warning(
Boolean(viewBox) || React.Children.count(children) === 1 && 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,
style: svgStyle,
6 years ago
};
6 years ago
return (
<i className={classString} title={title} style={style} onClick={onClick}>
6 years ago
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
</i>
6 years ago
);
}
if (typeof type === 'string') {
let computedType = type;
if (theme) {
computedType = withThemeSuffix(type, theme);
}
6 years ago
// default outlined
const computedType = withThemeSuffix(
getTypeWithoutTheme(type),
theme || 'outlined',
);
6 years ago
return (
<i className={classString} title={title} style={style} onClick={onClick}>
6 years ago
<ReactIcon
className={svgClassString}
type={computedType}
style={svgStyle}
6 years ago
primaryColor={twoToneColor}
6 years ago
/>
</i>
6 years ago
);
}
6 years ago
return (
<i className={classString} title={title} style={style} onClick={onClick} />
);
8 years ago
};
export type IconType = typeof Icon & {
createFromIconfontCN: typeof createFromIconfontCN;
6 years ago
getTwoToneColor: typeof getTwoToneColor;
setTwoToneColor: typeof setTwoToneColor;
};
6 years ago
Icon.displayName = 'Icon';
(Icon as IconType).createFromIconfontCN = createFromIconfontCN;
6 years ago
(Icon as IconType).getTwoToneColor = getTwoToneColor;
(Icon as IconType).setTwoToneColor = setTwoToneColor;
export default Icon as IconType;