all files / src/ createReduxForm.js

99.04% Statements 103/104
98% Branches 49/50
95.65% Functions 22/23
93.75% Lines 15/16
19 statements, 2 functions, 21 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                                                                                                                             
import React, {Component, PropTypes} from 'react'
import hoistStatics from 'hoist-non-react-statics'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as importedActions from './actions'
import bindActionData from './bindActionData'
 
const getDisplayName = Comp => Comp.displayName || Comp.name || 'Component'
 
/**
 * The decorator that is the main API to redux-form
 */
const createReduxForm =
  structure =>
    initialConfig => {
      const config = {
        touchOnBlur: true,
        touchOnChange: false,
        destroyOnUnmount: true,
        getFormState: state => structure.getIn(state, 'form'),
        ...initialConfig
      }
      return WrappedComponent => {
        class ReduxForm extends Component {
          constructor(props) {
            super(props)
          }
 
          getChildContext() {
            return {
              _reduxForm: {
                ...this.props,
                getFormState: state => structure.getIn(this.props.getFormState(state), this.props.form),
                submitPassback: submit => this.submit = submit
              }
            }
          }
 
          submit() {
            throw new Error('You must include at least one submit button in your form')
          }
 
          render() {
            // remove some redux-form config-only props
            const {reduxMountPoint, destroyOnUnmount, form, getFormState, touchOnBlur, touchOnChange,
              ...passableProps } = this.props // eslint-disable-line no-redeclare
            return <WrappedComponent {...passableProps}/>
          }
        }
        ReduxForm.displayName = `ReduxForm(${getDisplayName(WrappedComponent)})`
        ReduxForm.WrappedComponent = WrappedComponent
        ReduxForm.childContextTypes = {
          _reduxForm: PropTypes.object.isRequired
        }
        ReduxForm.propTypes = {
          destroyOnUnmount: PropTypes.bool,
          form: PropTypes.string.isRequired,
          getFormState: PropTypes.func,
          touchOnBlur: PropTypes.bool,
          touchOnChange: PropTypes.bool
        }
        ReduxForm.defaultProps = config
 
        const connector = connect(
          undefined, // don't subscribe to ANYTHING!
          (dispatch, ownProps) =>
            bindActionCreators(
              bindActionData({
                  ...importedActions,
                  blur: bindActionData(importedActions.blur, {
                    touch: !!ownProps.touchOnBlur
                  }),
                  change: bindActionData(importedActions.change, {
                    touch: !!ownProps.touchOnChange
                  })
                },
                { form: ownProps.form }),
              dispatch)
        )
        return hoistStatics(connector(ReduxForm), WrappedComponent)
      }
    }
 
export default createReduxForm