Skip to content

Commit fff2e34

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents cded66e + fe18343 commit fff2e34

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/components/ui/currency-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const CurrencyInput: React.FC<
4040
}}
4141
onChange={(e) => {
4242
const rawValue = e.target.value;
43-
const strValue = sanitizeInput(rawValue, allowNegative);
43+
const strValue = sanitizeInput(rawValue, allowNegative, true);
4444
const bigIntValue = toSafeBigInt(strValue, allowNegative);
4545
onValueChange({ strValue, bigIntValue });
4646
}}

src/utils/numbers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const getCurrencyHelpers = ({
2323
formatter.formatToParts(11111111).find(({ type }) => type === 'group')?.value ?? '';
2424
const decimalSeparator =
2525
formatter.formatToParts(1.1).find(({ type }) => type === 'decimal')?.value ?? '.';
26+
const alternativeDecimalSeparator = decimalSeparator === '.' ? ',' : '.';
2627
const literalSeparator =
2728
formatter.formatToParts(1.1).find(({ type }) => type === 'literal')?.value ?? '';
2829
const decimalMultiplier = parseInt(`1${'0'.repeat(decimalDigits)}`, 10);
@@ -78,7 +79,7 @@ export const getCurrencyHelpers = ({
7879
};
7980

8081
/* Sanitize input by allowing only digits, negative sign, and one decimal separator */
81-
const sanitizeInput = (input: string, signed = false) => {
82+
const sanitizeInput = (input: string, signed = false, alternativeDecimal = false) => {
8283
let cleaned = '';
8384
let hasDecimalSeparator = false;
8485
let hasNegativeSign = false;
@@ -90,6 +91,16 @@ export const getCurrencyHelpers = ({
9091
hasDecimalSeparator = true;
9192
return;
9293
}
94+
if (
95+
alternativeDecimal &&
96+
letter === alternativeDecimalSeparator &&
97+
!hasDecimalSeparator &&
98+
!input.includes(decimalSeparator)
99+
) {
100+
cleaned += decimalSeparator;
101+
hasDecimalSeparator = true;
102+
return;
103+
}
93104
// when a user presses '-' sign, switch the sign of the number
94105
if (letter === '-' && !hasNegativeSign) {
95106
hasNegativeSign = true;

0 commit comments

Comments
 (0)