CurrencyInput
Captures a money amount. The member types major units, the value is held in cents as an integer.
- Overview
- Props
Input for a money amount. The user types in major units (1 234,50), and value / onChange use minor units (cents) as an integer (123450), or null when empty.
import { CurrencyInput } from "murray";
Accepts the TextInput props (label, error, helpText, placeholder, etc.) except prefix, suffix, type, inputMode, min, max and step, which the component owns. See the Props tab for the full list.
Usage
Controlled
Pass value and update it from onChange. Use this when the amount lives in your state or form library.
Stored value: 123450 cents
Uncontrolled
Pass defaultValue and let the input keep its own state. onChange still receives every change in cents.
Last onChange: 123450 cents
Invalid text
The user can type anything. Text that is not an amount (12abc, 12,5,0, 12,345 in EUR) emits onChange(null, { invalid: true }), stays as typed on blur, and shows an error until the user fixes it. Check invalid to block submit on optional fields, where null alone means empty. An error prop takes precedence over the built-in message.
Whole units
wholeUnits accepts whole amounts only and shows no decimals. value stays in minor units, so 50 € is 5000. Typing decimals is invalid text.
Stored value: 5000000 cents
Suffix
suffix adds text after the amount and its symbol, such as / month. Screen readers hear it after the currency name: "Allowance (euros / month)".
Locales and currencies
Pass currency from the data you edit (the company's or contract's currency), not from the user's locale: an ops person browsing in fr-FR can edit a Canadian amount in CAD. locale only decides how the number looks.
Everything else comes from Intl.NumberFormat:
- Symbol and position:
1 234,50 €infr-FR,$1,234.50inen-CA,SAR 1,234.50inen-SA. English locales with euros show€1,234.50, matchingformatAmountWithCurrency. - Separators: decimal and thousands separators follow the locale.
- Decimal places: from the currency. EUR, CAD and SAR all take 2, so values are in cents (halalas for SAR).
Typing behavior
- Thousands separators show only when the input is blurred. While focused the input shows
1234,50, so the cursor never jumps. - Spaces, group separators and the currency symbol carry no value. Letters, signs, a second decimal separator and decimals beyond the currency's precision make the text invalid; the error shows on blur, never while typing.
.and,both count as the decimal separator when the text has no locale decimal separator, because a phone's decimal key follows the device locale, not the app's. As a consequence, typing1.234ines-ESis invalid (three decimals), not one thousand.- Negative amounts are not supported.
Accessibility
- The accessible name adds the spoken currency after the label, so screen readers announce "Monthly allowance (euros)". This works with
label,aria-label,aria-labelledbyandtitle. - The visible symbol is
aria-hidden, so it is not read twice. - Polite live regions announce new error messages, including the built-in one on blur ("Only numbers with up to 2 decimal places are allowed."), and blur rewriting the typed amount ("Amount updated to 12,50 €"). Blur that only adds thousands separators stays quiet. On mobile, announcements go through
AccessibilityInfo.announceForAccessibility.
Usage in <Form>
On web, Form.CurrencyInput stores the amount in cents in the Formik values. Invalid text fails validation even on an optional field, so it blocks submit, and shows the format error rather than a schema error such as "required".
Condensed theme
For applications with condensed theme density, this component renders in a condensed style:
Error state
Enter an amount above 0 €
Playground
murray and murray/mobile.CurrencyInput props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| allowNegative | booleanundefined | false | Accept a leading minus sign, such as `-12,50`, which makes `value` negative: `-1250` is -12,50 €. | |
| aria-describedby | stringundefined | Id of an element outside the input that also describes it, such as an error shared by several inputs | ||
| aria-label | stringundefined | Accessibility label, only necessary when not associated with a label | ||
| aria-labelledby | stringundefined | |||
| aria-required | booleanundefined | Overrides `aria-required` set by `required`, to mark the input as mandatory without the asterisk | ||
| autoFocus | booleanundefined | If true, focuses the input on componentDidMount.The default value is false. | ||
| currency* | string | ISO 4217 code, such as `EUR`, `CAD` or `SAR`. Take it from the data you edit, not from the user's locale. | ||
| cursor | Cursorundefined | Enforce a cursor type | ||
| defaultValue | numbernullundefined | null | Uncontrolled initial amount in the minor unit of the currency. | |
| disabled | booleanundefined | Disable input | ||
| enterKeyHint | 6 possibilities"search""done""go""next""send"undefined | |||
| error | ReactNode | Contents of `InputError` component, if not set, error is not rendered. | ||
| focused | booleanundefined | Show focus styles even when the input isn't focused, e.g. while a readonly input's picker is open | ||
| groupLabel | ReactNode | Name of the group the input belongs to, such as the date its day field is part of. VoiceOver and TalkBack don't announce it when focus lands on the input, so on native it ends the input's accessible name, after `label`. Ignored without a `label`. | ||
| helpText | ReactNode | Contents of `InputHelp` component, if not set, description is not rendered. | ||
| id | stringundefined | |||
| label | ReactNode | |||
| locale | stringundefined | BCP 47 locale for separators and symbol position, such as `fr-FR` (French), `en-FR` (English in France), `fr-BE`, `nl-BE` (Dutch in Belgium), `es-ES` (Spanish), `fr-CA` (Canadian French), `en-CA` (Canadian English) or `ar-SA` (Arabic in Saudi Arabia). Defaults to the current i18n locale. | ||
| maxLength | numberundefined | Maximum string length that the user can enter in UTF-16 code units | ||
| minLength | numberundefined | Maximum string length that the user can enter in UTF-16 code units @platform web | ||
| name | stringundefined | Html input name @platform web | ||
| onChange | 2 possibilities((value: number | null, details: { invalid: boolean; }) => void)undefined | Receives the amount in minor units (cents) on every change, `null` when cleared or invalid. `details.invalid` tells text that is not an amount, such as "12,5,0", apart from an empty input. | ||
| onPress | 2 possibilities((event: GestureResponderEvent) => void)undefined | |||
| placeholder | stringundefined | |||
| prefixPointerEvents | 5 possibilities"none""auto""box-none""box-only"undefined | Sets pointer-events styles on the `prefix` element, `none` by default | ||
| readonly | booleanundefined | Make input readonly | ||
| render | 2 possibilities((elements: { input: ReactNode; prefix?: ReactNode; suffix?: ReactNode; }, pseudoState: PseudoState) => ReactNode)undefined | Fully control how input, prefix and suffix are rendererd | ||
| renderPlaceholder | (() => ReactNode)undefined | To render a custom placeholder view when the field is empty | ||
| required | booleanundefined | Adds required attribute to the input and a red asterisk on the right side of label, `false` by default Determines whether the input should have `aria-required` attribute | ||
| suffix | stringundefined | Text after the amount and its symbol, such as `/ month`. Screen readers hear it after the currency name. | ||
| suffixPointerEvents | 5 possibilities"none""auto""box-none""box-only"undefined | Sets pointer-events styles on the `suffix` element, `none` by default | ||
| sx | PositionPropsundefined | Positioning properties | ||
| title | stringundefined | Html input title @platform web | ||
| value | numbernullundefined | Controlled amount in the minor unit of the currency: `1250` is 12,50 €. `null` when empty. | ||
| wholeUnits | booleanundefined | false | Accept whole amounts only, such as `50` but not `50,50`. `value` stays in minor units: `5000` is 50 €. |
Undocumented props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| onBlur | any |
Style API
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: