UNPKG

@ima/react-page-renderer

Version:

IMA.js React page renderer.

72 lines (71 loc) 2.49 kB
import memoizeOne from 'memoize-one'; import { Component, createElement } from 'react'; import { ErrorBoundary } from './ErrorBoundary'; import { PageContext } from '../PageContext'; /** * An adapter component providing the current page controller's state to the * page view component through its properties. */ export class ViewAdapter extends 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 = memoizeOne((props, state)=>this.getContextValue(props, state)); /** * The function for creating context. */ this.createContext = memoizeOne(($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__*/ createElement(PageContext.Provider, { value: this._getContextValue(this.props, this.state) }, /*#__PURE__*/ 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__*/ createElement(ErrorBoundary, null, viewElement) : viewElement; } } //# sourceMappingURL=ViewAdapter.js.map