Skip to content

Commit 47f2f87

Browse files
author
Marco Cesarato
committed
feat: add continuity mode
#36
1 parent d0433a6 commit 47f2f87

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/InputSpinner.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ class InputSpinner extends Component {
177177
/**
178178
* On value change
179179
* @param value
180+
* @param isPressEvent
180181
*/
181-
async onChange(value) {
182+
async onChange(value, isPressEvent = false) {
182183
const isEmptyValue = isEmpty(value);
183184
this._clearOnChangeTimers();
184185

@@ -455,7 +456,15 @@ class InputSpinner extends Component {
455456
if (this._isDisabledButtonRight()) return;
456457
let currentValue = this.parseNum(this.state.value);
457458
let num = currentValue + this.parseNum(this.state.step);
458-
if (this.maxReached(currentValue)) {
459+
460+
if (
461+
this.maxReached(currentValue) &&
462+
!this.isEmptied() &&
463+
this.props.continuity !== false
464+
) {
465+
// Continuity mode
466+
num = this.state.min;
467+
} else if (this.maxReached(currentValue)) {
459468
return;
460469
}
461470
if (this.props.onIncrease) {
@@ -479,7 +488,7 @@ class InputSpinner extends Component {
479488
}
480489

481490
this.increaseTimer = setTimeout(this.increase.bind(this), wait);
482-
this.onChange(num);
491+
this.onChange(num, true);
483492
}
484493

485494
/**
@@ -489,9 +498,18 @@ class InputSpinner extends Component {
489498
if (this._isDisabledButtonLeft()) return;
490499
let currentValue = this.parseNum(this.state.value);
491500
let num = currentValue - this.parseNum(this.state.step);
492-
if (this.minReached(currentValue)) {
501+
502+
if (
503+
this.minReached(currentValue) &&
504+
!this.isEmptied() &&
505+
this.props.continuity !== false
506+
) {
507+
// Continuity mode
508+
num = this.state.max;
509+
} else if (this.minReached(currentValue)) {
493510
return;
494511
}
512+
495513
if (this.props.onDecrease) {
496514
let decreased_num = num;
497515
const res = await this.props.onDecrease(decreased_num);
@@ -513,7 +531,7 @@ class InputSpinner extends Component {
513531
}
514532

515533
this.decreaseTimer = setTimeout(this.decrease.bind(this), wait);
516-
this.onChange(num);
534+
this.onChange(num, true);
517535
}
518536

519537
/**
@@ -1095,6 +1113,7 @@ InputSpinner.propTypes = {
10951113
accelerationDelay: PropTypes.number,
10961114
speed: PropTypes.number,
10971115
emptied: PropTypes.bool,
1116+
continuity: PropTypes.bool,
10981117
typingTime: PropTypes.number,
10991118
buttonLeftDisabled: PropTypes.bool,
11001119
buttonRightDisabled: PropTypes.bool,
@@ -1154,6 +1173,7 @@ InputSpinner.defaultProps = {
11541173
accelerationDelay: defaultAccelerationDelay,
11551174
speed: defaultSpeed,
11561175
emptied: false,
1176+
continuity: false,
11571177
typingTime: defaultTypingTime,
11581178
buttonLeftDisabled: false,
11591179
buttonRightDisabled: false,

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface ReactNativeInputSpinnerProps {
5050
accelerationDelay?: number;
5151
speed?: number;
5252
emptied?: boolean;
53+
continuity?: boolean;
5354
typingTime?: number;
5455
buttonLeftDisabled?: boolean;
5556
buttonRightDisabled?: boolean;

0 commit comments

Comments
 (0)