ant-design/components/icon/CustomIcon.tsx

126 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-07-18 13:38:19 +08:00
import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import { Omit } from '../_util/type';
import { IconProps } from './index';
2018-07-18 13:38:19 +08:00
export interface CustomIconProps extends Omit<IconProps, 'type'> {
viewBox?: string;
component?: React.ComponentType<CustomIconComponentProps>;
}
export interface CustomIconComponentProps extends Omit<CustomIconProps, 'component'> {
2018-07-18 13:38:19 +08:00
}
const CustomIcon: React.SFC<CustomIconProps> = (props) => {
const {
className = '',
spin,
viewBox = '0 0 24 24',
children,
component: Component,
} = props;
2018-07-18 13:38:19 +08:00
const classString = classNames({
anticon: true,
'anticon-spin': !!spin,
}, className);
let content = (
<svg
{...omit(props, ['type', 'spin'])}
width={'1em'}
height={'1em'}
fill={'currentColor'}
aria-hidden={'true'}
viewBox={viewBox}
>
{children}
</svg>
);
if (Component) {
content = (
<Component
{...omit(props, ['type', 'spin', 'component'])}
2018-07-18 13:38:19 +08:00
width={'1em'}
height={'1em'}
fill={'currentColor'}
aria-hidden={'true'}
viewBox={viewBox}
>
{children}
</Component>
2018-07-18 13:38:19 +08:00
);
}
return (
<i className={classString}>
{content}
</i>
2018-07-18 13:38:19 +08:00
);
};
2018-07-18 17:35:39 +08:00
const customCache = new Set<string>();
export interface CustomIconOptions {
namespace?: string;
prefix?: string;
scriptLink?: string;
2018-07-19 20:32:34 +08:00
[key: string]: any;
2018-07-18 17:35:39 +08:00
}
export function create(options: CustomIconOptions = {}): React.ComponentClass<IconProps> {
const { namespace, prefix = '', scriptLink, ...extraCommonProps } = options;
2018-07-18 17:35:39 +08:00
class Custom extends React.Component<IconProps> {
render() {
const { type, className = '', spin } = this.props;
const classString = classNames({
anticon: true,
'anticon-spin': !!spin || type === 'loading',
}, className);
return (
<i className={classString}>
<svg
{...extraCommonProps}
{...omit(this.props, ['type', 'spin'])}
width={'1em'}
height={'1em'}
fill={'currentColor'}
aria-hidden={'true'}
>
2018-07-19 20:32:34 +08:00
<use xlinkHref={`#${prefix}${type}`}/>
</svg>
</i>
2018-07-18 17:35:39 +08:00
);
}
componentDidMount() {
/**
* DOM API required.
2018-07-19 20:32:34 +08:00
* Make sure in browser environment.
2018-07-18 17:35:39 +08:00
* The Custom Icon will create a <script/>
* that loads SVG symbols and insert the SVG Element into the document body.
*/
2018-07-19 20:32:34 +08:00
if (typeof document === 'object' && typeof window === 'object'
&& typeof document.createElement === 'function'
2018-07-18 17:35:39 +08:00
&& typeof scriptLink === 'string' && scriptLink.length
&& typeof namespace === 'string' && namespace.length
&& !customCache.has(namespace)
) {
const script = document.createElement('script');
script.setAttribute('src', scriptLink);
script.setAttribute('data-namespace', namespace);
customCache.add(namespace);
document.body.appendChild(script);
}
}
}
return Custom;
}
export default CustomIcon;