UNPKG

5.32 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _react = _interopRequireWildcard(require("react"));
9
10var _propTypes = _interopRequireDefault(require("prop-types"));
11
12var _lodash = _interopRequireDefault(require("lodash"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
17
18function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
19
20function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21
22function _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); } }
23
24function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
25
26function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
27
28function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
29
30function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
31
32function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
33
34function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
35
36var propTypes = {
37 children: _propTypes.default.object.isRequired,
38 debounceTimeoutMs: _propTypes.default.number,
39 debouncedOnChange: _propTypes.default.func,
40 throttleTimeoutMs: _propTypes.default.number,
41 throttledOnChange: _propTypes.default.func
42};
43
44var ThrottledTextInput =
45/*#__PURE__*/
46function (_Component) {
47 _inherits(ThrottledTextInput, _Component);
48
49 function ThrottledTextInput(props) {
50 var _this;
51
52 _classCallCheck(this, ThrottledTextInput);
53
54 _this = _possibleConstructorReturn(this, _getPrototypeOf(ThrottledTextInput).call(this, props));
55 _this.onChange = _this.onChange.bind(_assertThisInitialized(_assertThisInitialized(_this)));
56 _this.debouncedOnChange = _lodash.default.debounce(_this.debouncedOnChange.bind(_assertThisInitialized(_assertThisInitialized(_this))), props.debounceTimeoutMs);
57 _this.throttledOnChange = _lodash.default.throttle(_this.throttledOnChange.bind(_assertThisInitialized(_assertThisInitialized(_this))), props.throttleTimeoutMs);
58 return _this;
59 }
60
61 _createClass(ThrottledTextInput, [{
62 key: "debouncedOnChange",
63 value: function debouncedOnChange(value) {
64 this.props.debouncedOnChange(value);
65 }
66 }, {
67 key: "throttledOnChange",
68 value: function throttledOnChange(value) {
69 this.props.throttledOnChange(value);
70 }
71 }, {
72 key: "onChange",
73 value: function onChange(event) {
74 var value = event.target.value;
75 var _this$props = this.props,
76 debounceTimeoutMs = _this$props.debounceTimeoutMs,
77 debouncedOnChange = _this$props.debouncedOnChange,
78 throttleTimeoutMs = _this$props.throttleTimeoutMs,
79 throttledOnChange = _this$props.throttledOnChange;
80
81 if (debouncedOnChange && debounceTimeoutMs) {
82 this.debouncedOnChange(value);
83 } else if (throttledOnChange && throttleTimeoutMs) {
84 this.throttledOnChange(value);
85 }
86 }
87 }, {
88 key: "render",
89 value: function render() {
90 var children = this.props.children;
91 return _react.default.cloneElement(_react.default.Children.only(children), {
92 onChange: this.onChange
93 });
94 }
95 }]);
96
97 return ThrottledTextInput;
98}(_react.Component);
99
100ThrottledTextInput.propTypes = propTypes;
101var _default = ThrottledTextInput;
102exports.default = _default;
\No newline at end of file