Skip to content

Commit

Permalink
[DDW-543] Fixes multiple decimal separators bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikGuzei committed Jan 25, 2021
1 parent 51642de commit 88fc6d2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions source/components/NumericInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class NumericInputBase extends Component<NumericInputProps, State> {
const { decimalSeparator, groupSeparator } = this.getBigNumberFormat();
const changedCaretPosition = target.selectionStart;
const valueToProcess = target.value;
const fallbackInputValue = this.state.fallbackInputValue || '';
const fallbackInputValue = this.state.fallbackInputValue;
const isBackwardDelete = inputType === 'deleteContentBackward';
const isForwardDelete = inputType === 'deleteContentForward';
const isDeletion = isForwardDelete || isBackwardDelete;
Expand Down Expand Up @@ -209,9 +209,7 @@ class NumericInputBase extends Component<NumericInputProps, State> {

const currentNumber = value;
const currentValue =
currentNumber != null
? this.bigNumberToFormattedString(currentNumber)
: fallbackInputValue;
fallbackInputValue ?? this.bigNumberToFormattedString(currentNumber);

const currentNumberOfDecimalSeparators = this.getNumberOfDecimalSeparators(
currentValue
Expand Down Expand Up @@ -261,6 +259,7 @@ class NumericInputBase extends Component<NumericInputProps, State> {
return {
value: newNumber,
caretPosition: newCaretPos,
fallbackInputValue: null,
};
}

Expand Down Expand Up @@ -363,21 +362,19 @@ class NumericInputBase extends Component<NumericInputProps, State> {
const { decimalSeparator, groupSeparator } = this.getBigNumberFormat();
return new BigNumber(
value
.replace(new RegExp(escapeRegExp(groupSeparator), 'g'), '')
.replace(new RegExp(escapeRegExp(decimalSeparator), 'g'), '.')
.replace(escapedGlobalRegExp(groupSeparator), '')
.replace(escapedGlobalRegExp(decimalSeparator), '.')
);
}

getNumberOfGroupSeparators(value: string): number {
const { groupSeparator } = this.getBigNumberFormat();
return (value.match(new RegExp(escapeRegExp(groupSeparator), 'g')) || [])
.length;
return (value.match(escapedGlobalRegExp(groupSeparator)) || []).length;
}

getNumberOfDecimalSeparators(value: string): number {
const { decimalSeparator } = this.getBigNumberFormat();
return (value.match(new RegExp(escapeRegExp(decimalSeparator), 'g')) || [])
.length;
return (value.match(escapedGlobalRegExp(decimalSeparator)) || []).length;
}

isDifferentValue(first: ?BigNumber.Instance, second: ?BigNumber.Instance) {
Expand Down Expand Up @@ -425,3 +422,7 @@ class NumericInputBase extends Component<NumericInputProps, State> {
}

export const NumericInput = withTheme(NumericInputBase);

function escapedGlobalRegExp(regex) {
return new RegExp(escapeRegExp(regex), 'g');
}

0 comments on commit 88fc6d2

Please sign in to comment.