Skip to content

Commit e47040a

Browse files
Merge pull request #39 from selsamman/master
feat: Add longStep property to be able to step in larger units when long pressing
2 parents 39cf829 + 245a3eb commit e47040a

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

PROPS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| `inputStyle` | Input Style (Text number at middle) | Object | | Could overwrite other props |
4242
| `inputProps` | Customized TextInput Component props | Object | `null` | |
4343
| `leftButtonProps` | Customized left button (Touchable Component) props | Object | `null` | |
44+
| `longStep` | Value to increment or decrement the current spinner value `onLongPress` | String<br>Number | `step` |
4445
| `maxLength` | Limits the maximum number of characters that can be entered. | Number | | |
4546
| `max` | Max number permitted | String<br>Number | `null` | |
4647
| `min` | Min value permitted | String<br>Number | `0` | |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Check the "[Props List](PROPS.md)" file to have the complete list of component p
126126
| `selectTextOnFocus` | If `true`, all text will automatically be selected on focus. | Bool | `false` | |
127127
| `selectionColor` | The highlight and cursor color of the text input. | String | `null` | |
128128
| `step` | Value to increment or decrement the current spinner value | String<br>Number | `1` | |
129+
| `longStep` | Value to increment or decrement the current spinner value `onLongPress` | String<br>Number | `step` | |
129130
| `speed` | Speed acceleration ratio of increase or decrease `onLongPress` | Number | `7` | (value from `1` to `10`) |
130131
| `typingTime` | Time before debounce and trigger `onChange` event | Number | `750` | |
131132
| `type` | Type of spinner | String | `int` | Can be `int` or `real`/`float`... |

src/InputSpinner.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class InputSpinner extends Component {
3333
if (!this.isTypeDecimal() && spinnerStep < 1) {
3434
spinnerStep = 1;
3535
}
36+
let spinnerLongStep = this._parseNum(this.props.longStep);
37+
if (!this.isTypeDecimal() && spinnerLongStep < 1) {
38+
spinnerLongStep = 0;
39+
}
3640

3741
const min = this.props.min != null ? this._parseNum(this.props.min) : null;
3842
const max = this.props.max != null ? this._parseNum(this.props.max) : null;
@@ -62,6 +66,7 @@ class InputSpinner extends Component {
6266
max: max,
6367
value: initialValue,
6468
step: spinnerStep,
69+
longStep: spinnerLongStep,
6570
focused: false,
6671
buttonPress: null,
6772
};
@@ -99,6 +104,14 @@ class InputSpinner extends Component {
99104
}
100105
this.setState({step: spinnerStep});
101106
}
107+
// Parse longStep
108+
if (this.props.longStep !== prevProps.longStep) {
109+
let spinnerLongStep = this._parseNum(this.props.longStep);
110+
if (!this.isTypeDecimal() && spinnerLongStep < 1) {
111+
spinnerLongStep = 0;
112+
}
113+
this.setState({longStep: spinnerLongStep});
114+
}
102115
}
103116

104117
/**
@@ -549,11 +562,23 @@ class InputSpinner extends Component {
549562

550563
/**
551564
* Increase
565+
* @param event
566+
* @param isLongPress
567+
* @returns {Promise<void>}
552568
*/
553-
async increase() {
569+
async increase(event, isLongPress = false) {
554570
if (this._isDisabledButtonRight()) return;
555571
let currentValue = this._parseNum(this.state.value);
556-
let num = currentValue + this._parseNum(this.state.step);
572+
let num =
573+
currentValue +
574+
this._parseNum(
575+
isLongPress && this.state.longStep > 0
576+
? this.state.longStep
577+
: this.state.step,
578+
);
579+
if (isLongPress && this.state.longStep > 0) {
580+
num = Math.round(num / this.state.longStep) * this.state.longStep;
581+
}
557582

558583
if (
559584
this.isMaxReached(currentValue) &&
@@ -581,17 +606,32 @@ class InputSpinner extends Component {
581606
this.onLongPress(num);
582607
}
583608

584-
this.increaseTimer = setTimeout(this.increase.bind(this), wait);
609+
this.increaseTimer = setTimeout(
610+
this.increase.bind(this, event, true),
611+
wait,
612+
);
585613
this.onChange(num, true);
586614
}
587615

588616
/**
589617
* Decrease
618+
* @param event
619+
* @param isLongPress
620+
* @returns {Promise<void>}
590621
*/
591-
async decrease() {
622+
async decrease(event, isLongPress = false) {
592623
if (this._isDisabledButtonLeft()) return;
593624
let currentValue = this._parseNum(this.state.value);
594-
let num = currentValue - this._parseNum(this.state.step);
625+
let num =
626+
currentValue -
627+
this._parseNum(
628+
isLongPress && this.state.longStep > 0
629+
? this.state.longStep
630+
: this.state.step,
631+
);
632+
if (isLongPress && this.state.longStep > 0) {
633+
num = Math.round(num / this.state.longStep) * this.state.longStep;
634+
}
595635

596636
if (
597637
this.isMinReached(currentValue) &&
@@ -619,7 +659,10 @@ class InputSpinner extends Component {
619659
this.onLongPress(num);
620660
}
621661

622-
this.decreaseTimer = setTimeout(this.decrease.bind(this), wait);
662+
this.decreaseTimer = setTimeout(
663+
this.decrease.bind(this, event, true),
664+
wait,
665+
);
623666
this.onChange(num, true);
624667
}
625668

@@ -1149,6 +1192,7 @@ InputSpinner.propTypes = {
11491192
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
11501193
initialValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
11511194
step: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
1195+
longStep: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
11521196
precision: PropTypes.number,
11531197
shadow: PropTypes.bool,
11541198
rounded: PropTypes.bool,
@@ -1223,6 +1267,7 @@ InputSpinner.defaultProps = {
12231267
value: 0,
12241268
initialValue: null,
12251269
step: 1,
1270+
longStep: 0,
12261271
precision: 2,
12271272
rounded: true,
12281273
shadow: false,

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ReactNativeInputSpinnerProps {
88
value?: string | number;
99
initialValue?: string | number;
1010
step?: string | number;
11+
longStep?: string | number;
1112
precision?: number;
1213
shadow?: boolean;
1314
rounded?: boolean;

0 commit comments

Comments
 (0)