Skip to content

Commit 0b84349

Browse files
authored
feat: intlConfig support all NumberFormatOptions (#386)
1 parent 0b6d5ae commit 0b84349

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

src/components/CurrencyInputProps.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export type CurrencyInputOnChangeValues = {
3232

3333
export type IntlConfig = {
3434
locale: string;
35-
currency?: string;
36-
};
35+
} & Intl.NumberFormatOptions;
3736

3837
export type CurrencyInputProps = Overwrite<
3938
React.ComponentPropsWithRef<'input'>,
@@ -189,6 +188,6 @@ export type CurrencyInputProps = Overwrite<
189188
* specifically on blur events. If disabled or set to false, the onValueChange will not trigger on blur.
190189
* Default = true
191190
*/
192-
formatValueOnBlur?: boolean;
191+
formatValueOnBlur?: boolean;
193192
}
194193
>;

src/components/utils/formatValue.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,19 @@ export const formatValue = (options: FormatValueOptions): string => {
8888
value = '0' + value;
8989
}
9090

91+
const { locale, currency, ...formatOptions } = intlConfig || {};
92+
9193
const defaultNumberFormatOptions = {
94+
...formatOptions,
9295
minimumFractionDigits: decimalScale || 0,
9396
maximumFractionDigits: 20,
9497
};
9598

9699
const numberFormatter = intlConfig
97-
? new Intl.NumberFormat(
98-
intlConfig.locale,
99-
intlConfig.currency
100-
? {
101-
...defaultNumberFormatOptions,
102-
style: 'currency',
103-
currency: intlConfig.currency,
104-
}
105-
: defaultNumberFormatOptions
106-
)
100+
? new Intl.NumberFormat(locale, {
101+
...defaultNumberFormatOptions,
102+
...(currency && { style: 'currency', currency }),
103+
})
107104
: new Intl.NumberFormat(undefined, defaultNumberFormatOptions);
108105

109106
const parts = numberFormatter.formatToParts(Number(value));

src/components/utils/getLocaleConfig.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ const defaultConfig: LocaleConfig = {
2020
* Get locale config from input or default
2121
*/
2222
export const getLocaleConfig = (intlConfig?: IntlConfig): LocaleConfig => {
23-
const { locale, currency } = intlConfig || {};
23+
const { locale, currency, ...formatOptions } = intlConfig || {};
2424
const numberFormatter = locale
25-
? new Intl.NumberFormat(locale, currency ? { currency, style: 'currency' } : undefined)
25+
? new Intl.NumberFormat(locale, {
26+
...formatOptions,
27+
...(currency && { currency, style: 'currency' }),
28+
})
2629
: new Intl.NumberFormat();
2730

2831
return numberFormatter.formatToParts(1000.1).reduce((prev, curr, i): LocaleConfig => {

0 commit comments

Comments
 (0)