houdini/app/javascript/components/intl/HoudiniIntl.tsx

53 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-07-31 20:48:15 +00:00
// License: LGPL-3.0-or-later
import * as React from "react";
2020-10-20 21:57:09 +00:00
import { useIntl, IntlShape, IntlProvider, createIntl} from "react-intl";
2020-07-31 20:48:15 +00:00
import { Money } from "../../common/money";
2020-10-20 21:57:09 +00:00
import { HoudiniIntlContext } from "../../hooks/useHoudiniIntl";
import type {HoudiniIntlShape, FormatMoneyOptions} from '../../hooks/useHoudiniIntl';
2020-07-31 20:48:15 +00:00
function rawFormatMoney(intl:IntlShape, amount:Money, opts?:FormatMoneyOptions) : string {
const formatter = intl.formatters.getNumberFormat(intl.locale, {...opts,
style: 'currency',
currency: amount.currency.toUpperCase(),
});
const adjustedAmount = amount.amount / Math.pow(10, formatter.resolvedOptions().maximumFractionDigits);
return formatter.format(adjustedAmount);
}
2020-10-20 21:57:09 +00:00
/**
* Use this to scope a context to a tree of components.
* Works like [IntlProvider}(https://formatjs.io/docs/react-intl/components#intlprovider)
* But includes support for formatting money based on the current locale.
*
* @export
* @param {ConstructorParameters<typeof IntlProvider>[0]} props
* @returns {JSX.Element}
*/
export default function HoudiniIntlProvider(props:ConstructorParameters<typeof IntlProvider>[0]) : JSX.Element {
2020-07-31 20:48:15 +00:00
return <IntlProvider {...props}>
<InnerProvider>
{props.children}
</InnerProvider>
</IntlProvider>;
}
2020-10-20 21:57:09 +00:00
function InnerProvider({children}:{children:React.ReactNode}) : JSX.Element {
2020-07-31 20:48:15 +00:00
const intl = useIntl();
const formatMoney = React.useCallback((amount:Money, opts?:FormatMoneyOptions) => {
return rawFormatMoney(intl, amount, opts);
}, [intl]);
const houdiniIntl = { ...intl, formatMoney};
return <HoudiniIntlContext.Provider value={houdiniIntl}>
{children}
</HoudiniIntlContext.Provider>;
}
export function createHoudiniIntl(...props:Parameters<typeof createIntl>) : HoudiniIntlShape {
const intl = createIntl(...props);
const formatMoney = (amount:Money, opts?:FormatMoneyOptions) => rawFormatMoney(intl, amount, opts);
return {...intl, formatMoney};
}