All components (283)
Masked field
Fields · MaskInput
What it is and when to use it
A text field that shapes what is typed: a phone number, an account code, a reference. It is a complete TextField (label, hint and error) whose value already comes out with the mask applied.
When to use it
- Data with a fixed shape that saves the writer some typing:
999 99 99 99,aa-9999,ES99 9999 9999. maskuses9for a digit,afor a letter and*for either; everything else is written as is. A backslash leaves a symbol as a fixed character (\9).valueanddefaultValueare what was typed without the mask (612345678).onChangegives both:(raw, masked).onCompletefires once when every slot is filled.parseturns what was typed into the value the application uses (a number, a date) andonTypedChangedelivers it, withnullwhile slots are missing or ifparserejects it. WithtypedValueandformatit is controlled from outside with that same value.- Pasting a number with a prefix (
+34 612 345 678) keeps the digits that fit, the last ones. - What is missing is drawn as gaps one digit wide, with the next one in the accent colour: the field does not change width while typing and you can see how much is left.
When not to
- An international phone number with a country prefix:
PhoneField. - An amount:
MoneyFieldorNumberField. - Free text:
TextField.
Keyboard: like a text field. When typing or deleting in the middle, the cursor stays behind the same character. What does not fit in a slot is discarded, and pasting unformatted text leaves it formatted.
Accessibility: it is a TextField: label, hint and error linked. The hint (___ __ __ __) shows the shape but does not replace the label, and if the mask only has digits it asks for the numeric keyboard on mobile.
Fine-tuning: the --amset-field-* variables of the text field.
What it looks like
<MaskInput label="Phone" mask="999 99 99 99" hint="Nine digits, no country code" />Props
Import it with import { MaskInput } from '@amset/ui'. Required props are marked.
| Prop | Type | What it does |
|---|---|---|
labelrequired | string | Required: a field without a visible label does not get into the library. |
maskrequired | string | The shape of what is typed: `9` is a digit, `a` a letter and `*` a digit or a letter. Everything else is written as is: `(999) 99 99 99`. A backslash leaves a symbol as a fixed character (`\9`). Anything in square brackets is optional: `999 999 99[9]` is complete with eight digits and accepts nine. |
back | ReactNode | Content behind the text: a unit ("€"), a copy button. |
backPointerEvents | "auto" | "none" | The same for what goes after (`back`). |
backWidth | string | Width of what goes after (`back`). |
className | string | — |
clearable | boolean | A button to empty the field, shown as soon as something is typed. Pressing it puts the focus back in the input. |
clearLabel | string | What the screen reader says on the clear button. By default, “Clear text”. |
countBy | CountStrategy | How characters are counted: `graphemes` (default, an emoji is one), `characters` or a custom function. |
counter | boolean | Shows "12 / 40" under the field, with the limit of `maxCount` or `maxLength`. |
defaultValue | string | What was typed without the mask, for the initial value when not controlled. |
definitions | Readonly<Record<string, RegExp>> | Your own symbols for the mask, each with the pattern its slot accepts (no `g` flag): with `{ h: /[0-9a-f]/i }`, the mask `hh:hh:hh` asks for a hexadecimal address. |
disabled | boolean | — |
error | string | Error message. When it is passed, the field is marked as invalid. |
errorMessage | (details: NativeValidation) => string | null | undefined | The message when the typed text does not meet the field's NATIVE rules (`minLength`, `maxLength`, `pattern`, `type` `email` or `url`): it receives the text and its `validity` (`tooShort`, `patternMismatch`, `typeMismatch`...) and returns the message, in your language and your words, instead of the browser's bubble. It is checked like `validate`: on leaving and, once it has failed, on every key. `validate` and `error` take precedence over this. |
format | (typed: T) => string | Writes a typed value as text without the mask (`format: (n) => String(n)`): the inverse of `parse`. |
front | ReactNode | Content before the text: an icon, a prefix ("+34"). |
frontPointerEvents | "auto" | "none" | Whether what goes before receives the mouse: `auto` (default) for a button or a link; `none` for an icon or a prefix, and pressing it leads to the field like pressing any other gap of the box. |
frontWidth | string | Width of what goes before (`front`), when it has to be fixed to align several fields: `'3rem'`. |
hiddenLabel | boolean | The label for the screen reader only. For when the field sits inside something that already explains itself (a table cell, a group of filters), never to save space. |
hint | string | Permanent hint text below the field. It does not replace the label. |
inputRef | Ref<HTMLInputElement> | The real `<input>`, to focus it, select its text or measure it from outside. It is separate from `ref` because the component paints a box with label, hint and error, and whoever asks for a ref almost always wants the control. |
invalid | boolean | — |
loading | boolean | Something about this field is being checked or loaded (whether the name is free, the address): a ring inside the box and `aria-busy` on the field. Typing can go on. |
loadingPosition | "start" | "end" | Where the ring goes: `end` (default) or `start`, in the place of the leading icon. |
maxCount | number | A limit that WARNS without cutting: typing can go on, the count turns red, the field is marked invalid and how many are left over is announced. To cut off hard there is `maxLength`. With `maxCount` the counter shows even if not asked for. |
numeric | boolean | Fixed-width digits (amounts, tax IDs, dates). |
onChange | (raw: string, masked: string) => void | Called on every change with what was typed without the mask and with it: `('612345678', '612 34 56 78')`. |
onClear | () => void | Called when the field is emptied with the button, after `onChange('')`. |
onComplete | (raw: string, masked: string) => void | Called once when every slot is filled. |
onTypedChange | (typed: T | null) => void | Called on every change with the typed value that comes out of `parse`, or `null`. |
parse | (raw: string, masked: string) => T | null | Converts what was typed into the value the application uses (a number, a date, an object): it receives what was typed without the mask and with it, and is only called with the mask complete. With `parse`, `onTypedChange` delivers that value, and `null` while slots are missing or if `parse` returns `null` (a date that does not exist). |
placeholder | string | Hint text. Without it, the mask with underscores: `(___) __`. With `''` there is no hint. |
placeholderChar | string | What an empty slot is drawn with in the hint. By default, `_`. |
readOnly | boolean | — |
required | boolean | — |
size | "sm" | "md" | "lg" | — |
success | string | The confirmation that what was typed is valid ("Username available"). An error or a warning take precedence over it. |
typedValue | T | null | The typed value, controlled from outside. `format` is needed to write it back into the field. |
validate | (value: string) => string | null | undefined | Checks what was typed and returns the error message, or nothing if it is valid. It runs when leaving the field and, once it has failed, on every keystroke, so the error goes away as soon as it is fixed. An external `error` takes precedence. |
validationBehavior | "aria" | "native" | `aria` (the default) announces the state without the browser interrupting submission with its bubble; `native` also adds its own validation. |
value | string | What was typed without the mask (`612345678`), when controlled from outside. |
variant | FieldVariant | — |
warning | string | A warning that lets you go on, with its icon and colour, where the error would go. The field is still valid. |
It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.
In the test bench
Every state and variant, to try one at a time, on its page at storybook.amsetui.com:
