Vitral 0.2.18
Form

InputMask

A text box that types into a pattern, such as a phone or a date, filling in the literals for you. InputText and AutoComplete take the same masks.

Import

main.ts
import { InputMask } from '@vitral/vue';

Basic

Value: —
<script setup lang="ts">import { InputMask, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const phone = ref('');const done = ref(false);</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 16rem">        <Label for="mask-phone">Phone</Label>        <InputMask id="mask-phone" v-model="phone" mask="(999) 999-9999" placeholder="(999) 999-9999" autocomplete="tel-national" @complete="done = true" />        <small style="color: var(--vt-text-muted-color)">Value: {{ phone || '—' }} {{ done ? '· complete' : '' }}</small>    </StackPanel></template>

Unmasked value

v-model: —
<script setup lang="ts">import { InputMask, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const cpf = ref('');</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 16rem">        <Label for="mask-cpf">CPF</Label>        <InputMask id="mask-cpf" v-model="cpf" mask="999.999.999-99" unmask />        <small style="color: var(--vt-text-muted-color)">v-model: {{ cpf || '—' }}</small>    </StackPanel></template>

A pattern that follows the value

A list shows the first pattern with room for what has been typed: the CPF turns into a CNPJ at the twelfth digit, and back when one is deleted. A function receives the characters typed so far and returns the pattern.

Value: —
Value: —
<script setup lang="ts">import { InputMask, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const taxId = ref('');const phone = ref('');// Landlines have eight digits after the area code, mobiles nine.const phoneMask = (raw: string) => (raw.length > 10 ? '(99) 99999-9999' : '(99) 9999-9999?9');</script> <template>    <StackPanel orientation="horizontal" spacing="1.5rem" wrap>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-taxid">CPF or CNPJ</Label>            <InputMask id="mask-taxid" v-model="taxId" :mask="['999.999.999-99', '99.999.999/9999-99']" unmask />            <small style="color: var(--vt-text-muted-color)">Value: {{ taxId || '—' }}</small>        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-br-phone">Phone</Label>            <InputMask id="mask-br-phone" v-model="phone" :mask="phoneMask" autocomplete="tel-national" />            <small style="color: var(--vt-text-muted-color)">Value: {{ phone || '—' }}</small>        </StackPanel>    </StackPanel></template>

Masks as values

A mask object carries the pattern with its settings, so it is defined once and gives the same box in InputMask, InputText and AutoComplete. A setting on the field wins over the one in the object.

Value: —
Value: —
Value: —
<script setup lang="ts">import { InputMask, InputText, Label, StackPanel, type MaskOptions } from '@vitral/vue';import { ref } from 'vue'; // Defined once, used by any field that takes a mask.const employeeId: MaskOptions = { pattern: 'aa-9999', case: 'upper', unmask: true }; const inMask = ref('');const inText = ref('');const kept = ref('');</script> <template>    <StackPanel orientation="horizontal" spacing="0.75rem" align="start" wrap>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-employee">Employee, in InputMask</Label>            <InputMask id="mask-employee" v-model="inMask" :mask="employeeId" />            <small style="color: var(--vt-text-muted-color)">Value: {{ inMask || '—' }}</small>        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-employee-text">The same mask, in InputText</Label>            <InputText id="mask-employee-text" v-model="inText" :mask="employeeId" clearable />            <small style="color: var(--vt-text-muted-color)">Value: {{ inText || '—' }}</small>        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-employee-kept">The field's setting wins</Label>            <InputMask id="mask-employee-kept" v-model="kept" :mask="employeeId" :unmask="false" />            <small style="color: var(--vt-text-muted-color)">Value: {{ kept || '—' }}</small>        </StackPanel>    </StackPanel></template>

Slot characters and optional parts

<script setup lang="ts">import { InputMask, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const date = ref('');const serial = ref('');</script> <template>    <StackPanel orientation="horizontal" spacing="0.75rem" align="start" wrap>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-date">Date</Label>            <InputMask id="mask-date" v-model="date" mask="99/99/9999" slot-char="mm/dd/yyyy" />        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <Label for="mask-serial">Serial (last two optional)</Label>            <InputMask id="mask-serial" v-model="serial" mask="aa-9999?-**" :auto-clear="false" />        </StackPanel>    </StackPanel></template>

Sizes and states

<script setup lang="ts">import { InputMask } from '@vitral/vue';</script> <template>    <InputMask mask="999" size="small" aria-label="Small" />    <InputMask mask="999" variant="filled" aria-label="Filled" />    <InputMask mask="999" invalid aria-label="Invalid" />    <InputMask mask="999" disabled model-value="123" aria-label="Disabled" /></template>

API

Read from packages/vue/src/components/InputMask/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
size Size—
variant InputVariantDefaults to the plugin's `inputVariant`.
invalid boolean—
disabled boolean—
readonly boolean—
fluid boolean—
mask MaskPropThe pattern: `9` takes a digit, `a` a letter, `*` either; anything after `?` is optional; every other character is typed for the reader. A list of patterns lets the value take more than one shape: the first with room for what has been typed is shown, so `['999.999.999-99', '99.999.999/9999-99']` is a CPF that turns into a CNPJ at the twelfth digit, and back again when one is deleted. A function receives the characters typed so far, without literals, and returns the pattern to show. An object carries the pattern with its settings, so one mask can be defined once and passed to any field.
slotChar stringWhat an empty slot shows: one character, or a string as long as the mask (`'mm/dd/yyyy'`). Defaults to `_`.
autoClear booleanClear an unfinished value when the box loses focus. Defaults to true.
unmask booleanv-model receives only the typed characters, without the literals.
definitions MaskDefinitionsExtra slot characters, each with the characters it accepts.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
complete event: { originalEvent: Event; value: string }Every required slot is filled.
focus event: FocusEvent—
blur event: FocusEvent—