UNPKG

uncontrollable-input

Version:

Wrapper to easilly create strong controllable/uncontrollable inputs

127 lines (97 loc) 5.49 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'); 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; } // If the param is an event, returns the value of it's target, // otherwise returns the param. var getEventValue = function getEventValue(event) { var target = void 0; if (event == null || (target = event.target) == null) { return event; } return target.nodeName.toLowerCase() === 'input' && target.type.toLowerCase() === 'checkbox' ? target.checked : target.value; }; var noop = function noop() {}; // This decorator can be used on a controlled input component to make // it able to automatically handled the uncontrolled mode. exports.default = function (options) { return function (ControlledInput) { var UncontrollableInput = function (_PureComponent) { _inherits(UncontrollableInput, _PureComponent); function UncontrollableInput(props) { _classCallCheck(this, UncontrollableInput); var _this = _possibleConstructorReturn(this, (UncontrollableInput.__proto__ || Object.getPrototypeOf(UncontrollableInput)).call(this)); var opts = typeof options === 'function' ? options(props) : options; var controlled = _this._controlled = 'value' in props; if (!controlled) { var _props$defaultValue = props.defaultValue, defaultValue = _props$defaultValue === undefined ? opts && opts.defaultValue : _props$defaultValue; _this.state = { value: defaultValue }; _this._onChange = function (event) { _this.props.onChange(event); if (event == null || !event.defaultPrevented) { _this.setState({ value: getEventValue(event) }); } }; } else if ('production' !== process.env.NODE_ENV && 'defaultValue' in props) { throw new Error(_this.constructor.name + ': controlled component should not have a default value'); } return _this; } _createClass(UncontrollableInput, [{ key: 'render', value: function render() { var props = this.props; if (!this._controlled) { props = _extends({}, props, { onChange: this._onChange, value: this.state.value }); delete props.defaultValue; } return (0, _react.createElement)(ControlledInput, props); } }, { key: 'value', get: function get() { return this._controlled ? this.props.value : this.state.value; }, set: function set(value) { if ('production' !== process.env.NODE_ENV && this._controlled) { throw new Error(this.constructor.name + ': should not set value on controlled component'); } this.setState({ value: value }); } }]); return UncontrollableInput; }(_react.PureComponent); if ('production' !== process.env.NODE_ENV) { UncontrollableInput.prototype.componentWillReceiveProps = function (newProps) { var name = this.constructor.name; var controlled = this._controlled; var newControlled = 'value' in newProps; if (!controlled) { if (newControlled) { throw new Error(name + ': uncontrolled component should not become controlled'); } } else if (!newControlled) { throw new Error(name + ': controlled component should not become uncontrolled'); } if (newProps.defaultValue !== this.props.defaultValue) { throw new Error(name + ': default value should not change'); } }; } UncontrollableInput.defaultProps = { onChange: noop }; return UncontrollableInput; }; }; //# sourceMappingURL=index.js.map