all files / src/ createReduxFormConnector.js

100% Statements 63/63
100% Branches 27/27
100% Functions 12/12
100% Lines 19/19
4 statements, 10 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62           54×     54×   54×                                   56× 56×       56×     54× 54× 54×                 54×   158×   54×        
import LazyCache from 'react-lazy-cache/noGetters';
import getDisplayName from './getDisplayName';
import createHigherOrderComponent from './createHigherOrderComponent';
 
/**
 * This component tracks props that affect how the form is mounted to the store. Normally these should not change,
 * but if they do, the connected components below it need to be redefined.
 */
const createReduxFormConnector =
  (isReactNative, React, connect) =>
    (WrappedComponent, mapStateToProps, mapDispatchToProps, mergeProps, options) => {
      const {Component, PropTypes} = React;
      class ReduxFormConnector extends Component {
        constructor(props) {
          super(props);
          this.cache = new LazyCache(this, {
            ReduxForm: {
              params: [
                // props that effect how redux-form connects to the redux store
                'reduxMountPoint',
                'form',
                'formKey',
                'getFormState'
              ],
              fn: createHigherOrderComponent(props, isReactNative, React, connect, WrappedComponent,
                mapStateToProps, mapDispatchToProps, mergeProps, options)
            }
          });
        }
 
        componentWillReceiveProps(nextProps) {
          this.cache.componentWillReceiveProps(nextProps);
        }
 
        render() {
          const ReduxForm = this.cache.get('ReduxForm');
          // remove some redux-form config-only props
          const {reduxMountPoint, destroyOnUnmount, form, getFormState, touchOnBlur, touchOnChange,
            ...passableProps } = this.props; // eslint-disable-line no-redeclare
          return <ReduxForm {...passableProps}/>;
        }
      }
      ReduxFormConnector.displayName = `ReduxFormConnector(${getDisplayName(WrappedComponent)})`;
      ReduxFormConnector.WrappedComponent = WrappedComponent;
      ReduxFormConnector.propTypes = {
        destroyOnUnmount: PropTypes.bool,
        reduxMountPoint: PropTypes.string,
        form: PropTypes.string.isRequired,
        formKey: PropTypes.string,
        getFormState: PropTypes.func,
        touchOnBlur: PropTypes.bool,
        touchOnChange: PropTypes.bool
      };
      ReduxFormConnector.defaultProps = {
        reduxMountPoint: 'form',
        getFormState: (state, reduxMountPoint) => state[reduxMountPoint]
      };
      return ReduxFormConnector;
    };
 
export default createReduxFormConnector;