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.

135 lines
3.0 KiB

import * as React from 'react';
import classNames from 'classnames';
import { antDesignIcons } from '@ant-design/icons';
import ReactIcon from '@ant-design/icons-react';
import createFromIconfontCN from './IconFont';
import { svgBaseProps } from './utils';
6 years ago
import warning from '../_util/warning';
6 years ago
ReactIcon.add(...antDesignIcons);
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 interface IconProps {
6 years ago
type?: string;
className?: string;
title?: string;
6 years ago
onClick?: React.MouseEventHandler<HTMLElement>;
component?: React.ComponentType<CustomIconComponentProps>;
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,
} = 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),
'Make sure that you provide correct `viewBox`' +
' prop (default `0 0 1024 1024`) to Icon.',
);
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 (type) {
6 years ago
return (
<i className={classString} title={title} style={style} onClick={onClick}>
6 years ago
<ReactIcon
className={svgClassString}
type={type}
style={svgStyle}
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
Icon.displayName = 'Icon';
(Icon as IconType).createFromIconfontCN = createFromIconfontCN;
export default Icon as IconType;