UNPKG

redux-form

Version:

A higher order component decorator for forms using Redux and React

153 lines (125 loc) 5.91 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _ConnectedField = require('./ConnectedField'); var _ConnectedField2 = _interopRequireDefault(_ConnectedField); var _shallowCompare = require('./util/shallowCompare'); var _shallowCompare2 = _interopRequireDefault(_shallowCompare); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var createField = function createField(_ref) { var deepEqual = _ref.deepEqual; var getIn = _ref.getIn; var setIn = _ref.setIn; var ConnectedField = (0, _ConnectedField2.default)({ deepEqual: deepEqual, getIn: getIn }); var Field = function (_Component) { _inherits(Field, _Component); function Field(props, context) { _classCallCheck(this, Field); var _this = _possibleConstructorReturn(this, (Field.__proto__ || Object.getPrototypeOf(Field)).call(this, props, context)); if (!context._reduxForm) { throw new Error('Field must be inside a component decorated with reduxForm()'); } _this.normalize = _this.normalize.bind(_this); return _this; } _createClass(Field, [{ key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps, nextState) { return (0, _shallowCompare2.default)(this, nextProps, nextState); } }, { key: 'componentWillMount', value: function componentWillMount() { this.context._reduxForm.register(this.name, 'Field'); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (this.props.name !== nextProps.name) { // unregister old name this.context._reduxForm.unregister(this.props.name); // register new name this.context._reduxForm.register(nextProps.name, 'Field'); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.context._reduxForm.unregister(this.name); } }, { key: 'getRenderedComponent', value: function getRenderedComponent() { (0, _invariant2.default)(this.props.withRef, 'If you want to access getRenderedComponent(), ' + 'you must specify a withRef prop to Field'); return this.refs.connected.getWrappedInstance().getRenderedComponent(); } }, { key: 'normalize', value: function normalize(name, value) { var normalize = this.props.normalize; if (!normalize) { return value; } var previousValues = this.context._reduxForm.getValues(); var previousValue = this.value; var nextValues = setIn(previousValues, name, value); return normalize(value, previousValue, nextValues, previousValues); } }, { key: 'render', value: function render() { return (0, _react.createElement)(ConnectedField, _extends({}, this.props, { normalize: this.normalize, _reduxForm: this.context._reduxForm, ref: 'connected' })); } }, { key: 'name', get: function get() { return this.props.name; } }, { key: 'dirty', get: function get() { return !this.pristine; } }, { key: 'pristine', get: function get() { return this.refs.connected.getWrappedInstance().isPristine(); } }, { key: 'value', get: function get() { return this.refs.connected && this.refs.connected.getWrappedInstance().getValue(); } }]); return Field; }(_react.Component); Field.propTypes = { name: _react.PropTypes.string.isRequired, component: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string]).isRequired, format: _react.PropTypes.func, normalize: _react.PropTypes.func, parse: _react.PropTypes.func, props: _react.PropTypes.object }; Field.contextTypes = { _reduxForm: _react.PropTypes.object }; return Field; }; exports.default = createField;