@deskpro/react-forms
Version:
Forms library for React
156 lines (130 loc) • 6.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
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 _class, _temp; /**
* @copyright 2015, Prometheus Research, LLC
*/
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _debounce = require('lodash/debounce');
var _debounce2 = _interopRequireDefault(_debounce);
var _noop = require('lodash/noop');
var _noop2 = _interopRequireDefault(_noop);
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; }
/**
* Input component with debounce.
*/
var Input = (_temp = _class = function (_React$Component) {
_inherits(Input, _React$Component);
function Input(props) {
_classCallCheck(this, Input);
var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props));
_this.onChange = function (e) {
var value = void 0;
if (e && e.target && 'value' in e.target) {
value = e.target.value;
if (value === '') {
value = null;
}
} else {
value = e;
}
_this._scheduleOnChange(value);
};
_this.onBlur = function (e) {
_this.props.onBlur(e);
if (_this._expectedValue !== undefined) {
_this._finalizeOnChange();
_this._cancelOnChange();
}
};
_this.state = { value: props.value };
_this._expectedValue = undefined;
_this._finalizeOnChangeDebounced = props.debounce ? (0, _debounce2.default)(_this._finalizeOnChange.bind(_this), props.debounce) : _this._finalizeOnChange.bind(_this);
return _this;
}
_createClass(Input, [{
key: 'render',
value: function render() {
var _props = this.props,
Component = _props.Component,
debounceEnabled = _props.debounce,
value = _props.value,
props = _objectWithoutProperties(_props, ['Component', 'debounce', 'value']);
if (debounceEnabled) {
value = this.state.value;
}
return _react2.default.createElement(Component, _extends({}, props, {
value: value,
onChange: this.onChange,
onBlur: this.onBlur }));
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.value !== this._expectedValue) {
if (nextProps.value !== this.props.value) {
this._cancelOnChange();
this.setState({ value: nextProps.value });
}
}
if (nextProps.debounce !== this.props.debounce) {
this._finalizeOnChange();
this._cancelOnChange();
this._finalizeOnChangeDebounced = nextProps.debounce ? (0, _debounce2.default)(this._finalizeOnChange.bind(this), nextProps.debounce) : this._finalizeOnChange.bind(this);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this._finalizeOnChange();
this._cancelOnChange();
}
}, {
key: '_scheduleOnChange',
value: function _scheduleOnChange(value) {
this.setState({ value: value });
this._expectedValue = value;
this._finalizeOnChangeDebounced();
}
}, {
key: '_finalizeOnChange',
value: function _finalizeOnChange() {
if (this._expectedValue !== undefined) {
var value = this._expectedValue;
this._expectedValue = undefined;
this.props.onChange(value);
}
}
}, {
key: '_cancelOnChange',
value: function _cancelOnChange() {
if (this._finalizeOnChangeDebounced.cancel) {
this._expectedValue = undefined;
this._finalizeOnChangeDebounced.cancel();
}
}
}]);
return Input;
}(_react2.default.Component), _class.propTypes = {
Component: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]),
debounce: _propTypes2.default.number,
value: _propTypes2.default.any,
onChange: _propTypes2.default.func
}, _class.defaultProps = {
Component: 'input',
debounce: 100,
onChange: _noop2.default,
onBlur: _noop2.default
}, _temp);
exports.default = Input;