Skip to content

Autocomplete ​

A free-text field with suggestions: the input stays free, picking a suggestion replaces it. Suggestions can be filtered locally or come from a server.

To pick from a closed list, use SelectBox (with searchable).

Usage ​

Basic usage ​

The filter ignores accents and case: "ev" finds "Évreux".

Input: “” · picked: —

vue
<script setup>
import { ref } from 'vue'

const cities = ['Paris', 'Marseille', 'Lyon', 'Évreux']
const city = ref('')
</script>

<template>
  <SuFormField label="City">
    <SuAutocomplete v-model="city" :options="cities" placeholder="City…" clearable @select="onSelect" />
  </SuFormField>
</template>

Detailed suggestions ​

A suggestion can have a value, a description, an icon, or be disabled.

vue
<SuAutocomplete
  v-model="query"
  :options="[
    { label: 'Paris', value: 75, description: 'Île-de-France', icon: MapPinIcon },
    { label: 'Lyon', value: 69, description: 'Auvergne-Rhône-Alpes', icon: MapPinIcon }
  ]"
  open-on-focus
  aria-label="City"
/>

Server suggestions ​

With :filter="false", options are shown as given; loading shows the searching state.

vue
<script setup>
import { ref, watch } from 'vue'

const query = ref('')
const results = ref([])
const loading = ref(false)

watch(query, async (text) => {
  if (text.trim().length < 2) return (results.value = [])
  loading.value = true
  results.value = await api.searchCities(text)
  loading.value = false
})
</script>

<template>
  <SuAutocomplete v-model="query" :options="results" :filter="false" :loading="loading" :min-chars="2" />
</template>

API ​

Props ​

PropTypeDefaultDescription
modelValuestring''Typed text (v-model)
options(string | AutocompleteOption)[][]Suggestions
filterboolean | (option, query) => booleantrueLocal filter; false for pre-filtered suggestions
minCharsnumber0Characters typed before the list opens
openOnFocusbooleanfalseOpens the list on focus
loadingbooleanfalseSearch in progress
clearablebooleanfalseButton and Escape to clear
maxHeightstring'16rem'Maximum list height
size'sm' | 'md' | 'lg''md'Size
state'default' | 'error' | 'success' | 'warning''default'Validation state
disabled / readonly / requiredbooleanfalseField states
name / placeholderstring-Field attributes
noResultsText / loadingText / clearLabelstringlocaleTexts

AutocompleteOption ​

PropertyTypeDescription
labelstringText shown and inserted in the field
valuestring | numberAssociated value (received with select)
descriptionstringSecondary text
iconComponentIcon
disabledbooleanSuggestion that cannot be picked

Events ​

EventPayloadDescription
update:modelValuestringText changed (typing or picking)
selectAutocompleteOptionSuggestion picked
focus / blurFocusEventField focus

Slots ​

SlotPropsDescription
option{ option, query, active }Suggestion rendering

Accessibility ​

  • role="combobox" with aria-autocomplete="list", aria-expanded and aria-controls pointing to the list (role="listbox").
  • Focus stays in the field; the active suggestion is conveyed by aria-activedescendant.
  • Keyboard: ↓ / ↑ open and browse the list (disabled suggestions are skipped), Alt + ↓ opens without activating, Enter picks, Escape closes then clears (clearable), Tab closes and keeps the text.
  • The number of suggestions is announced after a typing pause ("3 suggestions", "No suggestions").
  • In a SuFormField, the field and the list are named by the label.

Publié sous licence MIT.