Skip to content

Commit

Permalink
Fixed #1498 - When inputnumber has fractionDigits and suffix, a numbe…
Browse files Browse the repository at this point in the history
…r cannot be entered into it.
  • Loading branch information
mertsincan committed Sep 6, 2021
1 parent f07a6a6 commit c7e8611
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions src/components/inputnumber/InputNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 : '';
}
Expand All @@ -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);
Expand All @@ -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 : '';
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c7e8611

Please sign in to comment.