UNPKG

svf

Version:

A simple validate form and React-based implementation

95 lines (72 loc) 4.35 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 _validator = require("../validator"); var _validator2 = _interopRequireDefault(_validator); 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 (_Component) { _inherits(Form, _Component); function Form(props) { _classCallCheck(this, Form); var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props)); _this.state = { totalErrors: {} }; _this.totalRules = {}; _this.onSubmit = _this.onSubmit.bind(_this); _this.updateRules = _this.updateRules.bind(_this); _this.updateState = _this.updateState.bind(_this); return _this; } _createClass(Form, [{ key: "updateState", value: function updateState(params) { this.setState(params); } }, { key: "updateRules", value: function updateRules(rules) { this.totalRules = _extends({}, this.totalRules, rules); } }, { key: "onSubmit", value: function onSubmit(e) { e.preventDefault(); var _state = this.state, totalErrors = _state.totalErrors, otherState = _objectWithoutProperties(_state, ["totalErrors"]); var result = _validator2.default.check(this, this.totalRules); if (result) this.props.onSubmit(otherState); } }, { key: "render", value: function render() { var _this2 = this; return _react2.default.createElement( "form", { className: "form" }, this.props.children ? _react2.default.Children.map(this.props.children, function (child) { return _react2.default.cloneElement(child, _extends({}, _this2.state, { onSubmit: _this2.onSubmit, updateRules: _this2.updateRules, updateState: _this2.updateState })); }) : "" ); } }]); return Form; }(_react.Component); Form.defaultProps = { onSubmit: function onSubmit() {} }; exports.default = Form;