2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-10-08 15:09:56 +08:00
|
|
|
import classNames from 'classnames';
|
2018-07-19 12:23:33 +08:00
|
|
|
import { antDesignIcons } from '@ant-design/icons';
|
2018-08-04 18:42:41 +08:00
|
|
|
import ReactIcon from '@ant-design/icons-react';
|
2018-08-16 15:22:38 +08:00
|
|
|
import createFromIconfontCN from './IconFont';
|
2018-08-15 17:21:02 +08:00
|
|
|
import { getComputedSvgStyle, svgBaseProps } from './utils';
|
|
|
|
import warning from '../_util/warning';
|
2018-07-18 10:07:10 +08:00
|
|
|
|
2018-08-04 18:42:41 +08:00
|
|
|
ReactIcon.add(...antDesignIcons);
|
|
|
|
|
2018-08-15 17:21:02 +08:00
|
|
|
export interface CustomIconComponentProps {
|
|
|
|
width: string | number;
|
|
|
|
height: string | number;
|
|
|
|
fill: string;
|
2018-08-16 11:41:37 +08:00
|
|
|
viewBox?: string;
|
2018-08-15 17:21:02 +08:00
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
2018-08-16 11:41:37 +08:00
|
|
|
['aria-hidden']?: string;
|
2018-08-15 17:21:02 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 17:11:06 +08:00
|
|
|
export interface IconProps {
|
2018-08-15 17:21:02 +08:00
|
|
|
type?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
className?: string;
|
2016-08-22 17:26:14 +08:00
|
|
|
title?: string;
|
2018-08-15 17:21:02 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
|
|
|
component?: React.ComponentType<CustomIconComponentProps>;
|
|
|
|
viewBox?: string;
|
2016-10-08 15:09:56 +08:00
|
|
|
spin?: boolean;
|
2016-10-21 16:27:26 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-08-04 18:42:41 +08:00
|
|
|
svgStyle?: React.CSSProperties;
|
|
|
|
svgClassName?: string;
|
2018-08-06 18:10:30 +08:00
|
|
|
rotate?: number;
|
2018-08-04 18:42:41 +08:00
|
|
|
flip?: 'horizontal' | 'vertical' | 'both';
|
2018-08-06 18:10:30 +08:00
|
|
|
tag?: string;
|
2018-08-04 18:42:41 +08:00
|
|
|
prefixCls?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2018-08-15 17:21:02 +08:00
|
|
|
const Icon: React.SFC<IconProps> = (props) => {
|
2018-08-04 18:42:41 +08:00
|
|
|
const {
|
2018-08-15 17:21:02 +08:00
|
|
|
// affect outter <i>...</i>
|
2018-08-16 11:41:37 +08:00
|
|
|
tag: Tag = 'i',
|
|
|
|
title,
|
2018-08-16 16:13:18 +08:00
|
|
|
className,
|
2018-08-15 17:21:02 +08:00
|
|
|
onClick,
|
|
|
|
style,
|
|
|
|
|
|
|
|
// affect inner <svg>...</svg>
|
|
|
|
type,
|
2018-08-16 11:41:37 +08:00
|
|
|
component: Component,
|
|
|
|
viewBox,
|
2018-08-06 18:10:30 +08:00
|
|
|
spin,
|
|
|
|
flip,
|
2018-08-04 18:42:41 +08:00
|
|
|
svgClassName,
|
2018-08-06 18:10:30 +08:00
|
|
|
rotate = 0,
|
|
|
|
svgStyle = {},
|
2018-08-15 17:21:02 +08:00
|
|
|
|
|
|
|
// children
|
|
|
|
children,
|
2018-08-04 18:42:41 +08:00
|
|
|
} = props;
|
2018-08-15 17:21:02 +08:00
|
|
|
|
|
|
|
warning(
|
2018-08-16 11:41:37 +08:00
|
|
|
Boolean(type || Component || children),
|
2018-08-15 17:21:02 +08:00
|
|
|
'Icon should have `type` prop or `component` prop or `children`.',
|
|
|
|
);
|
|
|
|
|
2018-08-16 16:13:18 +08:00
|
|
|
const classString = classNames({
|
|
|
|
[`anticon`]: true,
|
|
|
|
[`anticon-${type}`]: Boolean(type),
|
|
|
|
}, className);
|
2018-08-04 18:42:41 +08:00
|
|
|
|
2018-08-14 15:31:20 +08:00
|
|
|
const svgClassString = classNames({
|
|
|
|
[`anticon-spin`]: !!spin || type === 'loading',
|
2018-08-16 16:13:18 +08:00
|
|
|
}, svgClassName);
|
2018-08-14 15:31:20 +08:00
|
|
|
|
2018-08-15 17:21:02 +08:00
|
|
|
const computedSvgStyle = getComputedSvgStyle(
|
|
|
|
{ rotate, flip },
|
|
|
|
svgStyle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// component > children > type
|
2018-08-16 11:41:37 +08:00
|
|
|
if (Component) {
|
|
|
|
const innerSvgProps: CustomIconComponentProps = {
|
2018-08-15 17:21:02 +08:00
|
|
|
...svgBaseProps,
|
|
|
|
className: svgClassString,
|
|
|
|
style: computedSvgStyle,
|
2018-08-16 11:41:37 +08:00
|
|
|
viewBox,
|
2018-08-15 17:21:02 +08:00
|
|
|
};
|
2018-08-16 11:41:37 +08:00
|
|
|
if (!viewBox) {
|
|
|
|
delete innerSvgProps.viewBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tag className={classString} title={title} style={style} onClick={onClick}>
|
|
|
|
<Component {...innerSvgProps} >
|
|
|
|
{children}
|
|
|
|
</Component>
|
|
|
|
</Tag>
|
2018-08-15 17:21:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (children) {
|
2018-08-16 11:41:37 +08:00
|
|
|
warning(
|
|
|
|
Boolean(viewBox),
|
|
|
|
'Make sure that you provide correct `viewBox`' +
|
|
|
|
' prop (default `0 0 1024 1024`) to Icon.',
|
|
|
|
);
|
|
|
|
const innerSvgProps: CustomIconComponentProps = {
|
2018-08-15 17:21:02 +08:00
|
|
|
...svgBaseProps,
|
|
|
|
className: svgClassString,
|
|
|
|
style: computedSvgStyle,
|
|
|
|
};
|
2018-08-16 11:41:37 +08:00
|
|
|
return (
|
|
|
|
<Tag className={classString} title={title} style={style} onClick={onClick}>
|
|
|
|
<svg {...innerSvgProps} viewBox={viewBox}>
|
|
|
|
{children}
|
|
|
|
</svg>
|
|
|
|
</Tag>
|
2018-08-15 17:21:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type) {
|
2018-08-16 11:41:37 +08:00
|
|
|
return (
|
|
|
|
<Tag className={classString} title={title} style={style} onClick={onClick}>
|
|
|
|
<ReactIcon
|
|
|
|
className={svgClassString}
|
|
|
|
type={type}
|
|
|
|
style={computedSvgStyle}
|
|
|
|
/>
|
|
|
|
</Tag>
|
2018-08-15 17:21:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:41:37 +08:00
|
|
|
return (
|
|
|
|
<Tag className={classString} title={title} style={style} onClick={onClick} />
|
2018-07-19 20:08:04 +08:00
|
|
|
);
|
2017-04-06 03:41:03 +08:00
|
|
|
};
|
2017-04-06 02:50:12 +08:00
|
|
|
|
2018-08-16 15:22:38 +08:00
|
|
|
export type IconType = typeof Icon & {
|
|
|
|
createFromIconfontCN: typeof createFromIconfontCN;
|
2018-07-19 12:23:33 +08:00
|
|
|
};
|
|
|
|
|
2018-08-15 17:21:02 +08:00
|
|
|
Icon.displayName = 'Icon';
|
2018-08-16 15:22:38 +08:00
|
|
|
(Icon as IconType).createFromIconfontCN = createFromIconfontCN;
|
2018-07-19 12:23:33 +08:00
|
|
|
|
|
|
|
export default Icon as IconType;
|