Skip to content
EspañolEnglish
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.
  • mask uses 9 for a digit, a for a letter and * for either; everything else is written as is. A backslash leaves a symbol as a fixed character (\9).
  • value and defaultValue are what was typed without the mask (612345678). onChange gives both: (raw, masked). onComplete fires once when every slot is filled.
  • parse turns what was typed into the value the application uses (a number, a date) and onTypedChange delivers it, with null while slots are missing or if parse rejects it. With typedValue and format it 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: MoneyField or NumberField.
  • 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

Nine digits, no country code

<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.

PropTypeWhat it does
labelrequiredstringRequired: a field without a visible label does not get into the library.
maskrequiredstringThe 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.
backReactNodeContent behind the text: a unit ("€"), a copy button.
backPointerEvents"auto" | "none"The same for what goes after (`back`).
backWidthstringWidth of what goes after (`back`).
classNamestring—
clearablebooleanA button to empty the field, shown as soon as something is typed. Pressing it puts the focus back in the input.
clearLabelstringWhat the screen reader says on the clear button. By default, “Clear text”.
countByCountStrategyHow characters are counted: `graphemes` (default, an emoji is one), `characters` or a custom function.
counterbooleanShows "12 / 40" under the field, with the limit of `maxCount` or `maxLength`.
defaultValuestringWhat was typed without the mask, for the initial value when not controlled.
definitionsReadonly<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.
disabledboolean—
errorstringError message. When it is passed, the field is marked as invalid.
errorMessage(details: NativeValidation) => string | null | undefinedThe 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) => stringWrites a typed value as text without the mask (`format: (n) => String(n)`): the inverse of `parse`.
frontReactNodeContent 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.
frontWidthstringWidth of what goes before (`front`), when it has to be fixed to align several fields: `'3rem'`.
hiddenLabelbooleanThe 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.
hintstringPermanent hint text below the field. It does not replace the label.
inputRefRef<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.
invalidboolean—
loadingbooleanSomething 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.
maxCountnumberA 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.
numericbooleanFixed-width digits (amounts, tax IDs, dates).
onChange(raw: string, masked: string) => voidCalled on every change with what was typed without the mask and with it: `('612345678', '612 34 56 78')`.
onClear() => voidCalled when the field is emptied with the button, after `onChange('')`.
onComplete(raw: string, masked: string) => voidCalled once when every slot is filled.
onTypedChange(typed: T | null) => voidCalled on every change with the typed value that comes out of `parse`, or `null`.
parse(raw: string, masked: string) => T | nullConverts 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).
placeholderstringHint text. Without it, the mask with underscores: `(___) __`. With `''` there is no hint.
placeholderCharstringWhat an empty slot is drawn with in the hint. By default, `_`.
readOnlyboolean—
requiredboolean—
size"sm" | "md" | "lg"—
successstringThe confirmation that what was typed is valid ("Username available"). An error or a warning take precedence over it.
typedValueT | nullThe typed value, controlled from outside. `format` is needed to write it back into the field.
validate(value: string) => string | null | undefinedChecks 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.
valuestringWhat was typed without the mask (`612345678`), when controlled from outside.
variantFieldVariant—
warningstringA 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: