Vitral 0.2.18
Formulário

InputMask

Uma caixa de texto em que se digita dentro de um padrão, como um telefone ou uma data, com os literais preenchidos para você. InputText e AutoComplete aceitam as mesmas máscaras.

Importação

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

Básico

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>

Valor sem máscara

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>

Um padrão que acompanha o valor

Uma lista mostra o primeiro padrão com espaço para o que foi digitado: o CPF vira CNPJ no décimo segundo dígito, e volta quando um é apagado. Uma função recebe os caracteres digitados até ali e devolve o padrão.

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>

Máscaras como valores

Um objeto de máscara leva o padrão com as suas configurações, então é definido uma vez e dá a mesma caixa no InputMask, no InputText e no AutoComplete. Uma configuração no campo vale mais que a do objeto.

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>

Caracteres de posição e partes opcionais

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

Tamanhos e estados

<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

Lido de packages/vue/src/components/InputMask/types.ts, então diz o que o componente aceita de fato.

Props

NomeTipoDescrição
size Size—
variant InputVariantO padrão é o `inputVariant` do plugin.
invalid boolean—
disabled boolean—
readonly boolean—
fluid boolean—
mask MaskPropO padrão: `9` aceita um dígito, `a` uma letra, `*` qualquer um; tudo depois de `?` é opcional; qualquer outro caractere é digitado pelo leitor. Uma lista de padrões deixa o valor ter mais de uma forma: aparece o primeiro com espaço para o que foi digitado, então `['999.999.999-99', '99.999.999/9999-99']` é um CPF que vira CNPJ no décimo segundo dígito, e volta quando um é apagado. Uma função recebe os caracteres digitados até ali, sem os literais, e devolve o padrão a mostrar. Um objeto leva o padrão com as suas configurações, de modo que uma máscara pode ser definida uma vez e passada a qualquer campo.
slotChar stringO que uma posição vazia mostra: um caractere, ou uma string do tamanho da máscara (`'mm/dd/yyyy'`). O padrão é `_`.
autoClear booleanLimpa um valor incompleto quando a caixa perde o foco. O padrão é true.
unmask booleanO v-model recebe só os caracteres digitados, sem os literais.
definitions MaskDefinitionsCaracteres de posição extras, cada um com os caracteres que aceita.

Mais pt, dt e unstyled de BaseProps; veja pass-through e modo sem estilo.

Emits

EventoPayloadDescrição
complete event: { originalEvent: Event; value: string }Todas as posições obrigatórias estão preenchidas.
focus event: FocusEvent—
blur event: FocusEvent—