/
Money.js
47 lines (40 loc) · 1.26 KB
/
Money.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @flow
import * as React from 'react';
import sx from '@adeira/sx';
import type { SupportedCurrencies, SupportedLocales } from '../constants';
import useSxDesignContext from '../useSxDesignContext';
type MoneyProps = {
+priceUnitAmount: number,
+priceUnitAmountCurrency: SupportedCurrencies,
};
export default function Money(props: MoneyProps): React.Node {
const sxDesign = useSxDesignContext();
return (
<span className={styles('text')}>
{MoneyFn({
...props,
locale: sxDesign.locale,
})}
</span>
);
}
const styles = sx.create({
text: {
color: 'rgba(var(--sx-money-text-color, var(--sx-foreground)))',
transition: 'inherit',
},
});
type MoneyFnProps = {
+priceUnitAmount: number,
+priceUnitAmountCurrency: SupportedCurrencies,
+locale: SupportedLocales,
};
// This function does essentially the same like the React <Money /> component except it can be
// called from non-React environment.
export function MoneyFn(props: MoneyFnProps): string {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat
return new Intl.NumberFormat(props.locale, {
style: 'currency',
currency: props.priceUnitAmountCurrency,
}).format(props.priceUnitAmount);
}