UNPKG

fv-react-form

Version:
173 lines (129 loc) 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 _fvEvent = require("fv-event"); var _fvEvent2 = _interopRequireDefault(_fvEvent); var _Util = require("../Util"); var _Util2 = _interopRequireDefault(_Util); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); 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 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)); _this.form = context.form; _this.listeners = []; var fieldChangeListener = _fvEvent2.default.addListener('field.change', function (field) { if (field.name === _this.props.name && field.type === 'checkbox' && field.formId === _this.form.id) { _this.setState({ checked: field.checked }); } }); _this.listeners.push(fieldChangeListener); return _this; } _createClass(Field, [{ key: "componentWillUnmount", value: function componentWillUnmount() { _Util2.default.removeListeners(this.listeners); } }, { key: "updateForm", value: function updateForm(field) { this.form.handleChange(field); if (this.props.onChange) { this.props.onChange(field); } } }, { key: "updateField", value: function updateField() { var field = { name: this.props.name, type: this.props.type, formId: this.form.id }; if (field.type === 'checkbox') { field.checked = this.state.checked; } if (field.type === 'file') { field.files = this.state.files; } // Handle updating of radio fields that have the same name. // TODO: Add tests and refactor. if (field.type === 'radio') { var formValue = this.form.getData(field.name); if (this.props.defaultChecked) { field.value = this.props.value; this.updateForm(field); return; } if (formValue === undefined) { field.value = this.state.value; this.updateForm(field); return; } if (this.state.value !== '') { field.value = this.state.value; this.updateForm(field); } return; } if (field.type !== 'checkbox' && field.type !== 'radio') { field.value = this.state.value; } this.updateForm(field); } }, { key: "getField", value: function getField() { var field = { name: this.props.name, type: this.props.type, formId: this.form.id }; if (field.type === 'checkbox') { field.checked = this.state.checked; } if (field.type === 'file') { field.files = this.state.files; } // Handle updating of radio fields that have the same name. // TODO: Add tests and refactor. if (field.type === 'radio') { var formValue = this.form.getData(field.name); if (this.props.defaultChecked) { field.value = this.props.value; return field; } if (formValue === undefined) { field.value = this.state.value; return field; } if (this.state.value !== '') { field.value = this.state.value; } return field; } if (field.type !== 'checkbox' && field.type !== 'radio') { field.value = this.state.value; } return field; } }]); return Field; }(_react.Component); exports.default = Field; Field.propTypes = { onChange: _propTypes2.default.func }; Field.contextTypes = { form: _propTypes2.default.object.isRequired };