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.

57 lines
1.5 KiB

import * as React from 'react';
import Icon, { IconProps } from './index';
const customCache = new Set<string>();
export interface CustomIconOptions {
scriptUrl?: string;
extraCommonProps?: { [key: string]: any };
}
6 years ago
export default function create(options: CustomIconOptions = {}): React.SFC<IconProps> {
const { scriptUrl, extraCommonProps = {} } = options;
6 years ago
/**
* 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.
*/
6 years ago
if (
typeof document !== 'undefined' &&
typeof window !== 'undefined' &&
typeof document.createElement === 'function' &&
typeof scriptUrl === 'string' &&
scriptUrl.length &&
!customCache.has(scriptUrl)
6 years ago
) {
const script = document.createElement('script');
script.setAttribute('src', scriptUrl);
script.setAttribute('data-namespace', scriptUrl);
customCache.add(scriptUrl);
6 years ago
document.body.appendChild(script);
}
6 years ago
const Iconfont: React.SFC<IconProps> = props => {
const { type, children, ...restProps } = props;
6 years ago
// component > children > type
let content = null;
if (props.type) {
content = <use xlinkHref={`#${type}`} />;
}
if (children) {
content = children;
}
6 years ago
return (
6 years ago
<Icon {...restProps} {...extraCommonProps}>
6 years ago
{content}
</Icon>
);
};
Iconfont.displayName = 'Iconfont';
return Iconfont;
}