Skip to content

Commit ae32117

Browse files
author
Marco Cesarato
committed
fix: add adjust value limits and fix inital value issue
1 parent dac9cec commit ae32117

File tree

1 file changed

+98
-36
lines changed

1 file changed

+98
-36
lines changed

index.js

Lines changed: 98 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,22 @@ class InputSpinner extends Component {
2626
spinnerStep = 1;
2727
}
2828

29+
const min =
30+
this.props.min != null ? this.parseNum(this.props.min) : null;
31+
const max =
32+
this.props.max != null ? this.parseNum(this.props.max) : null;
33+
34+
let initialValue =
35+
this.props.initialValue != null && !isNaN(12)
36+
? this.props.initialValue
37+
: this.props.value;
38+
initialValue = this.parseNum(initialValue);
39+
initialValue = this.adjustValueLimits(initialValue, min, max);
40+
2941
this.state = {
30-
min: this.parseNum(this.props.min),
31-
max: this.parseNum(this.props.max),
32-
value: this.parseNum(!isNaN(this.props.initialValue) ? this.props.initialValue : this.props.value),
42+
min: min,
43+
max: max,
44+
value: initialValue,
3345
step: spinnerStep,
3446
focused: false,
3547
buttonPress: null,
@@ -44,15 +56,27 @@ class InputSpinner extends Component {
4456
componentDidUpdate(prevProps) {
4557
// Parse Value
4658
if (this.props.value !== prevProps.value) {
47-
this.setState({value: this.parseNum(this.props.value)});
59+
let newValue = this.parseNum(this.props.value);
60+
newValue = this.adjustValueLimits(newValue);
61+
this.setState({value: newValue});
4862
}
4963
// Parse Min
5064
if (this.props.min !== prevProps.min) {
51-
this.setState({min: this.parseNum(this.props.min)});
65+
this.setState({
66+
min:
67+
this.props.min != null
68+
? this.parseNum(this.props.min)
69+
: null,
70+
});
5271
}
5372
// Parse Max
5473
if (this.props.max !== prevProps.max) {
55-
this.setState({max: this.parseNum(this.props.max)});
74+
this.setState({
75+
max:
76+
this.props.max != null
77+
? this.parseNum(this.props.max)
78+
: null,
79+
});
5680
}
5781
// Parse Step
5882
if (this.props.step !== prevProps.step) {
@@ -71,8 +95,13 @@ class InputSpinner extends Component {
7195
onChange(num) {
7296
if (this.props.disabled) return;
7397
const current_value = this.state.value;
74-
const separator = !this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".";
75-
if (String(num).endsWith(separator) && !this.getValue().endsWith(separator + "0")) {
98+
const separator = !this.isStringEmpty(this.props.decimalSeparator)
99+
? this.props.decimalSeparator
100+
: ".";
101+
if (
102+
String(num).endsWith(separator) &&
103+
!this.getValue().endsWith(separator + "0")
104+
) {
76105
this.decimalInput = true;
77106
}
78107
num = this.parseNum(String(num).replace(/^0+/, "")) || 0;
@@ -130,13 +159,34 @@ class InputSpinner extends Component {
130159
return num;
131160
}
132161

162+
adjustValueLimits(value, min = null, max = null) {
163+
if (min == null && this.state && this.state.min != null) {
164+
min = this.state.min;
165+
}
166+
if (max == null && this.state && this.state.max != null) {
167+
min = this.state.max;
168+
}
169+
if (min != null && value < min) {
170+
value = Math.min(min, Math.max(value, min));
171+
}
172+
if (max != null && value > max) {
173+
value = Math.max(max, Math.min(value, max));
174+
}
175+
return value;
176+
}
177+
133178
/**
134179
* Parse number type
135180
* @param num
136181
* @returns {*}
137182
*/
138183
parseNum(num) {
139-
num = String(num).replace(!this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".", ".");
184+
num = String(num).replace(
185+
!this.isStringEmpty(this.props.decimalSeparator)
186+
? this.props.decimalSeparator
187+
: ".",
188+
"."
189+
);
140190
if (this.typeDecimal()) {
141191
num = parseFloat(num);
142192
} else {
@@ -157,16 +207,22 @@ class InputSpinner extends Component {
157207
let value = this.state.value;
158208
if (this.typeDecimal() && this.decimalInput) {
159209
this.decimalInput = false;
160-
value = this.parseNum(value).toFixed(1)
161-
.replace(/0+$/, "");
210+
value = this.parseNum(value).toFixed(1).replace(/0+$/, "");
162211
} else if (this.typeDecimal()) {
163-
value = String(this.parseNum(
164-
this.parseNum(value).toFixed(this.props.precision)
165-
));
212+
value = String(
213+
this.parseNum(
214+
this.parseNum(value).toFixed(this.props.precision)
215+
)
216+
);
166217
} else {
167218
value = String(this.parseNum(value));
168219
}
169-
return value.replace(".", !this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".");
220+
return value.replace(
221+
".",
222+
!this.isStringEmpty(this.props.decimalSeparator)
223+
? this.props.decimalSeparator
224+
: "."
225+
);
170226
}
171227

172228
/**
@@ -234,7 +290,7 @@ class InputSpinner extends Component {
234290
* @returns {*}
235291
* @param e
236292
*/
237-
onSubmit(e){
293+
onSubmit(e) {
238294
if (this.props.onSubmit) {
239295
this.props.onSubmit(this.parseNum(e.nativeEvent.text));
240296
}
@@ -245,7 +301,7 @@ class InputSpinner extends Component {
245301
* @returns {*}
246302
* @param e
247303
*/
248-
onFocus(e){
304+
onFocus(e) {
249305
if (this.props.onFocus) {
250306
this.props.onFocus(e);
251307
}
@@ -257,7 +313,7 @@ class InputSpinner extends Component {
257313
* @returns {*}
258314
* @param e
259315
*/
260-
onBlur(e){
316+
onBlur(e) {
261317
if (this.props.onBlur) {
262318
this.props.onBlur(e);
263319
}
@@ -269,7 +325,7 @@ class InputSpinner extends Component {
269325
* @returns {*}
270326
* @param e
271327
*/
272-
onKeyPress(e){
328+
onKeyPress(e) {
273329
if (this.props.onKeyPress) {
274330
this.props.onKeyPress(e);
275331
}
@@ -281,10 +337,13 @@ class InputSpinner extends Component {
281337
* @returns {boolean}
282338
*/
283339
maxReached(num = null) {
284-
if (num == null) {
285-
num = this.state.value;
340+
if (this.state.max != null) {
341+
if (num == null) {
342+
num = this.state.value;
343+
}
344+
return num >= this.state.max;
286345
}
287-
return num >= this.state.max;
346+
return false;
288347
}
289348

290349
/**
@@ -293,10 +352,13 @@ class InputSpinner extends Component {
293352
* @returns {boolean}
294353
*/
295354
minReached(num = null) {
296-
if (num == null) {
297-
num = this.state.value;
355+
if (this.state.min != null) {
356+
if (num == null) {
357+
num = this.state.value;
358+
}
359+
return num <= this.state.min;
298360
}
299-
return num <= this.state.min;
361+
return false;
300362
}
301363

302364
/**
@@ -359,17 +421,17 @@ class InputSpinner extends Component {
359421
* @returns {Boolean}
360422
* @private
361423
*/
362-
_isDisabledButtonLeft(){
363-
return (this.props.disabled || this.props.buttonLeftDisabled);
424+
_isDisabledButtonLeft() {
425+
return this.props.disabled || this.props.buttonLeftDisabled;
364426
}
365427

366428
/**
367429
* Is right button disabled
368430
* @returns {Boolean}
369431
* @private
370432
*/
371-
_isDisabledButtonRight(){
372-
return (this.props.disabled || this.props.buttonRightDisabled);
433+
_isDisabledButtonRight() {
434+
return this.props.disabled || this.props.buttonRightDisabled;
373435
}
374436

375437
/**
@@ -415,8 +477,8 @@ class InputSpinner extends Component {
415477
return this.maxReached()
416478
? this._getColorMax()
417479
: this.minReached()
418-
? this._getColorMin()
419-
: this.props.color;
480+
? this._getColorMin()
481+
: this.props.color;
420482
}
421483

422484
/**
@@ -456,8 +518,8 @@ class InputSpinner extends Component {
456518
return this.maxReached()
457519
? this._getColorMax()
458520
: this.minReached()
459-
? this._getColorMin()
460-
: color;
521+
? this._getColorMin()
522+
: color;
461523
}
462524

463525
/**
@@ -736,7 +798,7 @@ class InputSpinner extends Component {
736798
{this.props.prepend}
737799

738800
<TextInput
739-
ref={input => this.textInput = input}
801+
ref={(input) => (this.textInput = input)}
740802
style={this._getInputTextStyle()}
741803
value={this.getValue()}
742804
selectionColor={this.props.selectionColor}
@@ -805,7 +867,7 @@ InputSpinner.propTypes = {
805867
onMax: PropTypes.func,
806868
onIncrease: PropTypes.func,
807869
onDecrease: PropTypes.func,
808-
onSubmit : PropTypes.func,
870+
onSubmit: PropTypes.func,
809871
buttonLeftDisabled: PropTypes.bool,
810872
buttonRightDisabled: PropTypes.bool,
811873
buttonLeftText: PropTypes.string,
@@ -826,7 +888,7 @@ InputSpinner.propTypes = {
826888
InputSpinner.defaultProps = {
827889
type: "int",
828890
min: 0,
829-
max: 999,
891+
max: null,
830892
value: 0,
831893
initialValue: null,
832894
step: 1,

0 commit comments

Comments
 (0)