Skip to content

Commit b767640

Browse files
committed
feat: add possibility to cancel on change, increase and decrease
1 parent 7f51ee5 commit b767640

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

index.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ class InputSpinner extends Component {
5555
*/
5656
componentDidUpdate(prevProps) {
5757
// Parse Value
58-
if (this.props.value !== this.state.value) {
58+
if (this.props.value !== prevProps.value) {
5959
let newValue = this.parseNum(this.props.value);
6060
newValue = this.withinRange(newValue);
6161
this.setState({value: newValue});
6262
}
6363
// Parse Min
64-
if (this.props.min !== this.state.min) {
64+
if (this.props.min !== prevProps.min) {
6565
this.setState({
6666
min:
6767
this.props.min != null
@@ -70,7 +70,7 @@ class InputSpinner extends Component {
7070
});
7171
}
7272
// Parse Max
73-
if (this.props.max !== this.state.max) {
73+
if (this.props.max !== prevProps.max) {
7474
this.setState({
7575
max:
7676
this.props.max != null
@@ -79,7 +79,7 @@ class InputSpinner extends Component {
7979
});
8080
}
8181
// Parse Step
82-
if (this.props.step !== this.state.step) {
82+
if (this.props.step !== prevProps.step) {
8383
let spinnerStep = this.parseNum(this.props.step);
8484
if (!this.typeDecimal() && spinnerStep < 1) {
8585
spinnerStep = 1;
@@ -112,17 +112,21 @@ class InputSpinner extends Component {
112112
this.props.onMax(this.state.max);
113113
}
114114
}
115-
this.setState({value: num});
116115
} else {
117116
if (this.props.onMin) {
118117
this.props.onMin(this.state.min);
119118
}
120119
num = this.state.min;
121-
this.setState({value: num});
122120
}
123121
if (current_value !== num && this.props.onChange) {
124-
this.props.onChange(num);
122+
const res = this.props.onChange(num);
123+
if (res === false) {
124+
return;
125+
} else if (this.isNumeric(res)) {
126+
num = this.parseNum(res);
127+
}
125128
}
129+
this.setState({value: num});
126130
}
127131

128132
/**
@@ -274,7 +278,12 @@ class InputSpinner extends Component {
274278
if (this.maxReached(num)) {
275279
increased_num = this.state.max;
276280
}
277-
this.props.onIncrease(increased_num);
281+
const res = this.props.onIncrease(increased_num);
282+
if (res === false) {
283+
return;
284+
} else if (this.isNumeric(res)) {
285+
num = this.parseNum(res);
286+
}
278287
}
279288
this.onChange(num);
280289
}
@@ -291,11 +300,30 @@ class InputSpinner extends Component {
291300
if (this.minReached(num)) {
292301
decreased_num = this.state.min;
293302
}
294-
this.props.onDecrease(decreased_num);
303+
const res = this.props.onDecrease(decreased_num);
304+
if (res === false) {
305+
return;
306+
} else if (this.isNumeric(res)) {
307+
num = this.parseNum(res);
308+
}
295309
}
296310
this.onChange(num);
297311
}
298312

313+
/**
314+
* Detect if is a numeric value
315+
* @param num
316+
* @returns {boolean}
317+
*/
318+
isNumeric(num) {
319+
return (
320+
num !== null &&
321+
num !== false &&
322+
!isNaN(parseFloat(num)) &&
323+
!isNaN(num - 0)
324+
);
325+
}
326+
299327
/**
300328
* On Submit keyboard
301329
* @returns {*}

0 commit comments

Comments
 (0)