Skip to content

Commit 2ca7d16

Browse files
mohas22mohas22
authored andcommitted
updated currency component to show correct currency symbol based on ISO code
1 parent a65e972 commit 2ca7d16

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

packages/react-sdk-components/src/components/field/Currency/Currency.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { NumericFormat } from 'react-number-format';
33
import { TextField } from '@mui/material';
44
import { getComponentFromMap } from '../../../bridge/helpers/sdk_component_map';
@@ -45,9 +45,11 @@ export default function Currency(props: CurrrencyProps) {
4545
const helperTextToDisplay = validatemessage || helperText;
4646
const [values, setValues] = useState(value.toString());
4747

48-
const testProp = {
49-
'data-test-id': testId
50-
};
48+
const testProp = { 'data-test-id': testId };
49+
50+
useEffect(() => {
51+
setValues(value.toString());
52+
}, [value]);
5153

5254
// currencySymbols looks like this: { theCurrencySymbol: '$', theDecimalIndicator: '.', theSeparator: ',' }
5355
const theSymbols = getCurrencyCharacters(currencyISOCode);

packages/react-sdk-components/src/components/helpers/formatters/Currency.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import CurrencyMap from './CurrencyMap';
55
function NumberFormatter(value, { locale = 'en-US', decPlaces = 2, style = '', currency = 'USD' } = {}): string {
66
const currentLocale: string | undefined = getLocale(locale);
77
if (value !== null && value !== undefined) {
8-
return Number(value).toLocaleString(currentLocale, {
9-
minimumFractionDigits: decPlaces,
10-
maximumFractionDigits: decPlaces
11-
});
8+
return Number(value).toLocaleString(currentLocale, { minimumFractionDigits: decPlaces, maximumFractionDigits: decPlaces });
129
}
1310
return value;
1411
}
@@ -20,14 +17,16 @@ function CurrencyFormatter(
2017
const currentLocale: string | undefined = getLocale(locale);
2118
let formattedValue: string = value;
2219
if (value !== null && value !== undefined && value !== '') {
23-
formattedValue = NumberFormatter(value, {
24-
locale: currentLocale,
25-
decPlaces,
26-
style,
27-
currency
28-
});
20+
formattedValue = NumberFormatter(value, { locale: currentLocale, decPlaces, style, currency });
2921

30-
let countryCode: string | undefined = currentLocale?.split('-')[1].toUpperCase();
22+
// For currency other than EUR, we need to determine the country code from currency code
23+
// If currency is EUR, we use the locale to determine the country code
24+
let countryCode: string | undefined;
25+
if (currency !== 'EUR') {
26+
countryCode = currency.substring(0, 2);
27+
} else {
28+
countryCode = currentLocale?.split('-')[1].toUpperCase();
29+
}
3130

3231
// If countryCode is still undefined, setting it as US
3332
if (!countryCode) {
@@ -67,11 +66,7 @@ export default {
6766
Currency: (value, options) => CurrencyFormatter(value, options),
6867
'Currency-Code': (value, options) => CurrencyFormatter(value, { ...options, symbol: false }),
6968
Decimal: (value, options) => NumberFormatter(value, options),
70-
'Decimal-Auto': (value, options) =>
71-
NumberFormatter(value, {
72-
...options,
73-
decPlaces: Number.isInteger(value) ? 0 : 2
74-
}),
69+
'Decimal-Auto': (value, options) => NumberFormatter(value, { ...options, decPlaces: Number.isInteger(value) ? 0 : 2 }),
7570
Integer: (value, options) => NumberFormatter(value, { ...options, decPlaces: 0 }),
7671
Percentage: (value, options) => SymbolFormatter(value, { ...options, symbol: '%' })
7772
};

0 commit comments

Comments
 (0)