Skip to content

Commit 6a999e2

Browse files
committed
fix: issue related debounce value update
Refs: #20
1 parent c50d78a commit 6a999e2

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

src/InputSpinner.js

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class InputSpinner extends Component {
103103
* @private
104104
*/
105105
_setStateMin(callback = null) {
106-
return this.setState({value: this.state.min}, callback);
106+
return this.setState({value: ""}, callback);
107107
}
108108

109109
/**
@@ -115,11 +115,60 @@ class InputSpinner extends Component {
115115
return this.setState({value: this.state.max}, callback);
116116
}
117117

118+
/**
119+
* Clear min timer
120+
* @private
121+
*/
122+
_clearMinTimer() {
123+
clearTimeout(this.maxTimer);
124+
this.maxTimer = null;
125+
}
126+
127+
/**
128+
* Clear max timer
129+
* @private
130+
*/
131+
_clearMaxTimer() {
132+
clearTimeout(this.minTimer);
133+
this.minTimer = null;
134+
}
135+
136+
/**
137+
* Clear increase timer
138+
* @private
139+
*/
140+
_clearIncreaseTimer() {
141+
clearTimeout(this.increaseTimer);
142+
this.increaseTimer = null;
143+
}
144+
145+
/**
146+
* Clear decrease timer
147+
* @private
148+
*/
149+
_clearDecreaseTimer() {
150+
clearTimeout(this.decreaseTimer);
151+
this.decreaseTimer = null;
152+
}
153+
154+
/**
155+
* Clear all timers
156+
* @private
157+
*/
158+
clearTimers() {
159+
this._clearIncreaseTimer();
160+
this._clearDecreaseTimer();
161+
this._clearMaxTimer();
162+
this._clearMinTimer();
163+
}
164+
118165
/**
119166
* On value change
120167
* @param value
121168
*/
122169
async onChange(value) {
170+
this.clearTimers();
171+
123172
let num = value;
124173
let parsedNum = value;
125174
if (isEmpty(value)) {
@@ -140,14 +189,18 @@ class InputSpinner extends Component {
140189
if (!this.minReached(num)) {
141190
if (this.maxReached(num)) {
142191
parsedNum = this.state.max;
143-
this.maxTimer = this._debounceSetMax();
192+
if (!isEmpty(value)) {
193+
this.maxTimer = this._debounceSetMax();
194+
}
144195
if (this.props.onMax) {
145196
this.props.onMax(this.state.max);
146197
}
147198
}
148199
} else {
149200
parsedNum = this.state.min;
150-
this.minTimer = this._debounceSetMin();
201+
if (!isEmpty(value)) {
202+
this.minTimer = this._debounceSetMin();
203+
}
151204
if (this.props.onMin) {
152205
this.props.onMin(this.state.min);
153206
}
@@ -163,10 +216,6 @@ class InputSpinner extends Component {
163216
}
164217
}
165218
if (!isEmpty(value)) {
166-
if (parsedNum === num) {
167-
clearTimeout(this.minTimer);
168-
clearTimeout(this.maxTimer);
169-
}
170219
this.setState({value: num});
171220
} else {
172221
this.setState({value: value});
@@ -320,17 +369,6 @@ class InputSpinner extends Component {
320369
);
321370
}
322371

323-
clearTimers() {
324-
if (this.increaseTimer) {
325-
clearTimeout(this.increaseTimer);
326-
this.increaseTimer = null;
327-
}
328-
if (this.decreaseTimer) {
329-
clearTimeout(this.decreaseTimer);
330-
this.decreaseTimer = null;
331-
}
332-
}
333-
334372
/**
335373
* Get time to wait before increase/decrease on long press
336374
* @returns {number}

src/Utils.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ export const isEmpty = (x) => {
3333
* Debounce
3434
* @param callback
3535
* @param wait
36-
* @param context
3736
* @returns {function(...[*]=): void}
3837
*/
39-
export const debounce = (callback, wait, context = null) => {
38+
export const debounce = (callback, wait) => {
4039
let timeout;
41-
return (...args) => {
42-
if (context === null) {
43-
context = this;
44-
}
40+
return function (...args) {
41+
const context = this;
4542
clearTimeout(timeout);
4643
timeout = setTimeout(() => callback.apply(context, args), wait);
44+
return timeout;
4745
};
4846
};
4947

0 commit comments

Comments
 (0)