UNPKG

3.8 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.Textarea = exports.Input = exports.default = void 0;
5
6var _debounce = _interopRequireDefault(require("lodash/debounce"));
7
8var _react = _interopRequireDefault(require("react"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
13
14var DEFAULT_DELAY = function DEFAULT_DELAY(_ref) {
15 var _ref$debounceTimeout = _ref.debounceTimeout,
16 debounceTimeout = _ref$debounceTimeout === void 0 ? 250 : _ref$debounceTimeout;
17 return debounceTimeout;
18};
19
20var getEventValue = function getEventValue(event) {
21 var target, value;
22 return event != null && (target = event.target) != null && (value = target.value) !== undefined ? value : event;
23};
24
25var EMPTY_VALUE = {
26 value: undefined
27};
28
29var debounceInputDecorator = function debounceInputDecorator(delay) {
30 if (delay === void 0) {
31 delay = DEFAULT_DELAY;
32 }
33
34 return function (Input) {
35 return function (_React$Component) {
36 _inheritsLoose(DebouncedInput, _React$Component);
37
38 function DebouncedInput(props) {
39 var _this;
40
41 _this = _React$Component.call(this) || this;
42 _this.state = {};
43 _this._notify = (0, _debounce.default)(function (event) {
44 _this.props.onChange(event);
45
46 _this.setState(EMPTY_VALUE);
47 }, typeof delay === 'function' ? delay(props) : delay);
48
49 _this._onBlur = function (event) {
50 _this._notify.flush();
51
52 var onBlur = _this.props.onBlur;
53
54 if (onBlur !== undefined) {
55 onBlur(event);
56 }
57 };
58
59 _this._onChange = function (event) {
60 _this.setState({
61 value: getEventValue(event)
62 });
63
64 if (event != null && typeof event.persist === 'function') {
65 event.persist();
66 }
67
68 _this._notify(event);
69 };
70
71 _this._onKeyDown = function (event) {
72 if (event != null && event.key === 'Enter') {
73 _this._notify.flush();
74 }
75
76 var onKeyDown = _this.props.onKeyDown;
77
78 if (onKeyDown !== undefined) {
79 onKeyDown(event);
80 }
81 };
82
83 _this._wrappedInstance = null;
84
85 _this._onRef = function (ref) {
86 _this._wrappedInstance = ref;
87 };
88
89 return _this;
90 }
91
92 var _proto = DebouncedInput.prototype;
93
94 _proto.componentWillReceiveProps = function componentWillReceiveProps(_ref2) {
95 var value = _ref2.value;
96
97 if (value !== this.props.value) {
98 this._notify.cancel();
99
100 this.setState(EMPTY_VALUE);
101 }
102 };
103
104 _proto.componentWillUnmount = function componentWillUnmount() {
105 this._notify.flush();
106 };
107
108 _proto.getWrappedInstance = function getWrappedInstance() {
109 return this._wrappedInstance;
110 };
111
112 _proto.render = function render() {
113 var props = Object.assign({}, this.props, {
114 onBlur: this._onBlur,
115 onChange: this._onChange,
116 onKeyDown: this._onKeyDown,
117 ref: this._onRef
118 });
119 delete props.debounceTimeout;
120 var value = this.state.value;
121
122 if (value !== undefined) {
123 props.value = value;
124 }
125
126 return _react.default.createElement(Input, props);
127 };
128
129 return DebouncedInput;
130 }(_react.default.Component);
131 };
132};
133
134exports.default = debounceInputDecorator;
135var Input = debounceInputDecorator()('input');
136exports.Input = Input;
137var Textarea = debounceInputDecorator()('textarea');
138exports.Textarea = Textarea;
139//# sourceMappingURL=index.js.map
\No newline at end of file