UNPKG

@bloomreach/react-sdk

Version:

Bloomreach SPA SDK for React

238 lines (215 loc) 9.08 kB
import React from "react"; import { TYPE_CONTAINER_ITEM_UNDEFINED, TYPE_CONTAINER_UNORDERED_LIST, TYPE_CONTAINER_ORDERED_LIST, TYPE_CONTAINER_NO_MARKUP, TYPE_CONTAINER_INLINE, isContainer, isContainerItem, initialize } from "@bloomreach/spa-sdk"; function BrContainerBox({page, children}) { return React.createElement("div", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container" : undefined }, React.Children.map(children, (child => React.createElement("div", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container-item" : undefined }, child)))); } function BrContainerInline(props) { const {page, children} = props; return React.createElement("div", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container" : undefined }, React.Children.map(children, (child => React.createElement("span", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container-item" : undefined }, child)))); } function BrContainerNoMarkup({children}) { return React.createElement(React.Fragment, null, children); } function BrContainerOrderedList({page, children}) { return React.createElement("ol", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container" : undefined }, React.Children.map(children, (child => React.createElement("li", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container-item" : undefined }, child)))); } function BrContainerUnorderedList({page, children}) { return React.createElement("ul", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container" : undefined }, React.Children.map(children, (child => React.createElement("li", { className: (page === null || page === void 0 ? void 0 : page.isPreview()) ? "hst-container-item" : undefined }, child)))); } function BrContainerItemUndefined({component}) { return React.createElement("div", null, `Component "${component === null || component === void 0 ? void 0 : component.getType()}" is not defined.`); } /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P((function(resolve) { resolve(value); })); } return new (P || (P = Promise))((function(resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); })); } typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; /** * Node component for rendering brXM components with prop-based mapping resolution. */ function BrNodeComponentServer(props) { const {component, mapping, children} = props; const componentName = component === null || component === void 0 ? void 0 : component.getName(); const resolvedMapping = component && componentName && mapping[componentName]; const content = resolvedMapping ? React.createElement(resolvedMapping, Object.assign(Object.assign({}, props), { isClientComponent: false })) : children; return content; } /** * Node container item component for rendering brXM container items with prop-based mapping. */ function BrNodeContainerItemServer(props) { const {component, mapping} = props; const getMapping = () => { var _a; const type = component === null || component === void 0 ? void 0 : component.getType(); if (type && type in mapping) { return mapping[type]; } return (_a = mapping[TYPE_CONTAINER_ITEM_UNDEFINED]) !== null && _a !== void 0 ? _a : BrContainerItemUndefined; }; return React.createElement(getMapping(), Object.assign(Object.assign({}, props), { isClientComponent: false })); } /** * Node container component for rendering brXM container components with prop-based mapping. */ function BrNodeContainerServer(props) { const {component, mapping} = props; const getMapping = () => { const type = component.getType(); if (type && type in mapping) { return mapping[type]; } switch (type) { case TYPE_CONTAINER_INLINE: return BrContainerInline; case TYPE_CONTAINER_NO_MARKUP: return BrContainerNoMarkup; case TYPE_CONTAINER_ORDERED_LIST: return BrContainerOrderedList; case TYPE_CONTAINER_UNORDERED_LIST: return BrContainerUnorderedList; default: return BrContainerBox; } }; return React.createElement(getMapping(), Object.assign(Object.assign({}, props), { isClientComponent: false })); } /** * Core node component for rendering brXM components with prop drilling. */ function BrNodeServer({children, component, page, mapping}) { if (React.Children.count(children)) { return React.createElement(React.Fragment, null, children); } const props = { page, mapping }; const childrenList = component.getChildren().map((child => React.createElement(BrNodeServer, Object.assign({ key: child.getId(), component: child }, props)))); if (isContainer(component)) { return React.createElement(BrNodeContainerServer, Object.assign({ component }, props), childrenList); } if (isContainerItem(component)) { return React.createElement(BrNodeContainerItemServer, Object.assign({ component }, props), childrenList); } return React.createElement(BrNodeComponentServer, Object.assign({ component }, props), childrenList); } /** * The brXM component with prop-based data access. */ function BrComponentServer({path, children, component, page, mapping}) { function getComponents() { if (!path) { return component.getChildren(); } const targetComponent = component.getComponent(...path.split("/")); return targetComponent ? [ targetComponent ] : []; } function renderComponents() { return getComponents().map((comp => React.createElement(BrNodeServer, { key: comp.getId(), component: comp, page, mapping }, typeof children === "function" ? children({ page, component: comp, mapping, isClientComponent: false }) : children))); } return React.createElement(React.Fragment, null, renderComponents()); } /** * The brXM page component with React Server Components (RSC) support. */ function BrPageServer(props) { return __awaiter(this, void 0, void 0, (function*() { const {configuration, mapping, page: initialPage, children} = props; let page; if (initialPage) { page = initialize(configuration, initialPage); } else if (!configuration.NBRMode) { page = yield initialize(configuration); } if (!page) { return null; } const component = page.getComponent(); const renderedChildren = typeof children === "function" ? children({ page, component, mapping, isClientComponent: false }) : children; return React.createElement(BrNodeServer, { page, mapping, component }, renderedChildren); })); } export { BrComponentServer, BrPageServer }; //# sourceMappingURL=index.server.js.map