Skip to content

CdsAutocomplete

An autocomplete is a field for picking from a long list: the user types a query, the list is filtered on the fly and the matches are highlighted. The field is built on CdsTextInput, and the menu and the list are the same as in CdsSelect, so the label, the messages, validation and the menu behave identically.

Storybook GitLab Figma
Test coverage: 83% — partial coverage
API in specs
127 / 127 props, events, slots and exposed members with a test
Test cases
106 unit · 11 browser
Known bugs
12 pinned by an it.fails test
Code coverage
82.5% lines · 82.8% branches measured in v4.0.0-alpha.13
A11y (axe)
3 recommendations in 11 stories measured in v4.0.0-alpha.11

If there are few options and search is not needed, take CdsSelect. If the user may enter values outside the list — CdsCombobox.

Anatomy ​

The first diagram shows the frame of the component: the label, the description, the field and the messages block. The second one shows how the field itself is built: the zone of the selected value, the clear button and the menu indicator; the inner and outer icon zones are the same as in CdsTextInput.

Variations ​

The autocomplete repeats the variations of CdsSelect — grouping, "select all", list and menu settings. Below is what search adds.

List items ​

The list is set by the items prop — an array of strings or of objects with the same interface as in CdsList. Start typing and the list is filtered by item titles, with the matching fragments highlighted.

Search across several fields ​

By default the filter compares the query with the item title. The filter-keys prop widens the search area: title — the title, raw.* — a path to a field of the source object. Your own comparison logic is set by the custom-filter function, and no-filter turns the built-in filtering off entirely — when the results are already filtered by the server, for example.

Auto-selecting the first item ​

The auto-select-first prop highlights the first matching item — Enter or Tab select it without arrow keys or a click. The exact value highlights an item only when the query matches its title completely.

Multiple selection and chips ​

The multiple prop allows selecting several values, chips shows them as CdsTag tags, and closable-chips adds a cross — the value is removed right in the field. clear-on-select clears the search query after every selection, hide-selected removes the already selected items from the list.

Slots ​

The menu and field slots are the same as in CdsSelect and CdsTextInput; for examples of item, selection, chip and the menu slots see the select page — in the autocomplete they work the same way.

Slot nameDescription
itemThe whole list item; receives item, index and props
selectionThe selected value in the field
chipThe chip of a selected value (with chips)
prepend-item / append-itemContent before and after the list
no-dataMenu content when the search result is empty
subheader / dividerGroup subheader and divider
menu-header / menu-footerMenu header and footer; receive search and filteredItems

The no-data slot is seen more often in the autocomplete than in the select: a query is easy to mistype. Bind the query through v-model:search and offer a way out — adding the entered value to the reference book, for example.

Guidelines ​

Scenarios ​

Search on the server ​

When the reference book is too large to load in full, search on the server: v-model:search gives out the query, the response goes into items, and no-filter turns off the repeated filtering on the client. loading shows that a request is in progress, and the no-data slot changes the caption of the empty menu by state: a hint before the input, "Searching…" during the request, "Nothing found" after an empty response — the menu never stays blank.

Picking recipients ​

The combination of multiple, chips, closable-chips and clear-on-select collects the recipients as tags and is immediately ready for the next input. filter-keys searches both by name and by email, hide-selected removes the already added ones from the list, and the item slot shows the avatar and the address — that way it is easier not to confuse namesakes.

Submitting a form ​

The autocomplete takes part in CdsForm validation on a par with text fields: rules will not let the form through without a chosen city. Remember that only the selected item stays as the value — text typed without a selection does not get into the model.

A large reference book ​

The menu renders items through virtual scrolling, so a list of tens of thousands of entries does not slow down either on opening or on filtering. auto-select-first speeds up the input: type the number — press Enter.

API ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Active state of the field
activeColorstring | undefined—Color of the component in its active state
appendIconIconValue | undefined—Icon in the append slot, after the content
appendInnerIconIconValue | undefined—Icon in the inner append slot, after the content of the field
autocompletestring | undefined'off'Helps to influence browser suggestions. The suppress value changes the name attribute of the fields, and off relies on the browser to stop suggesting values on its own. Any other value is passed to the built-in autocomplete of the corresponding element.
autofocusboolean | undefined—Enables autofocus
autoSelectFirstboolean | "exact" | undefined—Highlights the first matching item as you type; Enter or Tab select it. The exact value highlights it only on a full match of the search query with the item title.
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
centerAffixboolean | undefined—Vertically center the appendInner, prependInner, clearIcon and label elements.
chipsboolean | undefined—Shows the selected values as removable chips (CdsTag).
clearableboolean | undefined—Shows the clear button
clearIconIconValue | undefined'remove'Icon of the clear button
clearOnSelectboolean | undefined—Clears the search query after an item is selected (relevant for multiple).
closableChipsboolean | undefined—Adds a remove button to the chips (relevant together with chips).
closeTextstring | undefined'$cds.close'Text for screen readers describing the close action of the menu.
colorstring | undefined—Component color
counterstring | number | boolean | undefined—Character counter. Accepts a boolean, a number or a string
counterValuenumber | ((value: any) => number) | undefined—Custom counter value. Either a number or a function computing it from the current value
customFilterFilterFunction | undefined—Your own value filtering function. Receives the value, the search query and the item, returns a match or -1
customKeyFilterFilterKeyFunctions | undefined—Separate filtering functions for specific item keys — an object of the form { key: function }
descriptionstring | undefined—Description of the component, shown under the label
disabledboolean | null | undefinednullDisabled state
eagerboolean | undefined—Renders the menu content upfront, without waiting for the first opening.
ellipsisboolean | undefined—In multiple mode keeps the selected values on a single line with an ellipsis
errorboolean | undefined—Error state
errorMessagesstring | readonly string[] | null | undefined[]Puts the component into the error state and passes custom error messages. They are merged with any checks defined in the rules prop. This field does not trigger validation.
filterKeysFilterKeys | undefined['title']Item keys the search runs over. By default filtering goes by the item title
filterModeFilterMode | undefined'intersection'How the results of customKeyFilter and of the general filtering are combined: by intersection, by union or by replacement
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
hideDetailsboolean | "auto" | undefined—Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages
hideMessagesboolean | undefined—Hide the messages under the input
hideNoDataboolean | undefined—Hides the menu and the "no data" placeholder when the item list is empty.
hideSelectedboolean | undefined—Hides the already selected items from the menu list.
hintstring | undefined—Hint text shown under the input
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Unique identifier of the component
isSelectAllstring | boolean | undefinedfalseAdds a "select all" entry as the first item of the list (for multiple only). true uses the default $cds.selectAll caption, a string sets your own caption.
itemChildrenSelectItemKeyfalseObject field that holds the child items, or a function that returns them
itemColorstring | undefined—Color of the selected items in the menu list. color is used by default.
itemPropsSelectItemKey'props'Object field that holds the props of the item, or a function that returns them
itemsany[] | undefined[]Array of items to display
itemTitleSelectItemKey'title'Object field used as the item title, or a function that returns it
itemTypeSelectItemKey'type'Object field that defines the item type (item, divider, subheader), or a function that returns it
itemValueSelectItemKey'value'Object field used as the item value, or a function that returns it
labelstring | undefined—Label text of the component
listPropsCdsListProps | undefined—Props passed through to the inner CdsList component.
loadingstring | boolean | undefined—Loading indicator for the component
maxErrorsstring | number | undefined1Maximum number of validation errors shown
maxWidthstring | number | undefined—Maximum component width
menuboolean | undefined—Controls the open state of the dropdown menu (v-model:menu).
menuHeightstring | number | undefined178Maximum height of the dropdown menu.
menuIconIconValue | undefined'chevron-down'Icon indicating that the menu can be expanded.
menuPropsCdsMenuProps | undefined—Props passed through to the inner CdsMenu component.
messagesstring | readonly string[] | undefined[]Messages shown under the input
minWidthstring | number | undefined—Minimum component width
modelModifiersRecord<string, boolean> | undefined—v-model modifiers passed by Vue to process the field value
modelValueanynullValue of the component in v-model. If the component supports the multiple prop, an empty array is used by default.
multipleboolean | undefined—Allows selecting several values. The selected items are shown as chips or as a list.
namestring | undefined—Name of the input
noDataTextstring | undefined'$cds.noDataText'Text shown in the menu when the item list is empty.
noFilterboolean | undefined—Turns off the built-in filtering — the list is shown as is, the search is handled outside
openOnClearboolean | undefined—Opens the menu after the value is cleared.
openTextstring | undefined'$cds.open'Text for screen readers describing the open action of the menu.
persistentClearableboolean | undefined—Always show the clear button, not only on hover or focus
persistentCounterboolean | undefined—Always show the counter, not only while the field is in use
persistentHintboolean | undefined—Always show the hint, not only on focus
persistentPlaceholderboolean | undefined—Always show the placeholder instead of hiding it while the field is in use
placeholderstring | undefined—Placeholder text shown in an empty field
prefixstring | undefined—Text prefix shown before the field value
prependIconIconValue | undefined—Icon in the prepend slot, before the content
prependInnerIconIconValue | undefined—Icon in the inner prepend slot, before the content of the field
readonlyboolean | null | undefinednullRead-only state
returnObjectboolean | undefined—Return an object in the model value instead of an array
reverseboolean | undefined—Reverses the order of the elements inside the field
rolestring | undefined'combobox'ARIA role for the native input element
rulesreadonly ValidationRule[] | undefined[]Accepts an array of items of different types: functions, booleans and strings. Functions take the input value as an argument and must return either true / false, or a string with an error message. The input goes into the error state if a function returns false (or any value in the array is false) or is a string.
searchstring | undefined—Current value of the search query used to filter the items (v-model:search).
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
suffixstring | undefined—Text suffix shown after the field value
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
transitionstring | boolean | (TransitionProps & { component?: Component | undefined; }) | { component: Component; } | null | undefined{ component: CdsModalTransition }Transition animation
typestring | undefined'text'Type of the native input
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
valueComparatorValueComparator | undefined—Function that compares item values to determine the selected state
widthstring | number | undefined—Component width

Events

NameTypeDescription
update:focused[focused: boolean]The focus state of the component changed (v-model:focused).
update:menu[value: boolean]The open state of the dropdown menu changed (v-model:menu).
update:modelValue[value: any]The selected value changed (v-model).
update:search[value: string]The search query changed (v-model:search).
update:selectedAll[value: boolean]The "all selected" state changed (for multiple + isSelectAll only).

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
append-innerCdsInputDefaultSlotSlot added at the end of the input
append-itemanyContent after the item list in the menu.
chip{ item: any; internalItem: ListItem<any>; index: number; props: Record<string, unknown>; }Custom rendering of the chip of a selected value (relevant together with chips).
clearCdsInputDefaultSlot & { props: Record<string, any>; }Slot for a custom clear icon (shown when the clearable prop is true).
description{ description: string | undefined; props: Record<string, any>; }Default slot of the CdsDescription component
detailsCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
divider{ props: Record<string, unknown>; index: number; }Custom rendering of a divider in the menu list.
item{ item: any; internalItem: ListItem<any>; index: number; props: Record<string, unknown>; }Custom rendering of an item of the menu list.
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component
loaderLoaderSlotPropsSlot for a custom loader (shown when the loading prop is true).
menu-footer{ search: Ref<string | undefined, string | undefined>; filteredItems: ListItem<any>[]; }Content of the menu footer. Receives the current search query and the filtered items.
menu-header{ search: Ref<string | undefined, string | undefined>; filteredItems: ListItem<any>[]; }Content of the menu header. Receives the current search query and the filtered items.
messageCdsMessageSlotSlot for customizing the content of a message
messagesCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
no-dataanyContent shown in the menu when the item list is empty.
prependCdsInputSlotAdds an element before the input
prepend-innerCdsInputDefaultSlotSlot added at the start of the input
prepend-itemanyContent before the item list in the menu.
selection{ item: any; internalItem: ListItem<any>; index: number; }Custom rendering of the selected value inside the field.
subheader{ props: Record<string, unknown>; index: number; }Custom rendering of a group subheader in the menu list.

Exposed

NameTypeDescription
controlRefHTMLElement | undefinedRef to the control of the input
fieldIconColorstring | undefinedIcon color
filteredItemsListItem<any>[]Items filtered by the current search query.
isDirtybooleanWhether the input is dirty
isDisabledbooleanWhether the input is disabled
isFocusedbooleanWhether the component is focused.
isPurebooleanWhether the search query has not changed since the last selection.
isReadonlybooleanWhether the input is read-only
isValidboolean | nullWhether the input is valid
isValidatingbooleanWhether the input is being validated
reset() => Promise<void>Resets the input to its initial state
resetValidation() => Promise<void>Resets the validation of the input
selectAll() => voidToggle the selection of all available items (for multiple + isSelectAll only).
validate(silent?: boolean) => Promise<string[]>Validates the input