Skip to content

Commit

Permalink
revert currency input
Browse files Browse the repository at this point in the history
  • Loading branch information
ezra-sg committed Oct 9, 2023
1 parent ecd66f4 commit 215a2f7
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/components/evm/inputs/CurrencyInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -523,20 +523,8 @@ export default defineComponent({
this.inputIsDirty = true;
}
// save caret position and number of large number separators (e.g. comma or dot) for later
let caretPosition = this.inputElement.selectionStart || 0;
const savedLargeNumberSeparatorCount = (this.inputElement.value.match(this.largeNumberSeparatorRegex) || []).length;
this.inputElement.value = val;
// get information needed to preserve user caret position in case commas/dots are added/removed
const newLargeNumberSeparatorCount = (
this.inputElement.value.match(this.notIntegerOrDecimalSeparatorRegex) ?? []
).length;
const deltaLargeNumberSeparatorCount = newLargeNumberSeparatorCount - savedLargeNumberSeparatorCount;
this.setInputCaretPosition(caretPosition + deltaLargeNumberSeparatorCount);
// set the indent amount for the symbol label inside the input
// 1's are 7px, other numbers are 8px, separators like commas are 2px
const length = val.length;
Expand All @@ -561,11 +549,6 @@ export default defineComponent({
// this method is responsible for emitting a new modelValue, as well as performing certain formatting tasks
// such as ensuring the user cannot paste invalid characters
handleInput() {
if (this.isReadonly || this.isDisabled) {
// this prevents an issue on iOS safari where, when there are two inputs on the page and the second is readonly and depends on the first
// such as on the staking and wrapping pages, focus is lost when the user enters a value in to the first input
return;
}
const zeroWithDecimalSeparator = `0${this.decimalSeparator}`;
const emit = (val: BigNumber) => {
Expand Down Expand Up @@ -636,6 +619,10 @@ export default defineComponent({
.replace(this.notIntegerOrSeparatorRegex, ''),
);
// save caret position and number of large number separators (e.g. comma or dot) for later
let caretPosition = this.inputElement.selectionStart || 0;
const savedLargeNumberSeparatorCount = (this.inputElement.value.match(this.largeNumberSeparatorRegex) || []).length;
// if input element value is zero-ish, emit 0
if (['', null, undefined, '0', zeroWithDecimalSeparator, this.decimalSeparator].includes(this.inputElement.value)) {
// if the user types a decimal separator in a blank input, add a zero before the separator
Expand All @@ -647,8 +634,6 @@ export default defineComponent({
return;
}
let caretPosition = this.inputElement.selectionStart || 0;
// don't format or emit if the user is about to type a decimal
if (
[this.decimalSeparator, '0'].includes(this.inputElement.value[this.inputElement.value.length - 1]) &&
Expand Down Expand Up @@ -688,6 +673,14 @@ export default defineComponent({
newValue = getBigNumberFromLocalizedNumberString(this.inputElement.value, this.decimals, this.locale);
}
// get information needed to preserve user caret position in case commas/dots are added/removed
const newLargeNumberSeparatorCount = (
this.inputElement.value.match(this.notIntegerOrDecimalSeparatorRegex) ?? []
).length;
const deltaLargeNumberSeparatorCount = newLargeNumberSeparatorCount - savedLargeNumberSeparatorCount;
this.setInputCaretPosition(caretPosition + deltaLargeNumberSeparatorCount);
emit(newValue);
},
Expand Down

0 comments on commit 215a2f7

Please sign in to comment.