Vitral 0.2.18
Form

AutoComplete

A text box that suggests as you type, with suggestions supplied by your app, single or multiple values as chips, and a button for the whole list.

Import

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

Basic

Value: null
<script setup lang="ts">import { AutoComplete, FilterService, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; interface Country {    name: string;    code: string;    region: string;} const countries: Country[] = [    { name: 'Angola', code: 'AO', region: 'Africa' },    { name: 'Argentina', code: 'AR', region: 'Americas' },    { name: 'Brazil', code: 'BR', region: 'Americas' },    { name: 'Cabo Verde', code: 'CV', region: 'Africa' },    { name: 'Canada', code: 'CA', region: 'Americas' },    { name: 'Chile', code: 'CL', region: 'Americas' },    { name: 'Colombia', code: 'CO', region: 'Americas' },    { name: 'France', code: 'FR', region: 'Europe' },    { name: 'Germany', code: 'DE', region: 'Europe' },    { name: 'Moçambique', code: 'MZ', region: 'Africa' },    { name: 'Peru', code: 'PE', region: 'Americas' },    { name: 'Portugal', code: 'PT', region: 'Europe' },    { name: 'Spain', code: 'ES', region: 'Europe' },    { name: 'São Tomé and Príncipe', code: 'ST', region: 'Africa' },    { name: 'Uruguay', code: 'UY', region: 'Americas' }]; const match = (query: string) => countries.filter((c) => FilterService.matches(c.name, query, 'contains')); const single = ref<Country | string | null>(null);const suggestions = ref<Country[]>([]);</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 16rem">        <Label for="ac-country">Country</Label>        <AutoComplete            id="ac-country"            v-model="single"            :suggestions="suggestions"            option-label="name"            placeholder="Type a country"            @complete="suggestions = match($event.query)"        />        <small style="color: var(--vt-text-muted-color)">Value: {{ typeof single === 'string' ? `"${single}"` : (single?.code ?? 'null') }}</small>    </StackPanel></template>

Multiple

<script setup lang="ts">import { AutoComplete, FilterService, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; interface Country {    name: string;    code: string;    region: string;} const countries: Country[] = [    { name: 'Angola', code: 'AO', region: 'Africa' },    { name: 'Argentina', code: 'AR', region: 'Americas' },    { name: 'Brazil', code: 'BR', region: 'Americas' },    { name: 'Cabo Verde', code: 'CV', region: 'Africa' },    { name: 'Canada', code: 'CA', region: 'Americas' },    { name: 'Chile', code: 'CL', region: 'Americas' },    { name: 'Colombia', code: 'CO', region: 'Americas' },    { name: 'France', code: 'FR', region: 'Europe' },    { name: 'Germany', code: 'DE', region: 'Europe' },    { name: 'Moçambique', code: 'MZ', region: 'Africa' },    { name: 'Peru', code: 'PE', region: 'Americas' },    { name: 'Portugal', code: 'PT', region: 'Europe' },    { name: 'Spain', code: 'ES', region: 'Europe' },    { name: 'São Tomé and Príncipe', code: 'ST', region: 'Africa' },    { name: 'Uruguay', code: 'UY', region: 'Americas' }]; const match = (query: string) => countries.filter((c) => FilterService.matches(c.name, query, 'contains')); const many = ref<Country[]>([]);const suggestions = ref<Country[]>([]);</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 22rem">        <Label for="ac-many">Countries</Label>        <AutoComplete id="ac-many" v-model="many" :suggestions="suggestions" option-label="name" multiple fluid @complete="suggestions = match($event.query)" />    </StackPanel></template>

With a mask

The query is typed into a mask, and with `unmask` it is only the digits. A chosen suggestion shows its label; typing again reads it into the mask.

Value: null
<script setup lang="ts">import { AutoComplete, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; interface Customer {    name: string;    cpf: string;} const customers: Customer[] = [    { name: 'Ana Souza', cpf: '12345678901' },    { name: 'Bruno Lima', cpf: '12398765400' },    { name: 'Carla Dias', cpf: '98765432100' }]; const customer = ref<Customer | string | null>(null);const suggestions = ref<Customer[]>([]);</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 16rem">        <Label for="ac-cpf">Customer, by CPF</Label>        <AutoComplete            id="ac-cpf"            v-model="customer"            :suggestions="suggestions"            mask="999.999.999-99"            unmask            :auto-clear="false"            option-label="name"            @complete="suggestions = customers.filter((c) => c.cpf.startsWith($event.query))"        >            <template #option="{ option }">{{ (option as Customer).name }} · {{ (option as Customer).cpf }}</template>        </AutoComplete>        <small style="color: var(--vt-text-muted-color)">Value: {{ typeof customer === 'string' ? `"${customer}"` : (customer?.name ?? 'null') }}</small>    </StackPanel></template>

Groups and templates

<script setup lang="ts">import { AutoComplete, FilterService, Label, StackPanel } from '@vitral/vue';import { ref } from 'vue'; interface Country {    name: string;    code: string;    region: string;} const countries: Country[] = [    { name: 'Angola', code: 'AO', region: 'Africa' },    { name: 'Argentina', code: 'AR', region: 'Americas' },    { name: 'Brazil', code: 'BR', region: 'Americas' },    { name: 'Cabo Verde', code: 'CV', region: 'Africa' },    { name: 'Canada', code: 'CA', region: 'Americas' },    { name: 'Chile', code: 'CL', region: 'Americas' },    { name: 'Colombia', code: 'CO', region: 'Americas' },    { name: 'France', code: 'FR', region: 'Europe' },    { name: 'Germany', code: 'DE', region: 'Europe' },    { name: 'Moçambique', code: 'MZ', region: 'Africa' },    { name: 'Peru', code: 'PE', region: 'Americas' },    { name: 'Portugal', code: 'PT', region: 'Europe' },    { name: 'Spain', code: 'ES', region: 'Europe' },    { name: 'São Tomé and Príncipe', code: 'ST', region: 'Africa' },    { name: 'Uruguay', code: 'UY', region: 'Americas' }]; const match = (query: string) => countries.filter((c) => FilterService.matches(c.name, query, 'contains')); const grouped = ref<Country | null>(null);const groupedSuggestions = ref<{ region: string; items: Country[] }[]>([]);function searchGrouped(query: string) {    const hits = match(query);    groupedSuggestions.value = ['Africa', 'Americas', 'Europe']        .map((region) => ({ region, items: hits.filter((c) => c.region === region) }))        .filter((g) => g.items.length);}</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 16rem">        <Label for="ac-grouped">Country by region</Label>        <AutoComplete            id="ac-grouped"            v-model="grouped"            :suggestions="groupedSuggestions"            option-label="name"            option-group-label="region"            auto-option-focus            @complete="searchGrouped($event.query)"        >            <template #option="{ option }">                <span style="flex: 1">{{ (option as Country).name }}</span>                <small style="color: var(--vt-text-muted-color)">{{ (option as Country).code }}</small>            </template>        </AutoComplete>    </StackPanel></template>

Plain strings, sizes and states

<script setup lang="ts">import { AutoComplete } from '@vitral/vue';import { ref } from 'vue'; const words = ref<string | null>(null);const wordSuggestions = ref<string[]>([]);</script> <template>    <AutoComplete        v-model="words"        :suggestions="wordSuggestions"        aria-label="Word"        size="small"        @complete="wordSuggestions = ['alpha', 'beta', 'gamma', 'delta'].filter((w) => w.includes($event.query))"    />    <AutoComplete :suggestions="[]" aria-label="Filled" variant="filled" />    <AutoComplete :suggestions="[]" aria-label="Loading" loading />    <AutoComplete :suggestions="[]" aria-label="Invalid" invalid />    <AutoComplete :suggestions="[]" aria-label="Disabled" disabled model-value="Disabled" dropdown /></template>

API

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

Props

NameTypeDescription
suggestions any[]What the list offers for the current query; the app sets it in answer to `complete`.
optionLabel stringField (dotted path) holding a suggestion's text. Without it, suggestions are shown as they are.
optionDisabled string—
optionGroupLabel stringTurns `suggestions` into groups: this field is each group's label…
optionGroupChildren string…and this one its suggestions. Defaults to `items`.
dataKey stringCompare object values by this field instead of structurally.
multiple booleanv-model becomes an array, shown as chips in the field.
dropdown booleanAdds a button that asks for the whole list.
dropdownMode 'blank' | 'current'What the button asks for: every suggestion (`'blank'`, the default) or those matching the current text.
forceSelection booleanOnly a chosen suggestion may stay in the box; other text is cleared when it loses focus.
delay numberMilliseconds to wait after typing before asking. Defaults to 300.
minLength numberCharacters to type before asking. Defaults to 1.
completeOnFocus booleanAsk as soon as the box takes focus.
autoOptionFocus booleanPut the first suggestion in focus when the list opens, so Enter takes it.
showClear booleanShows a clear button while there is a value.
loading booleanShows a spinner, for while the app is searching.
showEmptyMessage booleanSay so when nothing matches, rather than keeping the list closed. Defaults to true.
emptySearchMessage string—
size Size—
variant InputVariantDefaults to the plugin's `inputVariant`.
invalid boolean—
disabled boolean—
readonly boolean—
fluid boolean—
placement OverlayPlacement—
appendTo string`'body'` (the default), `'self'` to render in place, or a selector.
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.
scrollbar ScrollbarModeThe bars of the boxes this component scrolls inside — `'hover'`, `'always'` or `'native'`. The application's `scrollbar` option by default, which is `'hover'`.

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

Emits

EventPayloadDescription
complete event: AutoCompleteCompleteEventAsks the app for the suggestions matching `query`.
option-select event: AutoCompleteOptionEvent—
option-unselect event: AutoCompleteOptionEvent—
dropdown-click event: AutoCompleteCompleteEvent—
clear ——
show ——
hide ——
focus event: FocusEvent—
blur event: FocusEvent—

Slots

NameSlot propsDescription
option (props: { option: unknown; index: number; selected: boolean; focused: boolean })—
optiongroup (props: { group: unknown })—
chip (props: { value: unknown; label: string; remove: (event: Event) => void })A chip's content in multiple mode.
header ——
footer ——
empty ——
dropdownicon ——