JGuang

AutoComplete

AutoComplete · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

AutoComplete

Autocomplete function of input field.

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

  • Basic Usage · basic
  • Customized · options
  • Customize Input Component · custom
  • Non-case-sensitive AutoComplete · non-case-sensitive
  • Lookup-Patterns - Certain Category · certain-category
  • Lookup-Patterns - Uncertain Category · uncertain-category
  • Status · status
  • Variants · variant
  • Customize clear button · allowClear
  • Custom semantic dom styling · style-class

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: ReactElement) => ReactNode-4.24.0
popupRenderCustomize dropdown content(originNode: ReactElement) => 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

Semantic nodeNative descriptionVersion
rootRoot element with relative positioning, inline-flex layout, cursor styles, transitions, border and other basic selector container styles—
prefixPrefix element with layout and styling for prefix content—
contentMultiple selection container with layout, spacing, and wrapping styles for selected items—
placeholderPlaceholder element with font styles and colors for placeholder text—
clearClear button element with layout, styling and interactive effects for clear button—
inputInput element with search input styling, cursor control, font inheritance and other search-related styles. Remove border styles—
popup.rootPopup element with popup layer positioning, z-index, background, border, box-shadow and other popup container styles—
popup.listPopup list element with option list layout, scrolling, max-height and other list container styles—
popup.listItemPopup item element with option item padding, hover effects, selected states, disabled states and other option interactive styles—

Semantic description source: vendor/.dumi/theme/common/SelectSemanticTemplate.tsx

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

TokenTypeNative description
zIndexPopupnumberz-index of dropdown
optionSelectedColorstringText color when option is selected
optionSelectedFontWeightCSSProperties['fontWeight']Font weight when option is selected
optionSelectedBgstringBackground color when option is selected
optionActiveBgstringBackground color when option is active
optionPaddingCSSProperties['padding']Padding of option
optionFontSizenumberFont size of option
optionLineHeightCSSProperties['lineHeight']Line height of option
optionHeightnumberHeight of option
selectorBgstringBackground color of selector
clearBgstringBackground color of clear button
singleItemHeightLGnumberHeight of single selected item with large size
showArrowPaddingInlineEndnumberInline end padding of arrow
hoverBorderColorstringHover border color
activeBorderColorstringActive border color
activeOutlineColorstringActive outline color

Declaration source: vendor/components/select/style/token.ts

import type { CSSProperties } from 'react';

import type { FullToken, GetDefaultToken } from '../../theme/internal';

export interface MultipleSelectorToken {
  /**
   * @desc 多选标签背景色
   * @descEN Background color of multiple tag
   */
  multipleItemBg: string;
  /**
   * @desc 多选标签边框色
   * @descEN Border color of multiple tag
   */
  multipleItemBorderColor: string;
  /**
   * @desc 多选标签高度
   * @descEN Height of multiple tag
   */
  multipleItemHeight: number;
  /**
   * @desc 小号多选标签高度
   * @descEN Height of multiple tag with small size
   */
  multipleItemHeightSM: number;
  /**
   * @desc 大号多选标签高度
   * @descEN Height of multiple tag with large size
   */
  multipleItemHeightLG: number;
  /**
   * @desc 多选框禁用背景
   * @descEN Background color of multiple selector when disabled
   */
  multipleSelectorBgDisabled: string;
  /**
   * @desc 多选标签禁用文本颜色
   * @descEN Text color of multiple tag when disabled
   */
  multipleItemColorDisabled: string;
  /**
   * @desc 多选标签禁用边框色
   * @descEN Border color of multiple tag when disabled
   */
  multipleItemBorderColorDisabled: string;
  /**
   * @internal
   */
}

export interface ComponentToken extends MultipleSelectorToken {
  /**
   * @desc 下拉菜单 z-index
   * @descEN z-index of dropdown
   */
  zIndexPopup: number;
  /**
   * @desc 选项选中时文本颜色
   * @descEN Text color when option is selected
   */
  optionSelectedColor: string;
  /**
   * @desc 选项选中时文本字重
   * @descEN Font weight when option is selected
   */
  optionSelectedFontWeight: CSSProperties['fontWeight'];
  /**
   * @desc 选项选中时背景色
   * @descEN Background color when option is selected
   */
  optionSelectedBg: string;
  /**
   * @desc 选项激活态时背景色
   * @descEN Background color when option is active
   */
  optionActiveBg: string;
  /**
   * @desc 选项内间距
   * @descEN Padding of option
   */
  optionPadding: CSSProperties['padding'];
  /**
   * @desc 选项字体大小
   * @descEN Font size of option
   */
  optionFontSize: number;
  /**
   * @desc 选项行高
   * @descEN Line height of option
   */
  optionLineHeight: CSSProperties['lineHeight'];
  /**
   * @desc 选项高度
   * @descEN Height of option
   */
  optionHeight: number;
  /**
   * @desc 选框背景色
   * @descEN Background color of selector
   */
  selectorBg: string;
  /**
   * @desc 清空按钮背景色
   * @descEN Background color of clear button
   */
  clearBg: string;
  /**
   * @desc 单选大号回填项高度
   * @descEN Height of single selected item with large size
   */
  singleItemHeightLG: number;
  /**
   * @desc 箭头的行末内边距
   * @descEN Inline end padding of arrow
   */
  showArrowPaddingInlineEnd: number;
  /**
   * @desc 悬浮态边框色
   * @descEN Hover border color
   */
  hoverBorderColor: string;
  /**
   * @desc 激活态边框色
   * @descEN Active border color
   */
  activeBorderColor: string;
  /**
   * @desc 激活态 outline 颜色
   * @descEN Active outline color
   */
  activeOutlineColor: string;
}

export interface SelectorToken {
  selectAffixPadding: number | string;
  inputPaddingHorizontalBase: number | string;
  multipleSelectItemHeight: number;
  selectHeight: number;
}

export interface SelectToken extends FullToken<'Select'>, SelectorToken {
  rootPrefixCls: string;

  /** @private Used for internal calculation */
  INTERNAL_FIXED_ITEM_MARGIN: number;
}

FAQ

Why doesn't the text composition system work well with onSearch in controlled mode?

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?

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.

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/auto-complete/AutoComplete.tsx
  • vendor/components/auto-complete/index.tsx

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.