---
title: Autocomplete 自动完成
description: 结合选择与过滤,让用户从选项列表中搜索并选择
component: autocomplete
group: 组件
---
## 用法
```vue
California
Texas
```
触发器是唯一 Tab 入口;打开后焦点进入弹层内的搜索框,由它负责过滤、`aria-activedescendant` 与方向键导航,列表只维护活动行。
## 组件结构
```vue
说明
资讯
```
上游的对象式子部件 `Autocomplete.Trigger` / `.Value` / `.Indicator` / `.Popover` / `.Filter` / `.ClearButton` 在本库中是具名导出 `AutocompleteTrigger` / `AutocompleteValue` / `AutocompleteIndicator` / `AutocompletePopover` / `AutocompleteFilter` / `AutocompleteClearButton`。
## 示例
### 变体
Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `secondary`(低强调、无阴影,适用于 Surface 内)。
### 宽度充满
### 带描述
### 必填
### 禁用
### 含禁用选项
### 允许空选项
`allows-empty-collection` 允许集合为空时仍可打开并交互。
### 分组选项
### 多选
上游提醒:多选时 `Autocomplete.Value` 默认会把每个选中项在 `ListBox.Item` 中的子树复制到触发器里。本库不复制子树,而是给 `AutocompleteValue` 的作用域插槽提供 `selectedItems`(`{ key, textValue }[]`)与 `selectedText`(纯文本),由调用方决定渲染纯文本还是标签:
```vue
{{ isPlaceholder ? defaultChildren : selectedText }}
```
需要可移除标签时用 `selectedItems` 自行渲染,示例见「多选」。
### 受控组件
### 受控多选
### 受控展开状态
### 异步搜索
### 自定义指示器
### 自定义展示值
用 `AutocompleteValue` 的作用域插槽自定义展示值。
### 表面样式
在 Surface 内使用时请用 `variant="secondary"`。
### 虚拟滚动
集合使用 `ListBox` 的 `items` + `virtualized` + `row-height` 渲染可见窗口,仅挂载视口内的行。
## 高级示例
### 用户选择
### 用户多选
### 地点搜索
### 标签组选择
### 邮件收件人
## 自定义样式
### Tailwind CSS
### 全局 CSS
可使用 `@layer components` 指令自定义 Autocomplete 组件类。
```css
@layer components {
.hv-autocomplete {
@apply flex flex-col gap-1;
}
.hv-autocomplete__trigger {
@apply rounded-lg border border-border bg-surface p-2;
}
.hv-autocomplete__value {
@apply text-current;
}
.hv-autocomplete__clear-button {
@apply text-muted hover:text-foreground;
}
.hv-autocomplete__indicator {
@apply text-muted;
}
.hv-autocomplete__popover {
@apply rounded-lg border border-border bg-surface p-2;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/autocomplete.css)):
#### 基础类
- `.hv-autocomplete` - 根容器
- `.hv-autocomplete__trigger` - 展示值并打开弹层的容器
- `.hv-autocomplete__value` - 值或占位符
- `.hv-autocomplete__clear-button` - 清除已选值的按钮
- `.hv-autocomplete__indicator` - 下拉指示图标
- `.hv-autocomplete__popover` - 弹层容器
- `.hv-autocomplete__filter` - 过滤包装器
#### 变体类
- `.hv-autocomplete--primary` - 带阴影的主变体(默认)
- `.hv-autocomplete--secondary` - 无阴影的次变体
#### 状态类
- `.hv-autocomplete[data-invalid="true"]` - 无效状态
- `.hv-autocomplete[data-disabled="true"]` - 禁用状态
- `.hv-autocomplete__trigger[data-focus-visible="true"]` - 触发器聚焦
- `.hv-autocomplete__trigger[data-disabled="true"]` - 触发器禁用
- `.hv-autocomplete__trigger[data-open="true"]` - 展开状态
- `.hv-autocomplete__value[data-placeholder="true"]` - 占位符状态
- `.hv-autocomplete__clear-button[data-empty="true"]` - 无选择时隐藏清除按钮
- `.hv-autocomplete__indicator[data-open="true"]` - 打开时图标旋转 180°
### 交互状态
- **Hover**:触发器上 `:hover` 或 `[data-hovered="true"]`
- **Focus**:触发器与搜索框上 `:focus-visible`
- **Disabled**:根与触发器上 `[data-disabled="true"]`
- **Open**:触发器上 `[data-open="true"]`、指示器上 `[data-open="true"]`
## API 参考
### Autocomplete
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ----------------------------------- | ------------------ | ---------------------------------------------- |
| `placeholder` | `string` | `'Select an item'` | 为空时显示的占位文本 |
| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 |
| `allows-empty-collection` | `boolean` | `false` | 是否允许空集合 |
| `is-open` | `boolean` | - | 弹层打开状态(受控,`v-model:open` 亦可) |
| `default-open` | `boolean` | `false` | 弹层默认打开状态(非受控) |
| `disabled-keys` | `Array` | - | 禁用项的 key |
| `is-disabled` | `boolean` | `false` | 是否禁用 |
| `model-value` | `string \| number \| Array \| null` | - | 当前值(受控,`v-model`) |
| `value` | `string \| number \| Array \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array \| null` | - | 初始值(非受控) |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `name` | `string` | - | 表单提交字段名(输出隐藏输入,多选以逗号连接) |
| `form` | `string` | - | 关联的表单 id |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
事件:
| 事件 | 载荷 | 描述 |
| ------------------- | ----------------------------------- | ------------ |
| `update:modelValue` | `string \| number \| Array \| null` | 值变化 |
| `change` | 同上 | 值变化 |
| `update:open` | `boolean` | 弹层开关变化 |
| `openChange` | `boolean` | 弹层开关变化 |
| `clear` | - | 清除已选值 |
### AutocompleteTrigger
| Prop | 类型 | 默认值 | 描述 |
| ------------- | --------------------------- | ------- | -------------------------------------------------------- |
| `is-disabled` | `boolean` | `false` | 是否禁用触发器 |
| 默认插槽 | `Component` | - | 通常为 `AutocompleteValue` + `ClearButton` + `Indicator` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
触发器渲染为 `role="button"` 的可点击容器,是组件唯一的 Tab 入口(`tabindex="0"`;禁用时 `-1`),`Enter` / `Space` / `ArrowDown` 打开,`Escape` 关闭,`Delete` / `Backspace` 清除。
### AutocompleteValue
| Prop | 类型 | 默认值 | 描述 |
| -------- | ----------------------------------------------------------------- | ------ | ----------------------------- |
| 默认插槽 | `{ isPlaceholder, defaultChildren, selectedText, selectedItems }` | - | 自定义展示值 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### AutocompleteIndicator
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | -------------------------------- |
| 默认插槽 | `Component` | - | 自定义指示器图形(默认为下箭头) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
指示器是装饰元素(`aria-hidden`),激活由触发器负责。
### AutocompleteClearButton
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------- |
| 默认插槽 | `Component` | - | 自定义清除图标 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`aria-label` 默认 `Clear selection`,`tabindex="-1"`,无选择时 `data-empty="true"` 并隐藏。
### AutocompletePopover
| Prop | 类型 | 默认值 | 描述 |
| ------------------- | --------------------------- | ---------- | ----------------------------- |
| `placement` | 位置联合类型 | `'bottom'` | 相对触发器的位置 |
| `offset` | `number` | `8` | 偏移量 |
| `should-flip` | `boolean` | `true` | 空间不足时是否翻转 |
| `container-padding` | `number` | `12` | 距离视口边缘的最小间距 |
| `to` | `string \| HTMLElement` | - | 自定义 Portal 容器 |
| 默认插槽 | `Component` | - | 通常为 `AutocompleteFilter` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
宽度跟随触发器(`--trigger-width`)。
### AutocompleteFilter
| Prop | 类型 | 默认值 | 描述 |
| --------------------- | ------------------------------- | ---------------------- | ------------------------------------------------------------- |
| `filter` | `(text, inputValue) => boolean` | 不区分大小写的包含匹配 | 自定义过滤函数 |
| `input-value` | `string` | - | 受控搜索文本 |
| `default-input-value` | `string` | `''` | 非受控初始搜索文本 |
| 默认插槽 | `Component` | - | 必须包含 `SearchField`(内含 `SearchFieldInput`)与 `ListBox` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
过滤文本由 `AutocompleteFilter` 拥有,并通过集合的过滤协议影响列表的显示与键盘导航;它不会清除已接受的选择。
受控文本用 `:input-value` + `@input-change`(或 `v-model:input-value`):
| 事件 | 载荷 | 描述 |
| ------------------- | -------- | ------------ |
| `update:inputValue` | `string` | 搜索文本变化 |
| `inputChange` | `string` | 搜索文本变化 |
### 作用域插槽与渲染信息
`AutocompleteValue` 的默认插槽提供:
| 字段 | 类型 | 描述 |
| ----------------- | --------------------------- | -------------------------------------------- |
| `isPlaceholder` | `boolean` | 是否没有选中项 |
| `defaultChildren` | `string` | 缺省文本(选中项文本以逗号连接,否则占位符) |
| `selectedText` | `string` | 选中项 `textValue` 的连接文本 |
| `selectedItems` | `Array<{ key, textValue }>` | 选中项 |
上游 `state.selectedItems`(节点)在本库中是上表的 `selectedItems`(数据);不复制 item 子树,见「多选」说明。
## 无障碍
Autocomplete 实现带过滤的 ARIA select 模式:
- 触发器是唯一 Tab 入口,并带 `aria-haspopup="listbox"` / `aria-expanded` / `aria-controls`
- 打开后焦点进入弹层内搜索框(`role="combobox"`),`aria-activedescendant` 指向已挂载且可见的活动选项
- `ArrowDown` / `ArrowUp` / `Home` / `End` 在过滤后的可见集合中移动,`Enter` 确认,`Escape` 关闭,`Tab` 离开
- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交)
- 过滤不会清除已接受的选择;关闭后焦点回到触发器,弹层内不出现第二个 Tab 入口
## 相关组件
[ComboBox 组合框](./combo-box) · [Select 选择器](./select) · [ListBox 列表框](./list-box) · [SearchField 搜索框](./search-field)