From c7e86119ef4d2d199117962422953f945551ff3e Mon Sep 17 00:00:00 2001 From: mertsincan Date: Mon, 6 Sep 2021 11:58:09 +0300 Subject: [PATCH] Fixed #1498 - When inputnumber has fractionDigits and suffix, a number cannot be entered into it. --- src/components/inputnumber/InputNumber.vue | 57 ++++++++++++++++------ 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/components/inputnumber/InputNumber.vue b/src/components/inputnumber/InputNumber.vue index 8f600ce01..700c994fb 100755 --- a/src/components/inputnumber/InputNumber.vue +++ b/src/components/inputnumber/InputNumber.vue @@ -425,9 +425,8 @@ export default { event.preventDefault(); if (selectionStart === selectionEnd) { - let deleteChar = inputValue.charAt(selectionStart - 1); - let decimalCharIndex = inputValue.search(this._decimal); - this._decimal.lastIndex = 0; + const deleteChar = inputValue.charAt(selectionStart - 1); + const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue); if (this.isNumeralChar(deleteChar)) { const decimalLength = this.getDecimalLength(inputValue); @@ -447,10 +446,10 @@ export default { } } else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) { - const insertedText = (this.minFractionDigits || 0) < decimalLength ? '' : '0'; + const insertedText = this.isDecimalMode() && (this.minFractionDigits || 0) < decimalLength ? '' : '0'; newValueStr = inputValue.slice(0, selectionStart - 1) + insertedText + inputValue.slice(selectionStart); } - else if (decimalCharIndex > 0 && decimalCharIndex === 1) { + else if (decimalCharIndexWithoutPrefix === 1) { newValueStr = inputValue.slice(0, selectionStart - 1) + '0' + inputValue.slice(selectionStart); newValueStr = this.parseValue(newValueStr) > 0 ? newValueStr : ''; } @@ -475,8 +474,7 @@ export default { if (selectionStart === selectionEnd) { let deleteChar = inputValue.charAt(selectionStart); - let decimalCharIndex = inputValue.search(this._decimal); - this._decimal.lastIndex = 0; + const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue); if (this.isNumeralChar(deleteChar)) { const decimalLength = this.getDecimalLength(inputValue); @@ -496,10 +494,10 @@ export default { } } else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) { - const insertedText = (this.minFractionDigits || 0) < decimalLength ? '' : '0'; + const insertedText = this.isDecimalMode() && (this.minFractionDigits || 0) < decimalLength ? '' : '0'; newValueStr = inputValue.slice(0, selectionStart) + insertedText + inputValue.slice(selectionStart + 1); } - else if (decimalCharIndex > 0 && decimalCharIndex === 1) { + else if (decimalCharIndexWithoutPrefix === 1) { newValueStr = inputValue.slice(0, selectionStart) + '0' + inputValue.slice(selectionStart + 1); newValueStr = this.parseValue(newValueStr) > 0 ? newValueStr : ''; } @@ -560,6 +558,31 @@ export default { return false; }, + isDecimalMode() { + return this.mode === 'decimal'; + }, + getDecimalCharIndexes(val) { + let decimalCharIndex = val.search(this._decimal); + this._decimal.lastIndex = 0; + + const filteredVal = val.replace(this._prefix, '').trim().replace(/\s/g, '').replace(this._currency, ''); + const decimalCharIndexWithoutPrefix = filteredVal.search(this._decimal); + this._decimal.lastIndex = 0; + + return { decimalCharIndex, decimalCharIndexWithoutPrefix }; + }, + getCharIndexes(val) { + const decimalCharIndex = val.search(this._decimal); + this._decimal.lastIndex = 0; + const minusCharIndex = val.search(this._minusSign); + this._minusSign.lastIndex = 0; + const suffixCharIndex = val.search(this._suffix); + this._suffix.lastIndex = 0; + const currencyCharIndex = val.search(this._currency); + this._currency.lastIndex = 0; + + return { decimalCharIndex, minusCharIndex, suffixCharIndex, currencyCharIndex }; + }, insert(event, text, sign = { isDecimalSign: false, isMinusSign: false }) { const minusCharIndexOnText = text.search(this._minusSign); this._minusSign.lastIndex = 0; @@ -570,10 +593,7 @@ export default { const selectionStart = this.$refs.input.$el.selectionStart; const selectionEnd = this.$refs.input.$el.selectionEnd; let inputValue = this.$refs.input.$el.value.trim(); - const decimalCharIndex = inputValue.search(this._decimal); - this._decimal.lastIndex = 0; - const minusCharIndex = inputValue.search(this._minusSign); - this._minusSign.lastIndex = 0; + const { decimalCharIndex, minusCharIndex, suffixCharIndex, currencyCharIndex } = this.getCharIndexes(inputValue); let newValueStr; if (sign.isMinusSign) { @@ -605,7 +625,9 @@ export default { if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) { if ((selectionStart + text.length - (decimalCharIndex + 1)) <= maxFractionDigits) { - newValueStr = inputValue.slice(0, selectionStart) + text + inputValue.slice(selectionStart + text.length); + const charIndex = currencyCharIndex >= selectionStart ? currencyCharIndex - 1 : (suffixCharIndex >= selectionStart ? suffixCharIndex : inputValue.length); + + newValueStr = inputValue.slice(0, selectionStart) + text + inputValue.slice(selectionStart + text.length, charIndex) + inputValue.slice(charIndex); this.updateValue(event, newValueStr, text, operation); } } @@ -840,7 +862,12 @@ export default { if (value) { const valueSplit = value.split(this._decimal); - return valueSplit.length === 2 ? valueSplit[1].length : 0; + if (valueSplit.length === 2) { + return valueSplit[1].replace(this._suffix, '') + .trim() + .replace(/\s/g, '') + .replace(this._currency, '').length; + } } return 0;