UNPKG

@botonic/react

Version:

Build Chatbots using React

42 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deepMapWithIndex = exports.hasComplexChildren = exports.mapObjectNonBooleanValues = exports.renderComponent = exports.ConditionalWrapper = void 0; const core_1 = require("@botonic/core"); const react_1 = require("react"); const objects_1 = require("./objects"); const ConditionalWrapper = ({ condition, wrapper, children }) => condition ? wrapper(children) : children; exports.ConditionalWrapper = ConditionalWrapper; function renderComponent({ renderBrowser, renderNode }) { if ((0, core_1.isBrowser)()) return renderBrowser(); else if ((0, core_1.isNode)()) return renderNode(); throw new Error('Unexpected process type. Not recognized as browser nor node'); } exports.renderComponent = renderComponent; const mapObjectNonBooleanValues = obj => { // to avoid React SSR warnings: https://github.com/styled-components/styled-components/issues/1198#issue-262022540 return (0, objects_1.mapObject)(obj, ([key, value]) => { if (typeof value === 'boolean') return [key, Number(value)]; return [key, value]; }); }; exports.mapObjectNonBooleanValues = mapObjectNonBooleanValues; const hasChildren = element => (0, react_1.isValidElement)(element) && Boolean(element.props.children); const hasComplexChildren = element => hasChildren(element) && react_1.Children.toArray(element.props.children).reduce((response, child) => response || (0, react_1.isValidElement)(child), false); exports.hasComplexChildren = hasComplexChildren; const deepMapWithIndex = (children, deepMapFn) => { return react_1.Children.toArray(children).map((child, index) => { if ((0, react_1.isValidElement)(child) && (0, exports.hasComplexChildren)(child)) { // Clone the child that has children and map them too return deepMapFn((0, react_1.cloneElement)(child, Object.assign(Object.assign({}, child.props), { children: (0, exports.deepMapWithIndex)(child.props.children, deepMapFn), })), index, children); } return deepMapFn(child, index, children); }); }; exports.deepMapWithIndex = deepMapWithIndex; //# sourceMappingURL=react.js.map