UNPKG

react-jsonschema

Version:
175 lines (144 loc) 5.6 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 _react2 = _interopRequireDefault(_react); var _lodash = require('lodash'); var _SchemaField = require('./SchemaField'); var _SchemaField2 = _interopRequireDefault(_SchemaField); var _utils = require('../utils'); var _reducers = require('../reducers'); 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 Form = function (_Component) { _inherits(Form, _Component); function Form(props) { _classCallCheck(this, Form); var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props)); _this.state = {}; _this.handleAddItem = _this.handleAddItem.bind(_this); _this.handleDeleteItem = _this.handleDeleteItem.bind(_this); _this.handleFieldChange = _this.handleFieldChange.bind(_this); _this.handleSubmit = _this.handleSubmit.bind(_this); return _this; } _createClass(Form, [{ key: 'componentWillMount', value: function componentWillMount() { // Set the initial state this.setState({ formData: (0, _utils.getDefaultState)(this.props.schema, this.props.formData) }); } /** * Adds a new item * * @param {Object} e * @param {String} e.path * @param {Object} e.schema */ }, { key: 'handleAddItem', value: function handleAddItem(e) { var newState = (0, _reducers.addValueToState)(this.state, { path: e.path, schema: e.schema }); this.setState(newState); } /** * Deletes an existing item * * @param {Object} e * @param {Number} e.index * @param {String} e.path */ }, { key: 'handleDeleteItem', value: function handleDeleteItem(e) { var newState = (0, _reducers.deleteIndexFromState)(this.state, { index: e.index, path: e.path }); this.setState(newState); } /** * Handles a field change * * @param {Object} e * @param {String} e.path * @param {String|Number|Boolean} e.value */ }, { key: 'handleFieldChange', value: function handleFieldChange(e) { var newState = (0, _lodash.cloneDeep)(this.state); (0, _lodash.set)(newState, e.path, e.value); this.setState(newState); } /** * Checks required fields and executes the onSubmit callback function * * @param {Event} e */ }, { key: 'handleSubmit', value: function handleSubmit(e) { var _this2 = this; e.preventDefault(); var errors = []; // check required fields if (this.props.schema.required && this.props.schema.required.length) { this.props.schema.required.forEach(function (key) { // if the required fields are empty if (!_this2.state.formData[key]) { errors.push(key); } }); } if (errors.length) { // execute the onError callback if (this.props.onError) { this.props.onError.call(this, errors); } } // execute the onSubmit callback if (this.props.onSubmit) { this.props.onSubmit.call(this, e); } } }, { key: 'render', value: function render() { var _props = this.props; var children = _props.children; var schema = _props.schema; var formData = this.state.formData; return _react2.default.createElement( 'form', { onSubmit: this.handleSubmit }, _react2.default.createElement(_SchemaField2.default, { schema: schema, formData: formData, onChange: this.handleFieldChange, onAddItem: this.handleAddItem, onDeleteItem: this.handleDeleteItem }), children ); } }]); return Form; }(_react.Component); Form.propTypes = { schema: _react.PropTypes.object, formData: _react.PropTypes.object, onError: _react.PropTypes.func, onSubmit: _react.PropTypes.func, children: _react.PropTypes.any }; exports.default = Form;