@adobe-commerce/elsie 1.6.0-alpha2 → 1.6.0-alpha4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* Copyright 2024 Adobe
|
|
3
3
|
* All Rights Reserved.
|
|
4
4
|
*
|
|
5
|
-
* NOTICE: Adobe permits you to use, modify, and distribute this
|
|
6
|
-
* file in accordance with the terms of the Adobe license agreement
|
|
7
|
-
* accompanying it.
|
|
5
|
+
* NOTICE: Adobe permits you to use, modify, and distribute this
|
|
6
|
+
* file in accordance with the terms of the Adobe license agreement
|
|
7
|
+
* accompanying it.
|
|
8
8
|
*******************************************************************/
|
|
9
9
|
|
|
10
10
|
import { FunctionComponent } from 'preact';
|
|
11
11
|
import { HTMLAttributes, useMemo } from 'preact/compat';
|
|
12
|
-
import { classes,
|
|
12
|
+
import { classes, getPriceFormatter } from '@adobe-commerce/elsie/lib';
|
|
13
13
|
import '@adobe-commerce/elsie/components/Price/Price.css';
|
|
14
14
|
|
|
15
15
|
export interface PriceProps
|
|
@@ -39,43 +39,10 @@ export const Price: FunctionComponent<PriceProps> = ({
|
|
|
39
39
|
size = 'small',
|
|
40
40
|
...props
|
|
41
41
|
}) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
const globalLocale = getGlobalLocale();
|
|
48
|
-
if (globalLocale) {
|
|
49
|
-
return globalLocale;
|
|
50
|
-
}
|
|
51
|
-
// Fallback to browser locale or default
|
|
52
|
-
return process.env.LOCALE && process.env.LOCALE !== 'undefined' ? process.env.LOCALE : 'en-US';
|
|
53
|
-
}, [locale]);
|
|
54
|
-
|
|
55
|
-
const formatter = useMemo(
|
|
56
|
-
() => {
|
|
57
|
-
const params: Intl.NumberFormatOptions = {
|
|
58
|
-
style: 'currency',
|
|
59
|
-
currency: currency || 'USD',
|
|
60
|
-
// These options are needed to round to whole numbers if that's what you want.
|
|
61
|
-
minimumFractionDigits: 2, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
|
|
62
|
-
maximumFractionDigits: 2, // (causes 2500.99 to be printed as $2,501)
|
|
63
|
-
...formatOptions,
|
|
64
|
-
}
|
|
65
|
-
try {
|
|
66
|
-
return new Intl.NumberFormat(effectiveLocale, params);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error(`Error creating Intl.NumberFormat instance for locale ${effectiveLocale}. Falling back to en-US.`, error);
|
|
69
|
-
return new Intl.NumberFormat('en-US', params);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
[effectiveLocale, currency, formatOptions]
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const formattedAmount = useMemo(
|
|
76
|
-
() => formatter.format(amount),
|
|
77
|
-
[amount, formatter]
|
|
78
|
-
);
|
|
42
|
+
const formattedAmount = useMemo(() => {
|
|
43
|
+
const formatter = getPriceFormatter({ currency, locale, formatOptions });
|
|
44
|
+
return formatter.format(amount);
|
|
45
|
+
}, [amount, currency, locale, formatOptions]);
|
|
79
46
|
|
|
80
47
|
return (
|
|
81
48
|
<span
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/********************************************************************
|
|
2
|
+
* Copyright 2025 Adobe
|
|
3
|
+
* All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* NOTICE: Adobe permits you to use, modify, and distribute this
|
|
6
|
+
* file in accordance with the terms of the Adobe license agreement
|
|
7
|
+
* accompanying it.
|
|
8
|
+
*******************************************************************/
|
|
9
|
+
|
|
10
|
+
import { getGlobalLocale } from '@adobe-commerce/elsie/lib';
|
|
11
|
+
|
|
12
|
+
export interface PriceFormatterOptions {
|
|
13
|
+
currency?: string | null;
|
|
14
|
+
locale?: string;
|
|
15
|
+
formatOptions?: Intl.NumberFormatOptions;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determines the effective locale to use for price formatting
|
|
20
|
+
* Priority: prop locale > global locale > browser locale > default 'en-US'
|
|
21
|
+
*/
|
|
22
|
+
export function getEffectiveLocale(locale?: string): string {
|
|
23
|
+
if (locale) {
|
|
24
|
+
return locale;
|
|
25
|
+
}
|
|
26
|
+
const globalLocale = getGlobalLocale();
|
|
27
|
+
if (globalLocale) {
|
|
28
|
+
return globalLocale;
|
|
29
|
+
}
|
|
30
|
+
// Fallback to browser locale or default
|
|
31
|
+
return process.env.LOCALE && process.env.LOCALE !== 'undefined' ? process.env.LOCALE : 'en-US';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Gets an Intl.NumberFormat instance for price formatting
|
|
36
|
+
* Uses getEffectiveLocale internally to determine the best locale
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Single price formatting
|
|
40
|
+
* const formatter = getPriceFormatter({ currency: 'USD', locale: 'en-US' });
|
|
41
|
+
* const price = formatter.format(10.99); // "$10.99"
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* // Bulk price formatting (more efficient)
|
|
45
|
+
* const formatter = getPriceFormatter({ currency: 'EUR', locale: 'fr-FR' });
|
|
46
|
+
* const prices = [10.99, 25.50, 99.99].map(amount => formatter.format(amount));
|
|
47
|
+
*/
|
|
48
|
+
export function getPriceFormatter(
|
|
49
|
+
options: PriceFormatterOptions = {}
|
|
50
|
+
): Intl.NumberFormat {
|
|
51
|
+
const { currency, locale, formatOptions = {} } = options;
|
|
52
|
+
const effectiveLocale = getEffectiveLocale(locale);
|
|
53
|
+
|
|
54
|
+
const params: Intl.NumberFormatOptions = {
|
|
55
|
+
style: 'currency',
|
|
56
|
+
currency: currency || 'USD',
|
|
57
|
+
// These options are needed to round to whole numbers if that's what you want.
|
|
58
|
+
minimumFractionDigits: 2, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
|
|
59
|
+
maximumFractionDigits: 2, // (causes 2500.99 to be printed as $2,501)
|
|
60
|
+
...formatOptions,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
return new Intl.NumberFormat(effectiveLocale, params);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error(`Error creating Intl.NumberFormat instance for locale ${effectiveLocale}. Falling back to en-US.`, error);
|
|
67
|
+
return new Intl.NumberFormat('en-US', params);
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* Copyright 2024 Adobe
|
|
3
3
|
* All Rights Reserved.
|
|
4
4
|
*
|
|
5
|
-
* NOTICE: Adobe permits you to use, modify, and distribute this
|
|
6
|
-
* file in accordance with the terms of the Adobe license agreement
|
|
7
|
-
* accompanying it.
|
|
5
|
+
* NOTICE: Adobe permits you to use, modify, and distribute this
|
|
6
|
+
* file in accordance with the terms of the Adobe license agreement
|
|
7
|
+
* accompanying it.
|
|
8
8
|
*******************************************************************/
|
|
9
9
|
|
|
10
10
|
export * from '@adobe-commerce/elsie/lib/form-values';
|
|
@@ -25,3 +25,4 @@ export * from '@adobe-commerce/elsie/lib/is-number';
|
|
|
25
25
|
export * from '@adobe-commerce/elsie/lib/deviceUtils';
|
|
26
26
|
export * from '@adobe-commerce/elsie/lib/get-path-value';
|
|
27
27
|
export * from '@adobe-commerce/elsie/lib/get-cookie';
|
|
28
|
+
export * from '@adobe-commerce/elsie/lib/get-price-formatter';
|