useMoney
Formats prices in the page language (with the riyal icon for SAR), and parses or validates amounts.
import { useMoney, UseMoneyResult } from '@salla.sa/twilight-theme-engine/hooks/useMoney';In plain words
Showing a price looks simple until you need two decimals, Arabic digits, and the Saudi riyal symbol. useMoney() does that for you: format(1250) becomes "1,250.00" followed by the riyal icon.
It also has parse, which turns a price string back into a number, and isValid, which checks whether a value is a usable amount.
Signature
function useMoney(): UseMoneyResult
interface UseMoneyResult {
format: (
amount: number | string | undefined,
options?: { currency?: string; locale?: string; type?: 'product' | 'general' }
) => React.ReactNode;
parse: (moneyString: string) => number;
isValid: (value: unknown) => boolean;
}Try it live
١,٢٥٠.٠٠
- isValid(amount)
- true
- parse('1,250.50 SAR')
- 1250.5
import { useMoney } from '@salla.sa/twilight-theme-engine/hooks/useMoney';
export function Price() {
const { format } = useMoney();
return <span className="price">{format(1250)}</span>;
}
Example
import { useMoney } from '@salla.sa/twilight-theme-engine/hooks/useMoney';
export function Price({ amount }: { amount: number }) {
const { format } = useMoney();
return <span className="price">{format(amount)}</span>;
}
How it behaves
formatdefaults to currencySAR, the page locale and typeproduct, and always shows two decimals.For
SARit returns markup, the number followed by<i className="sicon-sar" />. Any other currency goes throughIntl.NumberFormatwithstyle: currency, falling back to "1,250.00 XYZ" for a code Intl does not know.An empty amount returns "-" when the store enables
settings.product.show_price_as_dashand the type isproduct; otherwise an empty string.Arabic digits are forced only when the store enables
arabic_numbers_enabledand the locale starts withar.
Gotchas
formatreturns a React node, not a string.format(100) + " SAR"oraria-label={format(100)}gives "[object Object]" for SAR. Render it as a child instead.
Related
Source and docs
- Engine source:
packages/theme-engine/src/hooks/useMoney.tsx