From 504748c59834d1a70ebd3901fb24ca6247d58a65 Mon Sep 17 00:00:00 2001 From: Matt McArdle Date: Tue, 14 Jun 2022 19:38:32 -0400 Subject: [PATCH 1/2] Configure String Value to Decimal Places of Step --- NumericInput/NumericInput.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/NumericInput/NumericInput.js b/NumericInput/NumericInput.js index 29255483..846b6d86 100644 --- a/NumericInput/NumericInput.js +++ b/NumericInput/NumericInput.js @@ -33,6 +33,13 @@ export default class NumericInput extends Component { } } + decimalCount = (num) => { + const numStr = String(num); + if (numStr.includes('.')) { + return numStr.split('.')[1].length; + }; + return 0; + } updateBaseResolution = (width, height) => { calcSize = create({ width, height }) } @@ -41,11 +48,11 @@ export default class NumericInput extends Component { if (this.props.maxValue === null || (value + this.props.step < this.props.maxValue)) { value = (value + this.props.step).toFixed(12) value = this.props.valueType === 'real' ? parseFloat(value) : parseInt(value) - this.setState({ value, stringValue: value.toString() }) + this.setState({ value, stringValue: value.toFixed(this.decimalCount(this.props.step)) }) } else if (this.props.maxValue !== null) { this.props.onLimitReached(true, 'Reached Maximum Value!') value = this.props.maxValue - this.setState({ value, stringValue: value.toString() }) + this.setState({ value, stringValue: value.toFixed(this.decimalCount(this.props.step)) }) } if (value !== this.props.value) @@ -62,7 +69,7 @@ export default class NumericInput extends Component { } if (value !== this.props.value) this.props.onChange && this.props.onChange(Number(value)) - this.setState({ value, stringValue: value.toString() }) + this.setState({ value, stringValue: value.toFixed(this.decimalCount(this.props.step)) }) } isLegalValue = (value, mReal, mInt) => value === '' || (((this.props.valueType === 'real' && mReal(value)) || (this.props.valueType !== 'real' && mInt(value))) && (this.props.maxValue === null || (parseFloat(value) <= this.props.maxValue)) && (this.props.minValue === null || (parseFloat(value) >= this.props.minValue))) From 6cbe7de326522f5d8bb27ad70f148c4ae99f7f1b Mon Sep 17 00:00:00 2001 From: Matt McArdle Date: Tue, 14 Jun 2022 20:32:09 -0400 Subject: [PATCH 2/2] Set correct number of decimal places on initial values --- NumericInput/NumericInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NumericInput/NumericInput.js b/NumericInput/NumericInput.js index 846b6d86..24d2aeb3 100644 --- a/NumericInput/NumericInput.js +++ b/NumericInput/NumericInput.js @@ -14,7 +14,7 @@ export default class NumericInput extends Component { this.state = { value: noInitSent ? props.value ? props.value : 0 : props.initValue, lastValid: noInitSent ? props.value ? props.value : 0 : props.initValue, - stringValue: (noInitSent ? props.value ? props.value : 0 : props.initValue).toString(), + stringValue: (noInitSent ? props.value ? props.value : 0 : props.initValue).toFixed(this.decimalCount(props.step)), } this.ref = null }