UNPKG

@ima/react-page-renderer

Version:

IMA.js React page renderer.

84 lines (83 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "ViewAdapter", { enumerable: true, get: function() { return ViewAdapter; } }); const _memoizeone = /*#__PURE__*/ _interop_require_default(require("memoize-one")); const _react = require("react"); const _ErrorBoundary = require("./ErrorBoundary"); const _PageContext = require("../PageContext"); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class ViewAdapter extends _react.Component { _managedRootView; _getContextValue; createContext; contextSelectors = [ (props)=>props.$Utils ]; /** * Initializes the adapter component. * * @param props Component properties, containing the actual page view * and the initial page state to pass to the view. */ constructor(props){ super(props); this.state = {}; /** * The actual page view to render. */ this._managedRootView = props.managedRootView; /** * The memoized context value. */ this._getContextValue = (0, _memoizeone.default)((props, state)=>this.getContextValue(props, state)); /** * The function for creating context. */ this.createContext = (0, _memoizeone.default)(($Utils, ...values)=>{ return { $Utils, ...values }; }); } static getDerivedStateFromProps(props, state) { if (!state) { return props.state; } return { ...Object.keys(state).reduce((acc, cur)=>{ acc[cur] = undefined; return acc; }, {}), ...props.state }; } getContextValue(props, state) { return this.createContext(// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-return ...this.contextSelectors.map((selector)=>selector(props, state))); } /** * @inheritDoc */ render() { const viewElement = /*#__PURE__*/ (0, _react.createElement)(_PageContext.PageContext.Provider, { value: this._getContextValue(this.props, this.state) }, /*#__PURE__*/ (0, _react.createElement)(this._managedRootView, Object.assign({}, this.state, { pageView: this.props.pageView, ref: (element)=>{ if (element && this.props.refCallback) { this.props.refCallback(); } } }))); // Wrap view with ErrorBoundary in $Debug env return $Debug ? /*#__PURE__*/ (0, _react.createElement)(_ErrorBoundary.ErrorBoundary, null, viewElement) : viewElement; } } //# sourceMappingURL=ViewAdapter.js.map