JGuang

自动完成

AutoComplete · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

自动完成

输入框自动完成功能。

何时使用

  • 需要一个输入框而不是选择器。
  • 需要输入建议/辅助提示。

和 Select 的区别是:

  • AutoComplete 是一个带提示的文本输入框,用户可以自由输入,关键词是辅助输入。
  • Select 是在限定的可选项中进行选择,关键词是选择。

代码演示

  • 基本使用 · basic
  • 自定义选项 · options
  • 自定义输入组件 · custom
  • 不区分大小写 · non-case-sensitive
  • 查询模式 - 确定类目 · certain-category
  • 查询模式 - 不确定类目 · uncertain-category
  • 自定义状态 · status
  • 多种形态 · variant
  • 自定义清除按钮 · allowClear
  • 自定义语义结构的样式和类 · style-class

API

通用属性参考:通用属性

参数说明类型默认值版本
allowClear支持清除boolean | { clearIcon?: ReactNode }false5.8.0: 支持对象形式
backfill使用键盘选择选项的时候把选中项回填到输入框中booleanfalse
children自定义输入框HTMLInputElement | HTMLTextAreaElement | React.ReactElement<InputProps><Input />
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
dataSource自动完成的数据源,请使用 options 替代DataSourceItemType[]--
defaultActiveFirstOption是否默认高亮第一个选项booleantrue
defaultOpen是否默认展开下拉菜单boolean-
defaultValue指定默认选中的条目string-
disabled是否禁用booleanfalse
dropdownClassName下拉菜单的 className 属性,请使用 classNames.popup.root 替代string--
dropdownMatchSelectWidth下拉菜单和输入框是否同宽,请使用 popupMatchSelectWidth 替代boolean | numbertrue-
dropdownRender自定义下拉框内容,使用 popupRender 替换(originNode: ReactElement) => ReactNode-4.24.0
popupRender自定义下拉框内容(originNode: ReactElement) => ReactNode-
popupClassName下拉菜单的 className 属性,使用 classNames.popup.root 替换string-4.23.0
dropdownStyle下拉菜单的 style 属性,使用 styles.popup.root 替换CSSProperties-
popupMatchSelectWidth下拉菜单和选择器同宽。默认将设置 min-width,当值小于选择框宽度时会被忽略。false 时会关闭虚拟滚动boolean | numbertrue
filterOption是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 falseboolean | function(inputValue, option)true
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。示例function(triggerNode)() => document.body
notFoundContent当下拉列表为空时显示的内容ReactNode-
open是否展开下拉菜单boolean-
options数据化配置选项内容,相比 jsx 定义会获得更好的渲染性能{ label, value }[]-
placeholder输入框提示string-
showSearch搜索配置true | Objecttrue
status设置校验状态'error' | 'warning'-4.19.0
size控件大小large | medium | small-
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
value指定当前选中的条目string-
variant形态变体outlined | borderless | filled | underlinedoutlined5.13.0
virtual设置 false 时关闭虚拟滚动booleantrue4.1.0
onBlur失去焦点时的回调function()-
onChange选中 option,或 input 的 value 变化时,调用此函数function(value)-
onDropdownVisibleChange展开下拉菜单的回调,使用 onOpenChange 替换(open: boolean) => void-
onOpenChange展开下拉菜单的回调(open: boolean) => void-
onFocus获得焦点时的回调function()-
onSearch搜索补全项的时候调用function(value)-
onSelect被选中时调用,参数为选中项的 value 值function(value, option)-
onClear清除内容时的回调function-4.6.0
onInputKeyDown按键按下时回调(event: KeyboardEvent) => void-
onPopupScroll下拉列表滚动时的回调(event: UIEvent) => void-

showSearch

参数说明类型默认值版本
filterOption是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 falseboolean | function(inputValue, option)true
onSearch搜索补全项的时候调用function(value)-

方法

名称描述版本
blur()移除焦点
focus()获取焦点

Semantic DOM

语义节点原生说明版本
root根元素,包含相对定位、行内 flex 布局、光标样式、过渡动画、边框等选择器容器的基础样式—
prefix前缀元素,包含前缀内容的布局和样式—
content多选容器,包含已选项的布局、间距、换行相关样式—
placeholder占位符元素,包含占位符文本的字体样式和颜色—
clear清除按钮元素,包含清除按钮的布局、样式和交互效果—
input输入框元素,包含搜索输入框的样式、光标控制、字体继承等搜索相关样式,去除了边框样式—
popup.root弹出菜单元素,包含弹出层的定位、层级、背景、边框、阴影等弹出容器样式—
popup.list弹出菜单列表元素,包含选项列表的布局、滚动、最大高度等列表容器样式—
popup.listItem弹出菜单条目元素,包含选项项的内边距、悬浮效果、选中状态、禁用状态等选项交互样式—

语义说明来源: vendor/.dumi/theme/common/SelectSemanticTemplate.tsx

主题变量(Design Token)

类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。

令牌类型原生说明
zIndexPopupnumber下拉菜单 z-index
optionSelectedColorstring选项选中时文本颜色
optionSelectedFontWeightCSSProperties['fontWeight']选项选中时文本字重
optionSelectedBgstring选项选中时背景色
optionActiveBgstring选项激活态时背景色
optionPaddingCSSProperties['padding']选项内间距
optionFontSizenumber选项字体大小
optionLineHeightCSSProperties['lineHeight']选项行高
optionHeightnumber选项高度
selectorBgstring选框背景色
clearBgstring清空按钮背景色
singleItemHeightLGnumber单选大号回填项高度
showArrowPaddingInlineEndnumber箭头的行末内边距
hoverBorderColorstring悬浮态边框色
activeBorderColorstring激活态边框色
activeOutlineColorstring激活态 outline 颜色

声明来源: 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

为何受控状态下使用 onSearch 无法输入中文?

请使用 onChange 进行受控管理。onSearch 触发于搜索输入,与 onChange 时机不同。此外,点击选项时也不会触发 onSearch 事件。

相关 issue:#18230 #17916

为何 options 为空时,受控 open 展开不会显示下拉菜单?

AutoComplete 组件本质上是 Input 输入框的一种扩展,当 options 为空时,显示空文本会让用户误以为该组件不可操作,实际上它仍然可以进行文本输入操作。因此,为了避免给用户带来困惑,当 options 为空时,open 属性为 true 也不会展示下拉菜单,需要与 options 属性配合使用。

原生通用属性

Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。

参数说明类型默认值
style自定义样式CSSProperties-
className自定义类名string-
rootClassName添加在组件最外层的 classNamestring-
autoFocus自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效booleanfalse

安装并复现示例

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

使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

原生类型参考

源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

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

来源与许可

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

保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。