UNPKG

redux-formo

Version:

An alternate forms framework for Redux+React.

210 lines (164 loc) 7.32 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 _react2 = _interopRequireDefault(_react); var _lodash = require('lodash.isequal'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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 Form = function (_React$Component) { _inherits(Form, _React$Component); function Form() { var _Object$getPrototypeO; _classCallCheck(this, Form); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Form)).call.apply(_Object$getPrototypeO, [this].concat(args))); _this.filter = _this.filter.bind(_this); _this.validate = _this.validate.bind(_this); _this.submit = _this.submit.bind(_this); _this.handleSubmit = _this.handleSubmit.bind(_this); return _this; } _createClass(Form, [{ key: 'getChildContext', value: function getChildContext() { return { formo: { getState: this.props.getState, name: this.props.name, filter: this.props.filterFn, validate: this.props.validateFn, defaults: this.props.defaultValues } }; } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.props.destroyOnUnmount) { this.props.destroy(); } } //FIXME: this can be removed when we move form filtering/validating/submitting to an action creator }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps) { var _this2 = this; //if there's new properties we should re-render if (Object.keys(nextProps).length !== Object.keys(this.props).length) { return true; } //check current properties haven't changed return Object.keys(nextProps).some(function (prop) { return !(0, _lodash2.default)(_this2.props[prop], nextProps[prop]); }); } }, { key: 'filter', value: function filter() { var _this3 = this; //FIXME: wrap this in an action creator return Promise.all(this.props.fields.map(function (name) { return _this3.props.filter(name); })); } }, { key: 'validate', value: function validate() { var _this4 = this; //FIXME: wrap this in an action creator return Promise.all(this.props.fields.map(function (name) { return _this4.props.validate(name); })); } }, { key: 'submit', value: function submit() { var _this5 = this; //FIXME: wrap this in an action creator return Promise.resolve().then(function () { return _this5.filter(); }).then(function () { return _this5.validate(); }).then(function () { if (_this5.props.valid) { return _this5.props.submit(); } }); } }, { key: 'handleSubmit', value: function handleSubmit(event) { if (event) { event.preventDefault(); } this.submit(); } }, { key: 'render', value: function render() { var _props = this.props; var stateKey = _props.stateKey; var name = _props.name; var destroyOnUnmount = _props.destroyOnUnmount; var filter = _props.filter; var validate = _props.validate; var submit = _props.submit; var children = _props.children; var Component = _props.component; var otherProps = _objectWithoutProperties(_props, ['stateKey', 'name', 'destroyOnUnmount', 'filter', 'validate', 'submit', 'children', 'component']); var childProps = _extends({}, otherProps, { //TODO: calculate other state or move to reducer //functions filter: this.filter, validate: this.validate, submit: this.submit, //handlers onSubmit: this.handleSubmit }); //console.log('Form.render()'); if (typeof Component === 'function') { return _react2.default.createElement(Component, childProps); } else if (children) { var child = _react2.default.Children.only(children); if (_react2.default.isValidElement(child) && typeof child.type === 'string') { return child; } else { return _react2.default.cloneElement(_react2.default.Children.only(child), childProps); } } else { throw new Error('No component/children.'); } } }]); return Form; }(_react2.default.Component); Form.childContextTypes = { formo: _react2.default.PropTypes.object.isRequired }; Form.propTypes = { getState: _react2.default.PropTypes.func.isRequired, name: _react2.default.PropTypes.string.isRequired, defaultValues: _react2.default.PropTypes.object, children: _react2.default.PropTypes.element, component: _react2.default.PropTypes.func, filter: _react2.default.PropTypes.func.isRequired, validate: _react2.default.PropTypes.func.isRequired, submit: _react2.default.PropTypes.func.isRequired, destroyOnUnmount: _react2.default.PropTypes.bool.isRequired }; Form.defaultProps = { defaultValues: {}, destroyOnUnmount: true }; exports.default = Form; //# sourceMappingURL=Form.js.map