Skip to content

Autocomplete

Provides input suggestions based on input content.

Basic Usage

Use the fetch-suggestions prop to set the method for retrieving the suggestion list.

Basic Usage

Custom Template

Use the default slot to customize the display content of suggestion items.

Custom Template

Simulate fetching suggestion data from a server with delay.

Remote Search

Disabled State

Use the disabled prop to disable the input.

Disabled

Clearable

Use the clearable prop to quickly clear the content.

Clearable

Different Sizes

Supports large, default, and small sizes.

Different Sizes

Custom Slots

Use loading and empty slots to customize the content during loading and when there is no data.

Custom Slots

Use in Nuxt

The Autocomplete component works well in Nuxt 3/4 environments. With auto-import support, you can simply use the component name directly.

Use in Nuxt

SSR Notes:

  • ✅ Initial state of the input (including v-model value) renders correctly on the server
  • ✅ Size and placeholder support SSR
  • ✅ Prefix and suffix slots generate HTML during the SSR phase
  • ⚠️ The suggestion dropdown only appears during client-side interaction (input or focus), without affecting the initial HTML structure
  • 💡 Debounce and dropdown positioning take effect after client-side activation (Hydration)

SSR Safety

The Autocomplete component internally wraps YhInput and the Popper system. It uses useId to ensure stability of the Input-to-dropdown association IDs during initial rendering, perfectly avoiding common ID conflict warnings under SSR.

API

Props

PropDescriptionTypeDefault
model-value / v-modelBinding valuestring
placeholderPlaceholder textstring
disabledWhether disabledbooleanfalse
clearableWhether clearablebooleanfalse
sizeInput size'large' | 'default' | 'small''default'
fetch-suggestionsMethod to fetch suggestions(query: string, callback: (suggestions: any[]) => void) => void
trigger-on-focusWhether to trigger suggestions on focusbooleantrue
debounceDebounce delay (ms)number300
placementDropdown position'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end''bottom-start'
value-keyKey name used for display in suggestion objectsstring'value'
highlight-first-itemWhether to highlight the first item by defaultbooleanfalse
teleportedWhether to append the dropdown to bodybooleantrue
fit-input-widthWhether the dropdown width matches the inputbooleantrue
prefix-iconPrefix iconstring | Component
suffix-iconSuffix iconstring | Component
nameNative name attributestring
autofocusAuto focusbooleanfalse
autocompleteNative autocomplete attributestring'off'

Events

Event NameDescriptionParameters
inputTriggered when input value changes(value: string) => void
changeTriggered when value changes(value: string) => void
focusTriggered on focus(event: FocusEvent) => void
blurTriggered on blur(event: FocusEvent) => void
clearTriggered when clear button is clicked() => void
selectTriggered when a suggestion item is selected(item: any) => void

Slots

Slot NameDescriptionScope
defaultCustom suggestion item content{ item: any }
prefixInput prefix content
suffixInput suffix content
prependInput prepend content
appendInput append content
loadingContent while loading
emptyContent when no data matches

Expose

NameDescriptionType
focusFocus the input() => void
blurBlur the input() => void
closeClose the suggestion list() => void
highlightHighlight a specified item(index: number) => void

Theme Variables

The Autocomplete component uses the following CSS variables. You can customize styles by overriding these variables:

VariableDescriptionDefault
--yh-autocomplete-border-colorBorder colorvar(--yh-border-color)
--yh-autocomplete-hover-border-colorHover border colorvar(--yh-border-color-hover)
--yh-autocomplete-focus-border-colorFocus border colorvar(--yh-color-primary)
--yh-autocomplete-disabled-bg-colorDisabled background colorvar(--yh-fill-color-light)

Released under the MIT License.