MaskedTextInput
Formats what the member types against a mask, such as an IBAN. Shows the formatted value, reports the raw one.
compliant
- Overview
- Props
Text input that formats user input according to a mask pattern. The displayed value is formatted (e.g. FR76 3000 6000), while onChange always receives the raw unmasked value (e.g. FR7630006000).
import { MaskedTextInput } from "murray";
Accepts all TextInput props (label, error, placeholder, suffix, etc.) plus mask and a required helpText. Like TextInput, it is controlled through value and onChange, or uncontrolled through defaultValue. Either way the input shows the formatted value and onChange receives the raw one.
Usage
Help text
helpText is required: show the expected format with a real example, such as Expected format: FR76 3000 1007 9412 3456 7890 185. A placeholder disappears as soon as the user types and screen readers don't reliably announce it. Help text stays visible while typing and next to an error, and is linked to the input through aria-describedby.
Pass helpText={null} only when the label already states the format.
Uncontrolled
Pass defaultValue and let the input keep its own state. onChange still receives the raw value as the user types.
Expected format: 732 829 320
Error state
Invalid IBAN
Expected format: FR76 3000 1007 9412 3456 7890 185
Expected format: 1 85 05 78 006 084 91
Masking
Common Formats
Use COMMON_MASKS instead of writing your own pattern.
import { COMMON_MASKS, MaskedTextInput } from "murray";
<MaskedTextInput mask={COMMON_MASKS.IBAN} helpText="Expected format: FR76 3000 1007 9412 3456 7890 185" />;
| Mask | Accepts | While blurred |
|---|---|---|
IBAN | Up to 34 letters or digits (ISO 13616) | Full value |
IBAN_OBFUSCATED | Same as IBAN | FR76 ●●●● ●●●● ●●●● ●●●● ●●●● 189 |
FR_SSN | 15 characters, 2A / 2B for Corsica | Full value |
FR_SSN_OBFUSCATED | Same as FR_SSN | 1 ●● ●● ●● ●●● ●●● 78 |
SIREN | 9 digits | Full value |
SIRET | 14 digits | Full value |
- Obfuscated masks show the full value on focus.
onChangealways gets the real value. - Set
autoComplete="off"on these fields. - Another format: write a function mask that reads
focused(Dynamic Mask Function).
Outside an input, obfuscate hides a stored value the same way. It keeps the first and last space-separated group.
import { COMMON_MASKS, createMaskArray, obfuscate } from "murray";
const IBAN_MASK = createMaskArray(COMMON_MASKS.IBAN);
const FR_SSN_MASK = createMaskArray(COMMON_MASKS.FR_SSN);
obfuscate("FR7630006000011234567890189", IBAN_MASK); // "FR76 ●●●● ●●●● ●●●● ●●●● ●●●● 189"
obfuscate("185052A12345678", FR_SSN_MASK); // "1 ●● ●● ●● ●●● ●●● 78"
IBAN
Expected format: FR76 3000 1007 9412 3456 7890 185
Obfuscated while blurred:
Expected format: FR76 3000 1007 9412 3456 7890 185
onChange value: FR7630006000011234567890189
SSN
Expected format: 1 85 05 78 006 084 91
Obfuscated while blurred:
Expected format: 1 85 05 78 006 084 91
Custom Format String
mask is a string where each character defines what the user can type at that position:
| Char | Matches | Example |
|---|---|---|
9 | Digit [0-9] | 99 99 → 12 34 |
A | Letter [a-zA-Z] | AA → AB |
S | Alphanumeric [a-zA-Z0-9] | SS → A1 |
* | Any non-whitespace character | **** → FR76 |
| other | Inserted as-is (formatting) | 9-9 → 1-2 |
Spaces in the mask are inserted as visual separators: "99 99" formats 1234 as 12 34.
Dynamic Mask Function
For advanced cases, mask also accepts a function. It receives the current unmasked value and { focused }. createMaskArray, imported from "murray", converts a mask format string into the array format it can return. The function must return either:
- A mask array — the component applies formatting (use this when the user is typing)
- A string — used as the display value directly (use this to show a custom representation)
For example, a Spanish DNI (8 digits and a letter) that hides all but the last 4 characters while blurred:
Usage in <Form>
On web, Form.MaskedTextInput stores the raw value, without mask literals, in the Formik values: validate the raw string (length(14) for a SIRET, not length(17)). Errors show once the field is touched.
Accessibility
MaskedTextInput inherits the accessibility features of TextInput:
labelis automatically associated with the input viaInputWrappererrorandhelpTextare announced to screen readers viaaria-describedby/accessibilityHint, error first- Supports all standard input accessibility props (
accessibilityLabel,accessibilityHint, etc.)
Playground
murray and murray/mobile.MaskedTextInput props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| aria-describedby | string | Id of an element outside the input that also describes it, such as an error shared by several inputs | ||
| aria-label | string | Accessibility label, only necessary when not associated with a label | ||
| aria-labelledby | string | |||
| aria-required | boolean | Overrides `aria-required` set by `required`, to mark the input as mandatory without the asterisk | ||
| autoFocus | boolean | If true, focuses the input on componentDidMount.The default value is false. | ||
| cursor | Cursor | Enforce a cursor type | ||
| defaultValue | string | Uncontrolled default value. | ||
| disabled | boolean | Disable input | ||
| enterKeyHint | "search""done""go""next""send" | |||
| error | ReactNode | Contents of `InputError` component, if not set, error is not rendered. | ||
| focused | boolean | 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* | Exclude<ReactNode, undefined> | Expected format with a real example ([WCAG 3.3.2](https://www.w3.org/WAI/WCAG22/Understanding/labels-or-instructions.html)). `null` only when the label already states the format. | ||
| id | string | |||
| label | ReactNode | |||
| mask* | MaskProp | |||
| max | stringnumber | |||
| maxLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| min | stringnumber | |||
| minLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| name | string | Html input name | ||
| onChange | ((value: string) => void) | Trigger when input value is changing | ||
| onPress | ((event: GestureResponderEvent) => void) | |||
| placeholder | string | |||
| prefix | ReactNode | Content section rendered on the left side of the input | ||
| prefixPointerEvents | "none""auto""box-none""box-only" | Sets pointer-events styles on the `prefix` element, `none` by default | ||
| readonly | boolean | Make input readonly | ||
| render | 1 possibilities((elements: { input: ReactNode; prefix?: ReactNode; suffix?: ReactNode; }, pseudoState: PseudoState) => ReactNode) | Fully control how input, prefix and suffix are rendererd | ||
| renderPlaceholder | (() => ReactNode) | To render a custom placeholder view when the field is empty | ||
| required | boolean | 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 | ||
| step | stringnumber | |||
| suffix | ReactNode | Content section rendered on the right side of the input | ||
| suffixPointerEvents | "none""auto""box-none""box-only" | Sets pointer-events styles on the `suffix` element, `none` by default | ||
| sx | PositionProps | Positioning properties | ||
| title | string | Html input title | ||
| type | HTMLInputTypeAttribute | HTML input type | ||
| value | string | Control the input. |
Undocumented props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| autoCapitalize | any | |||
| autoComplete | any | |||
| autoCorrect | any | |||
| clearButtonMode | any | |||
| inputMode | any | |||
| onBlur | any | |||
| onFocus | any | |||
| onKeyPress | any | |||
| onSubmitEditing | any | |||
| ref | any | |||
| returnKeyType | any |
Style API
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: