From ce1178706892f5408a91d685c61296a154ef01f6 Mon Sep 17 00:00:00 2001 From: melloware Date: Thu, 25 Nov 2021 08:15:06 -0500 Subject: [PATCH] Fix #2465: InputNumber default to min on allowEmpty=false --- src/components/inputnumber/InputNumber.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/components/inputnumber/InputNumber.js b/src/components/inputnumber/InputNumber.js index 7effba7fda..a8d35d9d83 100644 --- a/src/components/inputnumber/InputNumber.js +++ b/src/components/inputnumber/InputNumber.js @@ -783,13 +783,21 @@ export class InputNumber extends Component { if (valueStr != null) { newValue = this.parseValue(valueStr); - newValue = !newValue && !this.props.allowEmpty ? 0 : newValue; + newValue = this.evaluateEmpty(newValue); this.updateInput(newValue, insertedValueStr, operation, valueStr); this.handleOnChange(event, currentValue, newValue); } } + evaluateEmpty(newValue) { + let minimum = this.props.min || 0; + if (minimum < 0) { + minimum = 0; + } + return !newValue && !this.props.allowEmpty ? minimum : newValue; + } + handleOnChange(event, currentValue, newValue) { if (this.props.onChange && this.isValueChanged(currentValue, newValue)) { this.props.onChange({ @@ -907,7 +915,7 @@ export class InputNumber extends Component { } updateInputValue(newValue) { - newValue = !newValue && !this.props.allowEmpty ? 0 : newValue; + newValue = this.evaluateEmpty(newValue); const inputEl = this.inputRef.current; const value = inputEl.value; @@ -920,8 +928,8 @@ export class InputNumber extends Component { } formattedValue(val) { - const newVal = !val && !this.props.allowEmpty ? 0 : val; - return this.formatValue(newVal); + const newValue = this.evaluateEmpty(val); + return this.formatValue(newValue); } concatValues(val1, val2) {