reshow-build
Version:
React super cloneElement and createElement
169 lines (159 loc) • 5.09 kB
JavaScript
;
var _interopRequireWildcard = require("reshow-runtime/helpers/interopRequireWildcard");
exports.__esModule = true;
exports.mergeRef = exports["default"] = void 0;
var _reshowConstant = require("reshow-constant");
var _array = require("array.merge");
var React = _interopRequireWildcard(require("react"));
var _jsxRuntime = require("react/jsx-runtime");
var _mergeRef = require("./mergeRef.js");
exports.mergeRef = _mergeRef.mergeRef;
// @ts-check
var isValidElement = React.isValidElement,
cloneElement = React.cloneElement,
createElement = React.createElement,
Children = React.Children,
Fragment = React.Fragment;
/**
* @typedef {React.ComponentType|React.ReactNode|Function} Component
*/
/**
* @typedef {import('./mergeRef').RefType} RefType
*/
/**
* @typedef {object} BuildProps
*/
/**
* @typedef {object} ComponentOption
* @property {Component} [altWrap] This will only be used when the original component is not a valid element.
* @property {boolean} [doCallFunction]
*/
/**
* @param {function} component
* @param {BuildProps} props
* @param {Component|Component[]} child
* @param {ComponentOption} componentOption
* @returns {React.ReactElement?}
*/
var buildFunc = function buildFunc(component, props, child, componentOption) {
// anonymous function will call directly
var _ref = componentOption || {},
altWrap = _ref.altWrap,
doCallFunction = _ref.doCallFunction;
if (_reshowConstant.FUNCTION === typeof component && (!component.name || "children" === component.name) || doCallFunction) {
try {
if (child != _reshowConstant.T_NULL) {
props.children = /** @type React.ReactElement*/child;
}
var el = component(props);
if (isValidElement(el)) {
var elKey = el.key || props.key;
return null != elKey ? _buildReact(el, {
key: elKey
}) : el;
} else {
return altWrap ? _buildReact(altWrap, props, el) : _buildReact(el, props);
}
} catch (e) {
if (e.name === _reshowConstant.TYPE_ERROR) {
return _buildReact(component, props, child);
} else {
throw e;
}
}
} else {
return _buildReact(component, props, child);
}
};
/**
* @param {Component} component
* @param {BuildProps} props
* @param {Component|Component[]} child
* @returns {React.ReactElement?}
*/
var _buildReact = function buildReact(component, props, child) {
if (props === void 0) {
props = {};
}
if (child === void 0) {
child = _reshowConstant.T_UNDEFINED;
}
if (_reshowConstant.T_NULL == component) {
return _reshowConstant.T_NULL;
}
var isValidComp = isValidElement(component);
if (isValidComp && !(0, _reshowConstant.OBJ_SIZE)(props) && null == child) {
return component;
}
var params = [component, props];
if (child != _reshowConstant.T_NULL) {
params.push(child);
}
if (!component || _reshowConstant.T_TRUE === component || _reshowConstant.STRING === typeof component && component !== /**@type string*/component.replace(/[^a-z1-6]/g, "")) {
if (null != props.children) {
throw new TypeError("String component should not have child");
} else {
return _buildReact(/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: (/**@type string*/component)
}), props);
}
} else {
return (isValidComp ? cloneElement : createElement).apply(_reshowConstant.T_NULL, params);
}
};
/**
* @param {Component|Component[]} [component]
* @param {ComponentOption} componentOption
*/
var build = function build(component, componentOption) {
if (componentOption === void 0) {
componentOption = {};
}
return (
/**
* @param {BuildProps} props
* @param {Component|Component[]} child
* @returns {React.ReactElement?}
*/
function (props, child) {
if (props === void 0) {
props = {};
}
if (child === void 0) {
child = _reshowConstant.T_UNDEFINED;
}
if (_reshowConstant.T_NULL == component) {
return _reshowConstant.T_NULL;
}
var _ref2 = componentOption || {},
altWrap = _ref2.altWrap;
if (altWrap) {
if (_reshowConstant.FUNCTION !== typeof component && !isValidElement(component)) {
child = component;
component = altWrap;
}
}
/**
* @param {Component} comp
*/
var run = function run(comp) {
props = (0, _array.removeEmpty)(props, _reshowConstant.T_TRUE);
return (isValidElement(comp) ? _buildReact : buildFunc)(/**@type any*/comp, props, child, componentOption);
};
if ((0, _reshowConstant.IS_ARRAY)(component)) {
var key = props.key;
props.key = _reshowConstant.T_UNDEFINED;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Fragment, {
children: Children.map(component.map(function (comp) {
return run(comp);
}), function (c) {
return c;
})
}, key);
} else {
return run(component);
}
}
);
};
var _default = exports["default"] = build;