ant-design/components/icon/index.tsx

155 lines
3.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
2018-08-30 20:43:03 +08:00
import * as allIcons from '@ant-design/icons';
2018-08-04 18:42:41 +08:00
import ReactIcon from '@ant-design/icons-react';
import createFromIconfontCN from './IconFont';
2018-09-02 19:48:57 +08:00
import {
svgBaseProps, withThemeSuffix,
removeTypeTheme, getThemeFromTypeName,
} from './utils';
2018-08-15 17:21:02 +08:00
import warning from '../_util/warning';
2018-09-01 19:44:21 +08:00
import { getTwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
2018-07-18 10:07:10 +08:00
2018-09-01 17:44:15 +08:00
// Initial setting
2018-08-30 20:43:03 +08:00
ReactIcon.add(...Object.keys(allIcons).map((key) => (allIcons as any)[key]));
2018-09-01 19:44:21 +08:00
setTwoToneColor('#1890ff');
2018-08-04 18:42:41 +08:00
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
}
2018-08-31 11:14:51 +08:00
export type ThemeType = 'filled' | 'outlined' | 'twoTone';
2018-08-30 22:53:24 +08:00
export interface IconProps {
2018-08-15 17:21:02 +08:00
type?: string;
className?: string;
2018-08-30 22:53:24 +08:00
theme?: ThemeType;
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>;
2018-09-01 19:44:21 +08:00
twoToneColor?: string;
2018-08-15 17:21:02 +08:00
viewBox?: string;
spin?: boolean;
2016-10-21 16:27:26 +08:00
style?: React.CSSProperties;
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 16:13:18 +08:00
className,
2018-08-15 17:21:02 +08:00
// affect inner <svg>...</svg>
type,
2018-08-16 11:41:37 +08:00
component: Component,
viewBox,
spin,
2018-08-15 17:21:02 +08:00
// children
children,
2018-08-30 22:53:24 +08:00
// other
2018-09-01 19:44:21 +08:00
theme, // default to outlined
twoToneColor,
...restProps
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-09-01 20:11:13 +08:00
});
let innerNode;
2018-08-15 17:21:02 +08:00
// 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,
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;
}
innerNode = (
<Component {...innerSvgProps} >
{children}
</Component>
2018-08-15 17:21:02 +08:00
);
}
if (children) {
2018-08-16 11:41:37 +08:00
warning(
2018-08-30 22:53:24 +08:00
Boolean(viewBox) || React.Children.count(children) === 1 && React.Children.only(children).type === 'use',
2018-08-16 11:41:37 +08:00
'Make sure that you provide correct `viewBox`' +
2018-08-30 22:53:24 +08:00
' prop (default `0 0 1024 1024`) to the icon.',
2018-08-16 11:41:37 +08:00
);
const innerSvgProps: CustomIconComponentProps = {
2018-08-15 17:21:02 +08:00
...svgBaseProps,
className: svgClassString,
};
innerNode = (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
2018-08-15 17:21:02 +08:00
);
}
2018-08-30 22:53:24 +08:00
if (typeof type === 'string') {
let computedType = type;
if (theme) {
2018-09-02 19:48:57 +08:00
const alreadyHaveTheme = getThemeFromTypeName(type);
warning(!alreadyHaveTheme,
`This icon already has a theme '${alreadyHaveTheme}'.` +
` The prop 'theme' ${theme} will be ignored.`);
2018-08-30 22:53:24 +08:00
}
2018-09-02 19:48:57 +08:00
computedType = withThemeSuffix(
removeTypeTheme(type),
theme || 'outlined',
);
innerNode = (
<ReactIcon
className={svgClassString}
type={computedType}
primaryColor={twoToneColor}
/>
2018-08-15 17:21:02 +08:00
);
}
2018-08-16 11:41:37 +08:00
return (
<i {...restProps} className={classString}>
{innerNode}
</i>
);
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-09-01 19:44:21 +08:00
getTwoToneColor: typeof getTwoToneColor;
setTwoToneColor: typeof setTwoToneColor;
};
2018-08-15 17:21:02 +08:00
Icon.displayName = 'Icon';
(Icon as IconType).createFromIconfontCN = createFromIconfontCN;
2018-09-01 19:44:21 +08:00
(Icon as IconType).getTwoToneColor = getTwoToneColor;
(Icon as IconType).setTwoToneColor = setTwoToneColor;
export default Icon as IconType;