Skip to content

Commit f052c1d

Browse files
committed
fix: simplified get hold interval method
1 parent eec689a commit f052c1d

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/InputSpinner.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {debounce, isNumeric, isEmpty} from "./Utils";
77
/**
88
* Default constants
99
*/
10-
const defaultSpeed = 2.5;
10+
const defaultSpeed = 7;
1111
const defaultAccelerationDelay = 1000;
1212
const defaultColor = "#3E525F";
1313
const defaultTypingTime = 500;
@@ -176,15 +176,17 @@ class InputSpinner extends Component {
176176
* @param value
177177
*/
178178
async onChange(value) {
179+
const isEmptyValue = isEmpty(value);
179180
this._clearOnChangeTimers();
180181

181182
let num = value;
182183
let parsedNum = value;
183-
if (isEmpty(value)) {
184+
if (isEmptyValue) {
184185
num = this.state.min;
185186
}
187+
186188
if (this.props.disabled) return;
187-
const current_value = this.state.value;
189+
188190
const separator = !isEmpty(this.props.decimalSeparator)
189191
? this.props.decimalSeparator
190192
: ".";
@@ -199,7 +201,7 @@ class InputSpinner extends Component {
199201
if (this.maxReached(num)) {
200202
if (this.maxReached(num)) {
201203
parsedNum = this.state.max;
202-
if (!isEmpty(value)) {
204+
if (!isEmptyValue) {
203205
this.maxTimer = this._debounceSetMax();
204206
}
205207
if (this.props.onMax) {
@@ -208,34 +210,37 @@ class InputSpinner extends Component {
208210
}
209211
}
210212
} else {
211-
if (!isEmpty(value) && !this.isEmptied()) {
212-
parsedNum = this.state.min;
213+
if (!isEmptyValue) {
213214
this.minTimer = this._debounceSetMin();
214-
} else if (this.isEmptied()) {
215-
parsedNum = null;
215+
}
216+
217+
if (isEmptyValue && this.isEmptied()) {
218+
num = parsedNum = null;
216219
} else {
217220
parsedNum = this.state.min;
218221
}
222+
219223
if (this.props.onMin) {
220224
this.props.onMin(parsedNum);
221225
}
222226
}
223227

224-
if (isEmpty(value) && this.isEmptied()) {
225-
parsedNum = value;
228+
if (isEmptyValue && this.isEmptied()) {
229+
num = parsedNum = null;
226230
}
227231

228-
if (current_value !== num && this.props.onChange) {
232+
if (this.state.value !== num && this.props.onChange) {
229233
const res = await this.props.onChange(parsedNum);
230-
if (!isEmpty(value)) {
234+
if (!isEmptyValue) {
231235
if (res === false) {
232236
return;
233237
} else if (isNumeric(res)) {
234238
num = this.parseNum(res);
235239
}
236240
}
237241
}
238-
if (!isEmpty(value)) {
242+
243+
if (!isEmptyValue) {
239244
this.setState({value: num});
240245
} else {
241246
this.setState({value: value});
@@ -435,22 +440,9 @@ class InputSpinner extends Component {
435440
* @private
436441
*/
437442
_getHoldChangeInterval() {
438-
const MIN_HOLD_CHANGE_INTERVAL = 10; // the minimum time interval between increases or decreases in value when holding a button
439-
const HOLD_CHANGE_ACCELERATION_FACTOR = this.props.acceleration; // a factor that controls how fast the speed of the change in value will accelerate while a button is held
440-
const HOLD_CHANGE_ACCELERATION_DELAY = this.props.accelerationDelay; // how long to wait after a button is being held before the acceleration of it speed going through values starts
441-
const TIME_HOLDING_BUTTON_FOR_MIN_CHANGE_INTERVAL =
442-
(Math.exp(MIN_HOLD_CHANGE_INTERVAL / -1) +
443-
HOLD_CHANGE_ACCELERATION_DELAY) /
444-
HOLD_CHANGE_ACCELERATION_FACTOR;
445-
const HOLD_TIME = this._getHoldTime(); // time on hold in ms
446-
447-
return HOLD_TIME >= TIME_HOLDING_BUTTON_FOR_MIN_CHANGE_INTERVAL
448-
? MIN_HOLD_CHANGE_INTERVAL // if the button has been held for long enough, return the minimum interval
449-
: -1 *
450-
Math.log(
451-
HOLD_CHANGE_ACCELERATION_FACTOR * HOLD_TIME -
452-
HOLD_CHANGE_ACCELERATION_DELAY,
453-
); // otherwise calculate with a logarithm => an exponential increase in speed
443+
const minInterval = 10;
444+
var time = (10 - Math.log(this.props.speed * this._getHoldTime())) * 100;
445+
return time < minInterval ? minInterval : time;
454446
}
455447

456448
/**

0 commit comments

Comments
 (0)