Autocomplete
NewA text field that suggests matching options as you type.
Preview
Installation
npx sigma-ui@latest add autocompleteUsage
<script setup lang="ts">
import {
Autocomplete,
AutocompleteAnchor,
AutocompleteEmpty,
AutocompleteInput,
AutocompleteItem,
AutocompleteList,
} from '@/components/ui/autocomplete';
</script>
<template>
<Autocomplete v-model="value" open-on-focus>
<AutocompleteAnchor>
<AutocompleteInput placeholder="Search fruit..." />
</AutocompleteAnchor>
<AutocompleteList>
<AutocompleteEmpty>No fruit found.</AutocompleteEmpty>
<AutocompleteItem value="Apple">
Apple
</AutocompleteItem>
</AutocompleteList>
</Autocomplete>
</template>v-model is the input text. Items stay visible while their value matches that text. Set open-on-focus to show the list when the field is focused.
Autocomplete or Combobox
Use Autocomplete when any text is a valid answer and the options are only suggestions, such as a search field or a city name. The value is the typed string. Use Combobox when the value must be one of the options. There, typing only filters the list.
Examples
Fruits
API Reference
Autocomplete
Props
| Prop | Type | Default |
|---|---|---|
modelValueThe controlled value of the Autocomplete (the input text). Can be bound with | string | - |
defaultValueThe value of the autocomplete when initially rendered. Use when you do not need to control the state. | string | - |
openThe controlled open state of the Autocomplete. Can be bound with | boolean | - |
defaultOpenThe open state of the autocomplete when it is initially rendered. Use when you do not need to control its open state. | boolean | - |
disabledWhen | boolean | - |
dirThe reading direction of the autocomplete when applicable. | Direction | - |
nameThe name of the field. Submitted with its owning form as part of a name/value pair. | string | - |
requiredWhen | boolean | - |
resetSearchTermOnBlurWhether to reset the searchTerm when the Autocomplete input blurred | boolean | `false` |
openOnFocusWhether to open the autocomplete when the input is focused | boolean | `false` |
openOnClickWhether to open the autocomplete when the input is clicked | boolean | `false` |
ignoreFilterWhen | boolean | - |
highlightOnHoverWhen | boolean | - |
loopWhen | boolean | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Events
| Event | Payload |
|---|---|
update:modelValueEvent handler called when the value changes. | [value: string] |
update:openEvent handler called when the open state of the autocomplete changes. | [value: boolean] |
highlightEvent handler when highlighted element changes. | [payload: { ref: HTMLElement; value: string; }] |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
AutocompleteAnchor
Props
| Prop | Type | Default |
|---|---|---|
referenceThe reference (or anchor) element that is being referred to for positioning. If not provided will use the current component as anchor. | ReferenceElement | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
AutocompleteEmpty
Props
| Prop | Type | Default |
|---|---|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
AutocompleteInput
Props
| Prop | Type | Default |
|---|---|---|
modelValueThe controlled value of the filter. Can be binded with v-model. | string | - |
autoFocusFocus on element when mounted. | boolean | - |
disabledWhen | boolean | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Events
| Event | Payload |
|---|---|
update:modelValue | [string] |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
AutocompleteItem
Props
| Prop | Type | Default |
|---|---|---|
textValueA string representation of the item contents. If the children are not plain text, then the | string | - |
valuerequiredThe value given as data when submitted with a | AcceptableValue | - |
disabledWhen | boolean | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Events
| Event | Payload |
|---|---|
selectEvent handler called when the selecting item. <br> It can be prevented by calling | [event: SelectEvent$3<AcceptableValue>] |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
AutocompleteList
Props
| Prop | Type | Default |
|---|---|---|
forceMountUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | boolean | - |
positionThe positioning mode to use, <br>
| "popper" | "inline" | "popper" |
bodyLockThe document.body will be lock, and scrolling will be disabled. | boolean | - |
hideWhenEmptyWhen | boolean | false |
memoDependenciesReactive dependencies that should invalidate the memoized content subtree. | unknown[] | - |
sideThe preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled. | "top" | "right" | "bottom" | "left" | "bottom" |
sideOffsetThe distance in pixels from the trigger. | number | 4 |
sideFlipFlip to the opposite side when colliding with boundary. | boolean | true |
alignThe preferred alignment against the trigger. May change when collisions occur. | "center" | "start" | "end" | "center" |
alignOffsetAn offset in pixels from the | number | 0 |
alignFlipFlip alignment when colliding with boundary.
May only occur when | boolean | true |
avoidCollisionsWhen | boolean | true |
collisionBoundaryThe element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check. | Element | (Element | null)[] | null | [] |
collisionPaddingThe distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 |
arrowPaddingThe padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. | number | 0 |
hideShiftedArrowWhen | boolean | true |
stickyThe sticky behavior on the align axis. | "partial" | "always" | "partial" |
hideWhenDetachedWhether to hide the content when the trigger becomes fully occluded. | boolean | false |
positionStrategyThe type of CSS position property to use. | "absolute" | "fixed" | - |
updatePositionStrategyStrategy to update the position of the floating element on every animation frame. | "always" | "optimized" | 'optimized' |
disableUpdateOnLayoutShiftWhether to disable the update position for the content when the layout shifted. | boolean | false |
prioritizePositionForce content to be position within the viewport. Might overlap the reference element, which may not be desired. | boolean | false |
referenceThe custom element or virtual element that will be set as the reference to position the floating element. If provided, it will replace the default anchor element. | ReferenceElement | - |
dirThe reading direction of the popper content when applicable. <br> If omitted, inherits globally from | Direction | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
disableOutsidePointerEventsWhen | boolean | - |
Events
| Event | Payload |
|---|---|
escapeKeyDownEvent handler called when the escape key is down. Can be prevented. | [event: KeyboardEvent] |
pointerDownOutsideEvent handler called when a | [event: PointerDownOutsideEvent] |
focusOutsideEvent handler called when the focus moves outside of the | [event: FocusOutsideEvent] |
interactOutsideEvent handler called when an interaction happens outside the | [event: PointerDownOutsideEvent | FocusOutsideEvent] |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
Accessibility
The field is a combobox. The list is a listbox, and the highlighted option follows the arrow keys.
| Key | Description |
|---|---|
| ArrowDown or ArrowUp | Moves the highlight through the matching options. |
| Enter | Sets the input to the highlighted option. |
| Esc | Closes the list. |