UNPKG

@talend/react-cmf

Version:

A framework built on top of best react libraries

128 lines (119 loc) 3.57 kB
/** * Internal. All stuff related to the settings handling in CMF. * @module react-cmf/lib/settings */ import { connect } from 'react-redux'; import PropTypes from 'prop-types'; /** * if viewId is undefined, try to generate a meaningfull one * else return given viewId * @param {string} viewId * @param {strign} componentName * @param {string} componentId */ import { memoize } from "lodash"; import { jsx as _jsx } from "react/jsx-runtime"; export 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 */ export 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 */ export 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 */ export const mapStateToViewProps = 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__*/_jsx(Component, {}); } return children; } PureWaitForSettings.propTypes = { initialized: PropTypes.bool, loading: PropTypes.any, children: PropTypes.node }; PureWaitForSettings.defaultProps = { loading: () => 'loading' }; export const WaitForSettings = connect(state => ({ initialized: state.cmf.settings.initialized }))(PureWaitForSettings); export default { mapStateToViewProps }; //# sourceMappingURL=settings.js.map