UNPKG

@talend/react-cmf

Version:

A framework built on top of best react libraries

118 lines (114 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.getStateAccessors = getStateAccessors; exports.getStateProps = getStateProps; exports.initState = initState; exports.statePropTypes = void 0; var _propTypes = _interopRequireDefault(require("prop-types")); var _immutable = _interopRequireDefault(require("immutable")); var _actions = _interopRequireDefault(require("./actions")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * This module provide props.setState and props.state into * cmfConnected component. It exposes CMF propTypes * @module react-cmf/lib/componentState * @see module:react-cmf/lib/cmfConnect * @example import { cmfConnect, componentState } from '@talend/react-cmf'; class MyComponent extends React.Component { static propTypes = { ...componentState.propTypes, }; render() { // ... } } export default cmfConnect({})(MyComponent); */ function getStateProps(state, name, id = 'default') { return { state: state.cmf.components.getIn([name, id]) }; } function initState(props) { if (!props.state && props.initState) { props.initState(props.initialState); } } function getAction({ name, id, operation, componentState }) { return { id, type: `${name}.${operation}`, cmf: { componentState } }; } function getStateAccessors(dispatch, name, id, DEFAULT_STATE) { const dispatchAction = (operation, componentState) => { dispatch(getAction({ id, name, componentState, operation })); }; const accessors = { setState(state) { dispatch((_, getState) => { let newState = state; if (typeof newState === 'function') { newState = state(getStateProps(getState(), name, id)); } const componentState = _actions.default.components.mergeState(name, id, newState); dispatchAction('setState', componentState); }); }, initState(initialState) { let state; if (DEFAULT_STATE) { state = DEFAULT_STATE.merge(initialState); } else if (initialState) { state = _immutable.default.Map.isMap(initialState) ? initialState : _immutable.default.fromJS(initialState); } if (state) { const componentState = _actions.default.components.addState(name, id, state); dispatchAction('initState', componentState); } }, deleteState(initialState) { if (DEFAULT_STATE || initialState) { const componentState = _actions.default.components.removeState(name, id); dispatchAction('deleteState', componentState); } } }; accessors.updateState = function updateState(state) { // eslint-disable-next-line no-console console.warn('DEPRECATION WARNING: please use props.setState'); accessors.setState(state); }; return accessors; } // DEPRECATION Warning: Please use cmfConnect.propTypes const statePropTypes = exports.statePropTypes = { state: _propTypes.default.object, initialState: _propTypes.default.object, setState: _propTypes.default.func, initState: _propTypes.default.func }; var _default = exports.default = { propTypes: statePropTypes, init: initState, getProps: getStateProps, getAccessors: getStateAccessors }; //# sourceMappingURL=componentState.js.map