Fixed #432 - InputNumber with decimals should support entering comma/decimal point
parent
4d58949b4a
commit
1150e3329c
|
@ -328,6 +328,10 @@ export default {
|
||||||
else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
|
else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
|
||||||
newValueStr = inputValue.slice(0, selectionStart - 1) + '0' + inputValue.slice(selectionStart);
|
newValueStr = inputValue.slice(0, selectionStart - 1) + '0' + inputValue.slice(selectionStart);
|
||||||
}
|
}
|
||||||
|
else if (decimalCharIndex > 0 && decimalCharIndex === 1) {
|
||||||
|
newValueStr = inputValue.slice(0, selectionStart - 1) + '0' + inputValue.slice(selectionStart);
|
||||||
|
newValueStr = this.parseValue(newValueStr) > 0 ? newValueStr : '';
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
newValueStr = inputValue.slice(0, selectionStart - 1) + inputValue.slice(selectionStart);
|
newValueStr = inputValue.slice(0, selectionStart - 1) + inputValue.slice(selectionStart);
|
||||||
}
|
}
|
||||||
|
@ -389,9 +393,10 @@ export default {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let code = event.which || event.keyCode;
|
let code = event.which || event.keyCode;
|
||||||
let char = String.fromCharCode(code);
|
let char = String.fromCharCode(code);
|
||||||
|
const isDecimalSign = this.isDecimalSign(char);
|
||||||
|
|
||||||
if ((48 <= code && code <= 57) || this.isMinusSign(char)) {
|
if ((48 <= code && code <= 57) || this.isMinusSign(char) || isDecimalSign) {
|
||||||
this.insert(event, char);
|
this.insert(event, char, isDecimalSign);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPaste(event) {
|
onPaste(event) {
|
||||||
|
@ -412,14 +417,33 @@ export default {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
insert(event, text) {
|
isDecimalSign(char) {
|
||||||
|
if (this._decimal.test(char)) {
|
||||||
|
this._decimal.lastIndex = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
insert(event, text, isDecimalSign = false) {
|
||||||
let selectionStart = this.$refs.input.$el.selectionStart;
|
let selectionStart = this.$refs.input.$el.selectionStart;
|
||||||
let selectionEnd = this.$refs.input.$el.selectionEnd;
|
let selectionEnd = this.$refs.input.$el.selectionEnd;
|
||||||
let inputValue = this.$refs.input.$el.value.trim();
|
let inputValue = this.$refs.input.$el.value.trim();
|
||||||
let maxFractionDigits = this.numberFormat.resolvedOptions().maximumFractionDigits;
|
const decimalCharIndex = inputValue.search(this._decimal);
|
||||||
let newValueStr;
|
|
||||||
let decimalCharIndex = inputValue.search(this._decimal);
|
|
||||||
this._decimal.lastIndex = 0;
|
this._decimal.lastIndex = 0;
|
||||||
|
let newValueStr;
|
||||||
|
|
||||||
|
if (isDecimalSign) {
|
||||||
|
if (decimalCharIndex > 0 && selectionStart === decimalCharIndex) {
|
||||||
|
this.updateValue(event, inputValue, 'insert');
|
||||||
|
}
|
||||||
|
else if (decimalCharIndex > selectionStart && decimalCharIndex < selectionEnd) {
|
||||||
|
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
|
||||||
|
this.updateValue(event, newValueStr, 'insert');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const maxFractionDigits = this.numberFormat.resolvedOptions().maximumFractionDigits;
|
||||||
|
|
||||||
if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
|
if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
|
||||||
if ((selectionStart + text.length - (decimalCharIndex + 1)) <= maxFractionDigits) {
|
if ((selectionStart + text.length - (decimalCharIndex + 1)) <= maxFractionDigits) {
|
||||||
|
@ -429,7 +453,9 @@ export default {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
|
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
|
||||||
this.updateValue(event, newValueStr, 'insert');
|
const operation = selectionStart !== selectionEnd ? 'range-insert' : 'insert';
|
||||||
|
this.updateValue(event, newValueStr, operation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
insertText(value, text, start, end) {
|
insertText(value, text, start, end) {
|
||||||
|
@ -559,6 +585,8 @@ export default {
|
||||||
if (newLength === currentLength) {
|
if (newLength === currentLength) {
|
||||||
if (operation === 'insert' || operation === 'delete-back-single')
|
if (operation === 'insert' || operation === 'delete-back-single')
|
||||||
this.$refs.input.$el.setSelectionRange(selectionEnd + 1, selectionEnd + 1);
|
this.$refs.input.$el.setSelectionRange(selectionEnd + 1, selectionEnd + 1);
|
||||||
|
else if (operation === 'range-insert')
|
||||||
|
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
|
||||||
else if (operation === 'delete-single')
|
else if (operation === 'delete-single')
|
||||||
this.$refs.input.$el.setSelectionRange(selectionEnd - 1, selectionEnd - 1);
|
this.$refs.input.$el.setSelectionRange(selectionEnd - 1, selectionEnd - 1);
|
||||||
else if (operation === 'delete-range')
|
else if (operation === 'delete-range')
|
||||||
|
|
Loading…
Reference in New Issue