Skip to content

Commit ae5a0f3

Browse files
committed
fix: on long press events issues
Ref: #38
1 parent e47040a commit ae5a0f3

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/InputSpinner.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,15 @@ class InputSpinner extends Component {
560560
return time < minInterval ? minInterval : time;
561561
}
562562

563+
/**
564+
* On hold increase
565+
* @param event
566+
* @returns {Promise<void>}
567+
*/
568+
async increaseHold(event) {
569+
this.increase(event, true);
570+
}
571+
563572
/**
564573
* Increase
565574
* @param event
@@ -599,20 +608,32 @@ class InputSpinner extends Component {
599608
}
600609

601610
let wait = this._getHoldChangeInterval();
602-
if (this.increaseTimer === null) {
611+
if (!isLongPress && this.increaseTimer === null) {
603612
this._startHoldTime();
604613
wait = this.props.accelerationDelay;
605-
} else {
614+
} else if (isLongPress) {
606615
this.onLongPress(num);
607616
}
608617

609-
this.increaseTimer = setTimeout(
610-
this.increase.bind(this, event, true),
611-
wait,
612-
);
618+
if (isLongPress) {
619+
this.increaseTimer = setTimeout(
620+
this.increase.bind(this, event, true),
621+
wait,
622+
);
623+
}
624+
613625
this.onChange(num, true);
614626
}
615627

628+
/**
629+
* On hold decrease
630+
* @param event
631+
* @returns {Promise<void>}
632+
*/
633+
async decreaseHold(event) {
634+
this.decrease(event, true);
635+
}
636+
616637
/**
617638
* Decrease
618639
* @param event
@@ -652,17 +673,20 @@ class InputSpinner extends Component {
652673
}
653674

654675
let wait = this._getHoldChangeInterval();
655-
if (this.decreaseTimer === null) {
676+
if (!isLongPress && this.decreaseTimer === null) {
656677
this._startHoldTime();
657678
wait = this.props.accelerationDelay;
658-
} else {
679+
} else if (isLongPress) {
659680
this.onLongPress(num);
660681
}
661682

662-
this.decreaseTimer = setTimeout(
663-
this.decrease.bind(this, event, true),
664-
wait,
665-
);
683+
if (isLongPress) {
684+
this.decreaseTimer = setTimeout(
685+
this.decrease.bind(this, event, true),
686+
wait,
687+
);
688+
}
689+
666690
this.onChange(num, true);
667691
}
668692

@@ -1101,6 +1125,8 @@ class InputSpinner extends Component {
11011125
style={buttonStyle}
11021126
onPressIn={this.decrease.bind(this)}
11031127
onPressOut={this.onPressOut.bind(this)}
1128+
onLongPress={this.decreaseHold.bind(this)}
1129+
delayLongPress={this.props.accelerationDelay}
11041130
{...this.props.leftButtonProps}>
11051131
{this._renderLeftButtonElement()}
11061132
</TouchableHighlight>
@@ -1137,6 +1163,8 @@ class InputSpinner extends Component {
11371163
style={buttonStyle}
11381164
onPressIn={this.increase.bind(this)}
11391165
onPressOut={this.onPressOut.bind(this)}
1166+
onLongPress={this.increaseHold.bind(this)}
1167+
delayLongPress={this.props.accelerationDelay}
11401168
{...this.props.rightButtonProps}>
11411169
{this._renderRightButtonElement()}
11421170
</TouchableHighlight>

src/Utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export const debounce = (callback, wait) => {
5252
*/
5353
export const isNumeric = (num) => {
5454
return (
55-
num !== "" &&
56-
num !== null &&
55+
num != null &&
5756
num !== false &&
57+
num !== "" &&
5858
!isNaN(parseFloat(num)) &&
5959
!isNaN(num - 0)
6060
);

0 commit comments

Comments
 (0)