UNPKG

valedictorian

Version:

Another React validation library

106 lines (78 loc) 4.33 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 _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _validatableInput = require("../validatable-input"); var _validatableInput2 = _interopRequireDefault(_validatableInput); 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 Checkbox = function (_ValidatableInput) { _inherits(Checkbox, _ValidatableInput); function Checkbox(props) { _classCallCheck(this, Checkbox); var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props)); _this.state = Object.assign({}, _this.state, { checked: _this.value === _this.props.checkedValue }); return _this; } _createClass(Checkbox, [{ key: "update", value: function update() { var context = this; return function (e) { var state = { checked: e.target.checked, value: e.target.checked ? context.props.checkedValue : context.props.uncheckedValue, changed: true }; context.setState(state, function () { // If part of a validation group, let the parent call validate if (context.context.validation) { context.context.validation.validate(); } else { context.validate(); } }); if (context.props.onChange) { context.props.onChange.apply(this, arguments); } }; } }, { key: "render", value: function render() { var props = Object.assign({}, this.props, { onChange: this.update(), ref: this.props.inputRef }); var value = this.state.checked ? this.props.checkedValue : this.props.uncheckedValue; delete props["validators"]; delete props["initial"]; delete props["type"]; delete props["checked"]; delete props["uncheckedValue"]; delete props["checkedValue"]; delete props["value"]; delete props["inputRef"]; return _react2.default.createElement("input", _extends({ type: "checkbox", checked: this.state.checked, value: this.state.value }, props)); } }]); return Checkbox; }(_validatableInput2.default); exports.default = Checkbox; Checkbox.contextTypes = { validation: _propTypes2.default.object }; Checkbox.propTypes = { checkedValue: _propTypes2.default.string.isRequired, uncheckedValue: _propTypes2.default.string.isRequired };