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.

203 lines
5.1 KiB

import * as React from 'react';
import classNames from 'classnames';
6 years ago
import * as allIcons from '@ant-design/icons/lib/dist';
import ReactIcon from '@ant-design/icons-react';
import createFromIconfontCN from './IconFont';
import {
6 years ago
svgBaseProps,
withThemeSuffix,
removeTypeTheme,
getThemeFromTypeName,
alias,
} from './utils';
6 years ago
import warning from '../_util/warning';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
6 years ago
import { getTwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
6 years ago
// Initial setting
6 years ago
ReactIcon.add(...Object.keys(allIcons).map(key => (allIcons as any)[key]));
6 years ago
setTwoToneColor('#1890ff');
let defaultTheme: ThemeType = 'outlined';
let dangerousTheme: ThemeType | undefined = undefined;
export interface TransferLocale {
icon: string;
}
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 {
tabIndex?: number;
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;
prefixCls?: string;
role?: string;
}
export interface IconComponent<P> extends React.SFC<P> {
createFromIconfontCN: typeof createFromIconfontCN;
getTwoToneColor: typeof getTwoToneColor;
setTwoToneColor: typeof setTwoToneColor;
unstable_ChangeThemeOfIconsDangerously?: typeof unstable_ChangeThemeOfIconsDangerously;
unstable_ChangeDefaultThemeOfIcons?: typeof unstable_ChangeDefaultThemeOfIcons;
}
6 years ago
const Icon: IconComponent<IconProps> = props => {
const {
6 years ago
// affect outter <i>...</i>
className,
6 years ago
// affect inner <svg>...</svg>
type,
6 years ago
component: Component,
viewBox,
spin,
6 years ago
tabIndex,
onClick,
6 years ago
// children
children,
// other
6 years ago
theme, // default to outlined
twoToneColor,
...restProps
} = 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`.',
);
6 years ago
const classString = classNames(
{
[`anticon`]: true,
[`anticon-${type}`]: Boolean(type),
},
className,
);
const svgClassString = classNames({
[`anticon-spin`]: !!spin || type === 'loading',
});
let innerNode: React.ReactNode;
6 years ago
// component > children > type
6 years ago
if (Component) {
const innerSvgProps: CustomIconComponentProps = {
6 years ago
...svgBaseProps,
className: svgClassString,
6 years ago
viewBox,
6 years ago
};
6 years ago
if (!viewBox) {
delete innerSvgProps.viewBox;
}
6 years ago
innerNode = <Component {...innerSvgProps}>{children}</Component>;
6 years ago
}
if (children) {
6 years ago
warning(
6 years ago
Boolean(viewBox) ||
(React.Children.count(children) === 1 &&
React.isValidElement(children) &&
React.Children.only(children).type === 'use'),
6 years ago
'Make sure that you provide correct `viewBox`' +
6 years ago
' prop (default `0 0 1024 1024`) to the icon.',
6 years ago
);
const innerSvgProps: CustomIconComponentProps = {
6 years ago
...svgBaseProps,
className: svgClassString,
};
innerNode = (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
6 years ago
);
}
if (typeof type === 'string') {
let computedType = type;
if (theme) {
const themeInName = getThemeFromTypeName(type);
6 years ago
warning(
!themeInName || theme === themeInName,
`The icon name '${type}' already specify a theme '${themeInName}',` +
6 years ago
` the 'theme' prop '${theme}' will be ignored.`,
);
}
computedType = withThemeSuffix(
removeTypeTheme(alias(computedType)),
dangerousTheme || theme || defaultTheme,
);
innerNode = (
6 years ago
<ReactIcon className={svgClassString} type={computedType} primaryColor={twoToneColor} />
6 years ago
);
}
let iconTabIndex = tabIndex;
if (iconTabIndex === undefined && onClick) {
iconTabIndex = -1;
}
6 years ago
return (
<LocaleReceiver componentName="Icon">
{(locale: TransferLocale) => (
<i
aria-label={`${locale.icon}: ${type}`}
{...restProps}
tabIndex={iconTabIndex}
onClick={onClick}
className={classString}
>
{innerNode}
</i>
)}
</LocaleReceiver>
);
8 years ago
};
function unstable_ChangeThemeOfIconsDangerously(theme?: ThemeType) {
warning(
false,
`You are using the unstable method 'Icon.unstable_ChangeThemeOfAllIconsDangerously', ` +
6 years ago
`make sure that all the icons with theme '${theme}' display correctly.`,
);
dangerousTheme = theme;
}
function unstable_ChangeDefaultThemeOfIcons(theme: ThemeType) {
warning(
false,
`You are using the unstable method 'Icon.unstable_ChangeDefaultThemeOfIcons', ` +
6 years ago
`make sure that all the icons with theme '${theme}' display correctly.`,
);
defaultTheme = theme;
}
Icon.createFromIconfontCN = createFromIconfontCN;
Icon.getTwoToneColor = getTwoToneColor;
Icon.setTwoToneColor = setTwoToneColor;
export default Icon;