Skip to content

Commit 1b8ae16

Browse files
authored
SUPPORT-32352 fix(money input): add support for apostrophe as a fraction separator for money value input when locale is de-CH (#3094)
* fix(money input): add support for apostrophe as a fraction separator for money value input when locale is de-CH * fix(test): update test to only test for right quote added by .toLocaleString to numbers for locale de-CH
1 parent 4a47822 commit 1b8ae16

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.changeset/tangy-parents-sniff.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@commercetools-uikit/money-input': patch
3+
'@commercetools-uikit/utils': patch
4+
---
5+
6+
fix bug in money input where amounts above 999 were not being parsed correctly when locale is de-CH

packages/components/inputs/money-input/src/money-input.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ describe('MoneyInput.parseMoneyValue', () => {
328328
).toEqual({ amount: '1234,567', currencyCode: 'EUR' });
329329
});
330330
});
331+
describe('when called with a locale that uses right quote (0x2019) or single quote as fraction separator for 🇨🇭', () => {
332+
// The Swiss do things different https://en.wikipedia.org/wiki/Decimal_separator#Examples_of_use
333+
it('should parse the value according to the passed locale when separator is right quote', () => {
334+
expect(
335+
MoneyInput.parseMoneyValue(
336+
{
337+
type: 'highPrecision',
338+
currencyCode: 'EUR',
339+
fractionDigits: 3,
340+
preciseAmount: 1234567,
341+
},
342+
'de-CH'
343+
)
344+
).toEqual({ amount: '1’234.567', currencyCode: 'EUR' });
345+
});
346+
});
331347

332348
describe('when called with a minimal, valid centPrecision price', () => {
333349
it('should turn it into a value', () => {

packages/components/inputs/money-input/src/money-input.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ export const parseRawAmountToNumber = (rawAmount: string, locale: string) => {
274274
const lastComma = String(rawAmount).lastIndexOf(',');
275275
fractionsSeparator = lastComma > lastDot ? ',' : '.';
276276
}
277-
278277
fractionsSeparator = fractionsSeparator === '.' ? '\\.' : fractionsSeparator; // here we escape the '.' to use it as regex
279278
// The raw amount with only one sparator
280279
const normalizedAmount = String(rawAmount)
@@ -305,7 +304,7 @@ export const createMoneyValue = (
305304

306305
const currency = allCurrencies[currencyCode];
307306
if (!currency) return null;
308-
307+
// The user may enter a value with a comma, dot, or apostrophe as the decimal separator.
309308
if (rawAmount.length === 0 || !isNumberish(rawAmount)) return null;
310309

311310
warning(

packages/utils/src/is-numberish.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Given a string, validates that it has the correct format
22
// to be a number, with decimal separators and negative sign.
33
export default function isNumberish(number: string): boolean {
4-
return !/[^(\-?)\d,.\s]/.test(number);
4+
return !/[^(\-?)\d,.'\s]/.test(number);
55
}

0 commit comments

Comments
 (0)