UNPKG

@talend/react-cmf

Version:

A framework built on top of best react libraries

139 lines (129 loc) 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.WaitForSettings = void 0; exports.generateDefaultViewId = generateDefaultViewId; exports.mapStateToViewProps = void 0; exports.nonMemoized = nonMemoized; exports.withoutHOC = withoutHOC; var _reactRedux = require("react-redux"); var _propTypes = _interopRequireDefault(require("prop-types")); var _lodash = require("lodash"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Internal. All stuff related to the settings handling in CMF. * @module react-cmf/lib/settings */ /** * if viewId is undefined, try to generate a meaningfull one * else return given viewId * @param {string} viewId * @param {strign} componentName * @param {string} componentId */ function generateDefaultViewId(viewId, componentName, componentId) { if (!viewId) { if (componentName && componentId) { return `${componentName}#${componentId}`; } else if (componentName) { return componentName; } } return viewId; } /** * Extract component name without HOC * @param {String} componentName Connect(CMF(Container(MyComponent))) * @return {String} MyComponent */ function withoutHOC(componentName) { const parts = componentName.split('('); return parts[parts.length - 1].replaceAll(')', ''); } /** * try to retrieve view settings for a cmfconnected component * @param {Object} state the application state * @param {*} ownProps props given to the cmfConnected component * @param {*} componentName name of the cmfConnect component * @param {*} componentId componentId, can be undefined */ function nonMemoized(state, ownProps, componentName, componentId) { let viewProps; let viewId = ownProps.view; viewId = generateDefaultViewId(viewId, componentName, componentId); if (viewId && state.cmf.settings.props[viewId]) { viewProps = state.cmf.settings.props[viewId] || {}; } else if (componentName && componentName.includes('(')) { viewId = generateDefaultViewId(null, withoutHOC(componentName), componentId); if (viewId && state.cmf.settings.props[viewId]) { viewProps = state.cmf.settings.props[viewId] || {}; } } return viewProps; } /** * return props for a given view with reference and override support * this function is memoized and the map key is computed using * `${ownProps.view}-${componentName}-${componentId}` * * @example // state.cmf.settings should look like this "views":{ "homepage": { "sidemenu": { "_ref": "SidePanel#default" }, "listview": { "_ref": "List#default", "collectionId": "streams" } } }, "ref": { "SidePanel#default": { "actions": ["menu:1", "menu:2", ...] } } //in that case you will have the following props for the homepage view { sidemenu: { "actions": ["menu:1", "menu:2", ...] }, listview: { ... } } * * @param {Object} state redux state * @param {Object} ownProps the props passed to the component. may have a view attribute * @return {Object} React props for the component injected from the settings */ const mapStateToViewProps = exports.mapStateToViewProps = (0, _lodash.memoize)(nonMemoized, (state, ownProps, componentName, componentId) => `${ownProps.view}-${componentName}-${componentId}-${state.cmf.settings.initialized}`); function PureWaitForSettings({ initialized, loading, children }) { if (!initialized) { const Component = loading; return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {}); } return children; } PureWaitForSettings.propTypes = { initialized: _propTypes.default.bool, loading: _propTypes.default.any, children: _propTypes.default.node }; PureWaitForSettings.defaultProps = { loading: () => 'loading' }; const WaitForSettings = exports.WaitForSettings = (0, _reactRedux.connect)(state => ({ initialized: state.cmf.settings.initialized }))(PureWaitForSettings); var _default = exports.default = { mapStateToViewProps }; //# sourceMappingURL=settings.js.map