ant-design/components/icon/index.tsx

146 lines
3.3 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import { antDesignIcons } from '@ant-design/icons';
2018-08-04 18:42:41 +08:00
import ReactIcon from '@ant-design/icons-react';
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
}
export interface IconProps {
2018-08-15 17:21:02 +08:00
type?: string;
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;
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;
rotate?: number;
2018-08-04 18:42:41 +08:00
flip?: 'horizontal' | 'vertical' | 'both';
tag?: string;
2018-08-04 18:42:41 +08:00
prefixCls?: string;
}
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,
spin,
flip,
2018-08-04 18:42:41 +08:00
svgClassName,
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
const svgClassString = classNames({
[`anticon-spin`]: !!spin || type === 'loading',
2018-08-16 16:13:18 +08:00
}, svgClassName);
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} />
);
2017-04-06 03:41:03 +08:00
};
2017-04-06 02:50:12 +08:00
export type IconType = typeof Icon & {
createFromIconfontCN: typeof createFromIconfontCN;
};
2018-08-15 17:21:02 +08:00
Icon.displayName = 'Icon';
(Icon as IconType).createFromIconfontCN = createFromIconfontCN;
export default Icon as IconType;