choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
97 lines (75 loc) • 3.15 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = create;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _Icon = _interopRequireDefault(require("./Icon"));
var _excluded = ["type", "children"];
var customCache = new Set();
function isValidCustomScriptUrl(scriptUrl) {
return Boolean(typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl));
}
function createScriptUrlElements(scriptUrls) {
var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var currentScriptUrl = scriptUrls[index];
if (isValidCustomScriptUrl(currentScriptUrl)) {
var script = document.createElement('script');
script.setAttribute('src', currentScriptUrl);
script.setAttribute('data-namespace', currentScriptUrl);
if (scriptUrls.length > index + 1) {
script.onload = function () {
createScriptUrlElements(scriptUrls, index + 1);
};
script.onerror = function () {
createScriptUrlElements(scriptUrls, index + 1);
};
}
customCache.add(currentScriptUrl);
document.body.appendChild(script);
}
}
function create() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var scriptUrl = options.scriptUrl,
_options$extraCommonP = options.extraCommonProps,
extraCommonProps = _options$extraCommonP === void 0 ? {} : _options$extraCommonP;
/**
* 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 (scriptUrl && typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function') {
if (Array.isArray(scriptUrl)) {
// 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
createScriptUrlElements(scriptUrl.reverse());
} else {
createScriptUrlElements([scriptUrl]);
}
}
var Iconfont = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) {
var type = props.type,
children = props.children,
restProps = (0, _objectWithoutProperties2["default"])(props, _excluded); // children > type
var content = null;
if (props.type) {
content = /*#__PURE__*/_react["default"].createElement("use", {
xlinkHref: "#".concat(type)
});
}
if (children) {
content = children;
}
return /*#__PURE__*/_react["default"].createElement(_Icon["default"], (0, _extends2["default"])({}, extraCommonProps, restProps, {
ref: ref
}), content);
});
Iconfont.displayName = 'SvgIcon';
Iconfont.__C7N_ICON = true;
return Iconfont;
}
//# sourceMappingURL=IconFont.js.map