Back to Ant Design

AutoComplete

components/auto-complete/index.en-US.md

6.3.77.6 KB
Original Source

When To Use

  • When you need an input box instead of a selector.
  • When you need input suggestions or helping text.

The differences with Select are:

  • AutoComplete is an input box with text hints, and users can type freely. The keyword is aiding input.
  • Select is selecting among given choices. The keyword is select.

Examples

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">Basic Usage</code> <code src="./demo/options.tsx">Customized</code> <code src="./demo/custom.tsx">Customize Input Component</code> <code src="./demo/non-case-sensitive.tsx">Non-case-sensitive AutoComplete</code> <code src="./demo/certain-category.tsx">Lookup-Patterns - Certain Category</code> <code src="./demo/uncertain-category.tsx">Lookup-Patterns - Uncertain Category</code> <code src="./demo/status.tsx">Status</code> <code src="./demo/variant.tsx" version="5.13.0">Variants</code> <code src="./demo/allowClear.tsx">Customize clear button</code> <code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code> <code src="./demo/form-debug.tsx" debug>Debug in Form</code> <code src="./demo/AutoComplete-and-Select.tsx" debug>AutoComplete and Select</code> <code src="./demo/render-panel.tsx" debug>_InternalPanelDoNotUseOrYouWillBeFired</code>

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersion
allowClearShow clear buttonboolean | { clearIcon?: ReactNode }false5.8.0: Support Object type
backfillIf backfill selected item the input when using keyboardbooleanfalse
childrenCustomize input elementHTMLInputElement | HTMLTextAreaElement | React.ReactElement<InputProps><Input />
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
dataSourceData source of autocomplete options, please use options insteadDataSourceItemType[]--
defaultActiveFirstOptionWhether active first option by defaultbooleantrue
defaultOpenInitial open state of dropdownboolean-
defaultValueInitial selected optionstring-
disabledWhether disabled selectbooleanfalse
dropdownClassNameThe className of dropdown menu, please use classNames.popup.root insteadstring--
dropdownMatchSelectWidthDetermine whether the dropdown menu and the input are the same width, please use popupMatchSelectWidth insteadboolean | numbertrue-
dropdownRenderCustomize dropdown content, use popupRender instead(originNode: ReactNode) => ReactNode-4.24.0
popupRenderCustomize dropdown content(originNode: ReactNode) => ReactNode-
dropdownStyleThe style of dropdown menu, use styles.popup.root insteadCSSProperties-
popupClassNameThe className of dropdown menu, use classNames.popup.root insteadstring-4.23.0
popupMatchSelectWidthDetermine whether the dropdown menu and the select input are the same width. Default set min-width same as input. Will ignore when value less than select width. false will disable virtual scrollboolean | numbertrue
filterOptionIf true, filter options by input, if function, filter options against it. The function will receive two arguments, inputValue and option, if the function returns true, the option will be included in the filtered set; Otherwise, it will be excludedboolean | function(inputValue, option)true
getPopupContainerParent node of the dropdown. Default to body, if you encountered positioning problems during scroll, try changing to the scrollable area and position relative to it. Examplefunction(triggerNode)() => document.body
notFoundContentSpecify content to show when no result matchesReactNode-
openControlled open state of dropdownboolean-
optionsSelect options. Will get better perf than jsx definition{ label, value }[]-
placeholderThe placeholder of inputstring-
showSearchsearch for configurationtrue | Objecttrue
statusSet validation status'error' | 'warning'-4.19.0
sizeThe size of the input boxlarge | medium | small-
valueSelected optionstring-
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
variantVariants of inputoutlined | borderless | filled | underlinedoutlined5.13.0
virtualDisable virtual scroll when set to falsebooleantrue4.1.0
onBlurCalled when leaving the componentfunction()-
onChangeCalled when selecting an option or changing an input valuefunction(value)-
onDropdownVisibleChangeCalled when dropdown open, use onOpenChange instead(open: boolean) => void-
onOpenChangeCalled when dropdown open(open: boolean) => void-
onFocusCalled when entering the componentfunction()-
onSearchCalled when searching itemsfunction(value)-
onSelectCalled when a option is selected. param is option's value and option instancefunction(value, option)-
onClearCalled when clearfunction-4.6.0
onInputKeyDownCalled when key pressed(event: KeyboardEvent) => void-
onPopupScrollCalled when dropdown scrolls(event: UIEvent) => void-

showSearch

PropertyDescriptionTypeDefaultVersion
filterOptionIf true, filter options by input, if function, filter options against it. The function will receive two arguments, inputValue and option, if the function returns true, the option will be included in the filtered set; Otherwise, it will be excludedboolean | function(inputValue, option)true
onSearchCalled when searching itemsfunction(value)-

Methods

NameDescriptionVersion
blur()Remove focus
focus()Get focus

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Select"></ComponentTokenTable>

FAQ

Why doesn't the text composition system work well with onSearch in controlled mode? {#faq-controlled-onsearch-composition}

Please use onChange to manage control state. onSearch is used for searching input which is not the same as onChange. Besides, clicking on the option will not trigger the onSearch event.

Related issue: #18230 #17916

Why won't a controlled open AutoComplete display a drop-down menu when options are empty? {#faq-empty-options-controlled-open}

The AutoComplete component is essentially an extension of the Input form element. When the options property is empty, displaying empty text could mislead the user into believing the component is not operational, when in fact they are still able to input text. To avoid confusion, the open property will not display the drop-down menu when set to true and in combination with an empty options property. The open property must be used in conjunction with the options property.