UNPKG

2.77 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import React from 'react';
4import cx from 'classnames';
5import ConfigProvider from '../config-provider';
6import Icon from './index';
7
8var customCache = new Set();
9
10/** Icon.createFromIconfontCN
11 * @description 通过自定义 iconfont源来使用使用svg格式的图片
12 * @order 1
13 */
14export default function createFromIconfontCN() {
15 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16 var scriptUrl = options.scriptUrl,
17 _options$extraCommonP = options.extraCommonProps,
18 extraCommonProps = _options$extraCommonP === undefined ? {} : _options$extraCommonP;
19
20 var hasExist = customCache.has(scriptUrl);
21
22 if (document.querySelector('script[data-namespace="' + scriptUrl + '"]')) {
23 hasExist = true;
24 }
25
26 /**
27 * DOM API required.
28 * Make sure in browser environment.
29 * The Custom Icon will create a <script/>
30 * that loads SVG symbols and insert the SVG Element into the document body.
31 */
32 if (typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function' && typeof scriptUrl === 'string' && scriptUrl.length && !hasExist) {
33 var script = document.createElement('script');
34 script.setAttribute('src', scriptUrl);
35 script.setAttribute('data-namespace', scriptUrl);
36 customCache.add(scriptUrl);
37 document.body.appendChild(script);
38 }
39
40 var Iconfont = function Iconfont(props) {
41 var _cx;
42
43 var type = props.type,
44 size = props.size,
45 children = props.children,
46 className = props.className,
47 _props$prefix = props.prefix,
48 prefix = _props$prefix === undefined ? 'next-' : _props$prefix,
49 others = _objectWithoutProperties(props, ['type', 'size', 'children', 'className', 'prefix']);
50
51 // component > children > type
52
53
54 var content = null;
55 if (props.type) {
56 content = React.createElement('use', { xlinkHref: '#' + type });
57 }
58 if (children) {
59 content = children;
60 }
61
62 var classes = cx((_cx = {}, _cx[prefix + 'icon-remote'] = true, _cx), className);
63
64 return React.createElement(
65 Icon,
66 { size: size },
67 React.createElement(
68 'svg',
69 _extends({
70 className: classes,
71 focusable: false
72 }, others, extraCommonProps),
73 content
74 )
75 );
76 };
77
78 Iconfont.displayName = 'Iconfont';
79
80 return ConfigProvider.config(Iconfont);
81}
\No newline at end of file