ant-design/components/icon/IconFont.tsx
Amumu e150cfc371 fix: extraCommonProps not work properly (#19572)
* fix extraCommonProps not work properly

* add test case
2019-11-05 18:32:57 +08:00

57 lines
1.5 KiB
TypeScript

import * as React from 'react';
import Icon, { IconProps } from './index';
const customCache = new Set<string>();
export interface CustomIconOptions {
scriptUrl?: string;
extraCommonProps?: { [key: string]: any };
}
export default function create(options: CustomIconOptions = {}): React.SFC<IconProps> {
const { scriptUrl, extraCommonProps = {} } = options;
/**
* DOM API required.
* Make sure in browser environment.
* The Custom Icon will create a <script/>
* that loads SVG symbols and insert the SVG Element into the document body.
*/
if (
typeof document !== 'undefined' &&
typeof window !== 'undefined' &&
typeof document.createElement === 'function' &&
typeof scriptUrl === 'string' &&
scriptUrl.length &&
!customCache.has(scriptUrl)
) {
const script = document.createElement('script');
script.setAttribute('src', scriptUrl);
script.setAttribute('data-namespace', scriptUrl);
customCache.add(scriptUrl);
document.body.appendChild(script);
}
const Iconfont: React.SFC<IconProps> = props => {
const { type, children, ...restProps } = props;
// component > children > type
let content = null;
if (props.type) {
content = <use xlinkHref={`#${type}`} />;
}
if (children) {
content = children;
}
return (
<Icon {...extraCommonProps} {...restProps}>
{content}
</Icon>
);
};
Iconfont.displayName = 'Iconfont';
return Iconfont;
}