UNPKG

react-delay-input

Version:

React component that renders an Input with delayed onChange

268 lines (216 loc) 9.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.DelayInput = 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _lodash = require('lodash.debounce'); var _lodash2 = _interopRequireDefault(_lodash); 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 DelayInput = exports.DelayInput = function (_React$PureComponent) { _inherits(DelayInput, _React$PureComponent); function DelayInput(props) { _classCallCheck(this, DelayInput); var _this = _possibleConstructorReturn(this, (DelayInput.__proto__ || Object.getPrototypeOf(DelayInput)).call(this, props)); _this.onChange = function (event) { event.persist(); var oldValue = _this.state.value; _this.setState({ value: event.target.value }, function () { var value = _this.state.value; if (value.length >= _this.props.minLength) { _this.notify(event); return; } // If user hits backspace and goes below minLength consider it cleaning the value if (oldValue.length > value.length) { _this.notify(_extends({}, event, { target: _extends({}, event.target, { value: '' }) })); } }); }; _this.onKeyDown = function (event) { var onKeyDown = _this.props.onKeyDown; if (event.key === 'Enter') { _this.forceNotify(event); } // Invoke original onKeyDown if present if (onKeyDown) { onKeyDown(event); } }; _this.onBlur = function (event) { var onBlur = _this.props.onBlur; _this.forceNotify(event); // Invoke original onBlur if present if (onBlur) { onBlur(event); } }; _this.doNotify = function () { var onChange = _this.props.onChange; onChange.apply(undefined, arguments); }; _this.forceNotify = function (event) { if (!_this.isDebouncing) { return; } if (_this.cancel) { _this.cancel(); } var value = _this.state.value; var minLength = _this.props.minLength; if (value.length >= minLength) { _this.doNotify(event); } else { _this.doNotify(_extends({}, event, { target: _extends({}, event.target, { value: value }) })); } }; _this.state = { value: props.value || '' }; _this.isDebouncing = false; return _this; } _createClass(DelayInput, [{ key: 'componentDidMount', value: function componentDidMount() { this.createNotifier(this.props); } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, _prevState, _snapshot) { if (this.isDebouncing) { return; } var _props = this.props, delayMaxNext = _props.delayMax, delayTimeoutNext = _props.delayTimeout, leadingNotifyNext = _props.leadingNotify, trailingNotifyNext = _props.trailingNotify, valueNext = _props.value; var delayMax = prevProps.delayMax, delayTimeout = prevProps.delayTimeout, leadingNotify = prevProps.leadingNotify, trailingNotify = prevProps.trailingNotify, value = prevProps.value; if (typeof valueNext !== 'undefined' && value !== valueNext) { this.setState({ value: valueNext }); } if (delayMax !== delayMaxNext || delayTimeout !== delayTimeoutNext || leadingNotify !== leadingNotifyNext || trailingNotify !== trailingNotifyNext) { this.createNotifier(this.props); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.flush) { this.flush(); } } }, { key: 'createNotifier', value: function createNotifier(props) { var _this2 = this; var maxWait = props.delayMax, delayTimeout = props.delayTimeout, leading = props.leadingNotify, trailing = props.trailingNotify; if (delayTimeout < 0) { this.notify = function () { return null; }; } else if (delayTimeout === 0) { this.notify = this.doNotify; } else { var debounceOpts = { leading: leading, trailing: trailing }; // DO NOT set the maxWait option if undefined; it causes debounce to // maxWait with the default delayTimeout value. if (maxWait !== undefined && maxWait !== null) { debounceOpts.maxWait = maxWait; } var debouncedChangeFunc = (0, _lodash2.default)(function (event) { _this2.isDebouncing = false; _this2.doNotify(event); }, delayTimeout, debounceOpts); this.notify = function (event) { _this2.isDebouncing = true; debouncedChangeFunc(event); }; this.flush = function () { return debouncedChangeFunc.flush(); }; this.cancel = function () { _this2.isDebouncing = false; debouncedChangeFunc.cancel(); }; } } }, { key: 'render', value: function render() { var _props2 = this.props, element = _props2.element, forceNotifyByEnter = _props2.forceNotifyByEnter, forceNotifyOnBlur = _props2.forceNotifyOnBlur, onKeyDown = _props2.onKeyDown, onBlur = _props2.onBlur, inputRef = _props2.inputRef, _delayMax = _props2.delayMax, _delayTimeout = _props2.delayTimeout, _leadingNotify = _props2.leadingNotify, _minLength = _props2.minLength, _onChange = _props2.onChange, _trailingNotify = _props2.trailingNotify, _value = _props2.value, props = _objectWithoutProperties(_props2, ['element', 'forceNotifyByEnter', 'forceNotifyOnBlur', 'onKeyDown', 'onBlur', 'inputRef', 'delayMax', 'delayTimeout', 'leadingNotify', 'minLength', 'onChange', 'trailingNotify', 'value']); var maybeOnKeyDown = void 0; if (forceNotifyByEnter) { maybeOnKeyDown = { onKeyDown: this.onKeyDown }; } else if (onKeyDown) { maybeOnKeyDown = { onKeyDown: onKeyDown }; } else { maybeOnKeyDown = {}; } var maybeOnBlur = void 0; if (forceNotifyOnBlur) { maybeOnBlur = { onBlur: this.onBlur }; } else if (onBlur) { maybeOnBlur = { onBlur: onBlur }; } else { maybeOnBlur = {}; } var maybeRef = inputRef ? { ref: inputRef } : {}; return _react2.default.createElement(element, _extends({}, props, { onChange: this.onChange, value: this.state.value }, maybeOnKeyDown, maybeOnBlur, maybeRef)); } }]); return DelayInput; }(_react2.default.PureComponent); DelayInput.defaultProps = { element: 'input', type: 'text', onKeyDown: undefined, onBlur: undefined, value: undefined, minLength: 0, delayMax: undefined, delayTimeout: 100, forceNotifyByEnter: true, forceNotifyOnBlur: true, inputRef: undefined, leadingNotify: false, trailingNotify: true };