Back to Mantine

Mask Input

apps/mantine.dev/src/pages/core/mask-input.mdx

9.5.22.7 KB
Original Source

import { MaskInputDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.MaskInput);

Usage

MaskInput is a wrapper around useMask hook that provides all standard input props (label, description, error, etc.) and supports all mask options. The mask string defines the expected format using token characters (9 for digits, a for letters, etc.).

<Demo data={MaskInputDemos.usage} />

Dynamic mask

Use the modify option to change the mask based on the current input value. This example switches between standard and American Express credit card formats:

<Demo data={MaskInputDemos.dynamic} />

Custom tokens

Override or extend the built-in token map with the tokens option:

<Demo data={MaskInputDemos.customTokens} />

Regex array format

For complex masks where built-in tokens are not enough, pass an array of string literals and RegExp objects:

<Demo data={MaskInputDemos.regex} />

Transform

Use the transform option to convert each character before validation. This example auto-uppercases input so the A token accepts lowercase letters:

<Demo data={MaskInputDemos.transform} />

Disabled state

<Demo data={MaskInputDemos.disabled} />

Error state

<Demo data={MaskInputDemos.error} />

Success state

<Demo data={MaskInputDemos.success} />

Reset value

MaskInput is uncontrolled internally – setting value from a parent will not clear it. Use the resetRef prop to get a function that clears the input value imperatively:

<Demo data={MaskInputDemos.resetRef} />

With use-form

MaskInput is uncontrolled by design – it manages its own DOM value internally. To integrate with use-form, pass the initial value via defaultValue and use the onChangeRaw callback to write the raw (unmasked) value to form state. In uncontrolled form mode, pass { forceUpdate: false } to form.setFieldValue so the input is not remounted on every keystroke:

<Demo data={MaskInputDemos.withUseForm} />

Mask pattern syntax

The mask string defines the expected format. Each character is either a token (editable slot) or a literal (fixed character inserted automatically).

Built-in tokens

  • 9 – any single digit ([0-9])
  • a – any single letter ([A-Za-z])
  • A – any uppercase letter ([A-Z])
  • * – any alphanumeric character ([A-Za-z0-9])
  • # – digit or sign ([-+0-9])

Optional segments

Append ? after the last required character to mark remaining slots as optional:

tsx
<MaskInput mask="(999) 999-9999? x9999" /> // Extension is optional

Escaping

Prefix a token character with \ to treat it as a literal:

tsx
<MaskInput mask="\A999" /> // "A" is literal, not a token