Files
maoyingjie c8d54abefb feat(collections): auto incremental async loading, shared filtering and submenu delay
HD-10 scope, against heroui ce90ea3 collection sources and their
react-aria/react-stately dependencies:

- the ComboBox async demo now auto-loads on scroll-to-bottom
  (IntersectionObserver sentinel + fill chain mirroring the pinned
  ListBoxLoadMoreItem re-observe semantics), never duplicates requests
  (loading guards + monotonic sequence token), drops stale responses on
  query switches, keeps a retry path for failures (injectable mock
  failure) and documents its differences from the pinned SWAPI source;
  the Select demo's observer stays untouched (real-network scroll
  triggering verified in browser)
- the Autocomplete virtualization demo and the default filter share one
  locale-sensitive matcher: createBaseContains (Intl.Collator base
  sensitivity, cafe matches Cafe with accent folding) is now exported
  from the package root
- Autocomplete trigger semantics verified complete (single tab stop,
  Enter/Space/Escape/Delete/IME, single and multiple selection) and the
  scoped slots gained composition data instead of child copying:
  AutocompleteValue exposes selectedItem and per-entry source item,
  the ComboBox root exposes selectedItem
- DropdownSubmenuTrigger exposes the pinned delay contract (default
  200ms): the controlled open gate retimes Reka's fixed 100ms hover,
  keyboard opens stay immediate and cancel the pending hover, pointer
  leave cancels; the dead side/align forwarding is gone (Reka fixes the
  submenu side per direction); Button/Avatar trigger layout
  compensation kept after browser proof it is what wins the cascade
- tests/fixtures/collections-rtl.vue drives the real-browser delay and
  RTL geometry checks; b02-pickers contract pins the new export and
  slot typing

Full suite 111 files / 1666 tests, typecheck, build:docs and the
b02-pickers + api-root-aliases tarball contracts pass.
2026-10-29 13:46:34 +08:00

28 KiB
Raw Permalink Blame History

title, description, component, group
title description component group
ComboBox 组合框 将文本输入与 ListBox 结合,用户可通过输入查询把选项列表过滤为匹配项 combo-box 组件

用法

<script setup lang="ts">
import {
  ComboBox,
  ComboBoxInputGroup,
  ComboBoxPopover,
  ComboBoxTrigger,
  Input,
  Label,
  ListBox,
  ListBoxItem,
} from "@heroui-vue/components";
</script>

<template>
  <ComboBox class="w-[256px]">
    <Label>最喜欢的动物</Label>
    <ComboBoxInputGroup>
      <Input placeholder="搜索动物…" />
      <ComboBoxTrigger />
    </ComboBoxInputGroup>
    <ComboBoxPopover>
      <ListBox>
        <ListBoxItem id="cat" text-value="猫">猫</ListBoxItem>
        <ListBoxItem id="dog" text-value="狗">狗</ListBoxItem>
      </ListBox>
    </ComboBoxPopover>
  </ComboBox>
</template>

输入框既是过滤条件也是控件的真实焦点:ComboBoxInputGroup 把 Input 组合成 role="combobox" 控件,键盘与 aria-activedescendant 都落在它身上,弹层里的列表只维护活动行,不抢焦点。

组件结构

<script setup lang="ts">
import {
  ComboBox,
  ComboBoxInputGroup,
  ComboBoxPopover,
  ComboBoxTrigger,
  ComboBoxValue,
  Description,
  Header,
  Input,
  Label,
  ListBox,
  ListBoxItem,
  ListBoxItemIndicator,
  ListBoxSection,
  Separator,
} from "@heroui-vue/components";
</script>

<template>
  <ComboBox>
    <Label>分类</Label>
    <ComboBoxInputGroup>
      <Input placeholder="搜索…" />
      <ComboBoxTrigger />
    </ComboBoxInputGroup>
    <!-- 展示已选中的值,主要用于多选 -->
    <ComboBoxValue />
    <Description>说明</Description>
    <ComboBoxPopover>
      <ListBox>
        <ListBoxItem id="news" text-value="资讯">
          <Label>资讯</Label>
          <Description>最新消息</Description>
          <ListBoxItemIndicator />
        </ListBoxItem>
        <ListBoxSection>
          <Header>更多</Header>
          <ListBoxItem id="travel" text-value="旅行">
            <Label>旅行</Label>
          </ListBoxItem>
        </ListBoxSection>
      </ListBox>
    </ComboBoxPopover>
  </ComboBox>
</template>

上游的对象式子部件 ComboBox.InputGroup / .Trigger / .Value / .Popover 在本库中是具名导出 ComboBoxInputGroup / ComboBoxTrigger / ComboBoxValue / ComboBoxPopover。

示例

宽度充满

带描述

必填

禁用

含禁用选项

分组选项

受控组件

受控输入值

异步加载

示例的本地数据源模拟固定源的 SWAPI 请求状态机(搜索过滤、游标分页、加载中、滚动到底自动增量加载、取消旧请求与过期响应保护、失败重试);与 SWAPI 的差异是数据为固定 12 条本地角色、延迟为模拟值,失败由「模拟加载失败」开关注入。加载行位于 ListBox 之外(固定源为 ListBoxLoadMoreItem 行内哨兵),自动触发语义一致。

默认选中项

允许自定义值

自定义指示器

自定义展示值

自定义过滤

渲染函数

上游用 React render 函数替换根 DOM 元素;Vue 中对应 as(自定义根元素标签)与 as-child(把首个插槽元素当作根节点)。as-child 时该元素保留自己的属性、事件、ref 与子节点,并额外获得根类名与 data-slot="combo-box",其余部件渲染在它内部;a 之外的可聚焦性由调用方负责。

菜单触发方式

使用 menu-trigger prop 控制 Popover 何时打开:

  • focus(默认):输入框获得焦点时打开 Popover
  • input:用户编辑输入文本时打开 Popover
  • manual:仅当用户按下触发按钮或使用方向键时打开 Popover

表单值

使用 form-value 控制提交表单时提交选中项的 key 还是文本。默认值为 key;allows-custom-value 为真时始终提交当前输入文本。

提交层遵循本库选择类字段的统一约定:单选输出一个同名条目,多选按每个 key 输出一个同名条目(FormData.getAll(name) 得到数组,不再拼成逗号串);禁用或被 disabled-keys 禁用的项、以及从集合中删除的项都不提交;没有任何可提交值时整个字段不出现在 FormData 中。form 同时作用于可见输入与提交层,因此外部表单的校验与 reset 归属一致。

校验行为

使用 validation-behavior 控制校验信息的展示方式:

  • native(默认):值为空或无效时阻止表单提交
  • aria:实时显示错误,不阻止提交

自定义校验

使用 validate 在值无效时返回错误信息,有效时返回 true。

只读

使用 is-read-only 将 ComboBox 设为只读。选中值可以获得焦点,但不能修改。

多选

设置 selection-mode="multiple" 以允许选择多个选项。在多选模式下,使用 ComboBoxValue 展示已选项,并同时给内部的 ListBox 传入 selection-mode="multiple"。选择通过 v-model / default-value(数组)以及 @change(或 @update:model-value)控制;@selection-change 在多选下只携带首个 key,数组请用前两者。

表面样式

在 Surface 内使用时,请使用 variant="secondary" 以应用适合 Surface 背景的低强调变体。

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 ComboBox 组件类。 了解更多。

@layer components {
  .hv-combo-box {
    @apply flex flex-col gap-1;
  }

  .hv-combo-box__input-group {
    @apply relative inline-flex items-center;
  }

  .hv-combo-box__trigger {
    @apply absolute end-0 text-muted;
  }

  .hv-combo-box__popover {
    @apply rounded-lg border border-border bg-surface p-2;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

ComboBox 组件使用以下 CSS 类(查看源码样式):

基础类

  • .hv-combo-box - ComboBox 根容器
  • .hv-combo-box__input-group - 输入框与触发按钮的容器
  • .hv-combo-box__value - 已选值的展示区域(用于多选)
  • .hv-combo-box__trigger - 打开 Popover 的按钮
  • .hv-combo-box__popover - Popover 容器

状态类

  • .hv-combo-box[data-invalid="true"] - 无效状态(隐藏 Description)
  • .hv-combo-box[data-disabled="true"] - 禁用状态
  • .hv-combo-box__trigger[data-focus-visible="true"] - 触发器聚焦
  • .hv-combo-box__trigger[data-disabled="true"] - 触发器禁用
  • .hv-combo-box__trigger[data-open="true"] - 展开状态(图标旋转 180°)
  • .hv-combo-box__value[data-placeholder="true"] - 占位符状态
  • .hv-combo-box [data-slot="input"]:focus - 输入框聚焦(组合框里额外提供非 focus-visible 的聚焦态)

交互状态

  • Hover:触发器上 :hover 或 [data-hovered="true"]
  • Focus:触发器上 :focus-visible 或 [data-focus-visible="true"]
  • Disabled:ComboBox 上 [data-disabled="true"],触发器上 [data-disabled="true"]
  • Open:触发器上 [data-open="true"]

API 参考

ComboBox

Prop 类型 默认值 描述
input-value string - 当前输入值(受控,可用 v-model:input-value)
default-input-value string '' 默认输入值(非受控)
selection-mode 'single' | 'multiple' 'single' 单选或多选
model-value string | number | Array<string | number> | null - 当前值(受控,v-model);多选时为 key 数组
value string | number | Array<string | number> | null - 同 model-value(受控别名)
default-value string | number | Array<string | number> | null - 初始值(非受控);多选时为 key 数组
selected-key string | number | null - 单选当前 key(受控别名)
default-selected-key string | number | null - 单选初始 key(非受控别名)
items Array<{ id, textValue, isDisabled? }> - 数据驱动渲染;不传时以 ListBox 的静态子项作为集合
disabled-keys Array<string | number> - 禁用项的 key
open / is-open boolean - 弹层打开状态(受控,可用 v-model:open)
default-open boolean false 弹层默认打开状态(非受控)
default-filter (text: string, inputValue: string) => boolean 不区分大小写与重音的包含匹配 自定义过滤函数;默认谓词按 ConfigProvider 的 locale 折叠大小写与变音符号(Café 可由 cafe 命中)
is-disabled boolean false 是否禁用
disabled boolean false is-disabled 的等价别名
is-read-only boolean false 只读:值可聚焦但不可修改
is-required boolean false 是否必填
is-invalid boolean false 值是否无效
validate (value) => string | string[] | true | null | undefined - 校验函数;validation-behavior="native" 时提交会展示错误,实时校验请改用 is-invalid
validation-behavior 'native' | 'aria' 'native' 原生校验阻止提交,或只用 ARIA 标记
name string - 表单提交时的字段名(输出隐藏输入)
form string - 关联的表单 id
form-value 'key' | 'text' 'key' 提交 key 还是文本;allows-custom-value 时始终提交文本
auto-complete string 'off' 输入框 autocomplete
auto-focus boolean false 挂载时聚焦
allows-custom-value boolean false 允许输入不在列表中的值
allows-empty-collection boolean false 无匹配项时仍可打开弹层
menu-trigger 'focus' | 'input' | 'manual' 'focus' 打开弹层的交互方式
should-focus-wrap boolean false 方向键是否在首尾循环
full-width boolean false 是否占满容器宽度
variant 'primary' | 'secondary' 'primary' 视觉变体:primary 为默认带阴影样式;secondary 为低强调无阴影,适用于 Surface 内。根上的取值由内部 Input 继承,Input 自己显式设置时以子控件为准
placeholder string 'Select an item' ComboBoxValue 的缺省占位文本(上游根上没有该 prop,由 ComboBoxValue 的 placeholder 表达)
as string 'div' 自定义根元素标签
as-child boolean false 使用唯一子元素作为根节点并合并属性
默认插槽 { isOpen, inputValue, selectedKey, selectedKeys, selectedItem } - 组件内容;作用域提供当前状态
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

事件:

事件 载荷 描述
update:modelValue string | number | Array<string | number> | null 值变化(多选时为数组)
change 同上 值变化
update:selectedKey string | number | null 单选 key 变化
selectionChange string | number | null 单选 key 变化;多选时只携带首个 key
update:inputValue string 输入值变化
inputChange string 输入值变化
update:open boolean 弹层开关变化
openChange boolean 弹层开关变化

aria-label、aria-labelledby、aria-describedby 落在真正的 role="combobox" 输入框上,而不是根容器。

作用域插槽与渲染状态

默认插槽提供 { isOpen, inputValue, selectedKey, selectedKeys, selectedItem }。上游的 Render Props 表格里还有 state 与 selectedItem:selectedItem 以作用域数据补齐(根 items 中与单选 key 匹配的源数据项;多选或自定义值时为 null,ComboBoxValue 的 selectedItems 条目也携带同源 item),调用方可直接用自己的数据对象组合展示,无需按键反查;本库不暴露 React Aria 的 state 对象,该差异不另设等价 prop。

ComboBoxInputGroup

Prop 类型 默认值 描述
默认插槽 Component - 必须包含本库 Input;可再放 ComboBoxTrigger
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

ComboBoxValue

渲染已选中的值;没有选中项时渲染占位符。多选时以逗号连接各选中项文本。

Prop 类型 默认值 描述
placeholder string(上游为 ReactNode) 根上的 placeholder 未选中任何项时的占位文本
默认插槽 { isPlaceholder, selectedItems } - 自定义已选值展示;selectedItems 条目为 { key, textValue, item? }(item 为根 items 中的源数据项)
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

ComboBoxTrigger

Prop 类型 默认值 描述
is-disabled boolean false 是否禁用触发器
默认插槽 Component - 自定义触发器内容(默认下箭头)
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

触发器默认 tabindex="-1":整个组合框保留输入框一个 Tab 入口。

ComboBoxPopover

Prop 类型 默认值 描述
placement 'bottom' | 'bottom left' | 'bottom right' | 'bottom start' | 'bottom end' | 'top' | 'top left' | 'top right' | 'top start' | 'top end' | 'left' | 'left top' | 'left bottom' | 'start' | 'start top' | 'start bottom' | 'right' | 'right top' | 'right bottom' | 'end' | 'end top' | 'end bottom' 'bottom' 相对触发容器的位置
offset number 8 偏移量
should-flip boolean true 空间不足时是否翻转
container-padding number 12 距离视口边缘的最小间距
to string | HTMLElement - 自定义 Portal 容器
默认插槽 Component - 弹层内容(通常为 ListBox)
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

无障碍

ComboBox 实现 ARIA combobox 模式:

  • 真实焦点始终留在输入框,aria-activedescendant 指向已挂载且可见的活动选项
  • ArrowDown / ArrowUp / Home / End 在过滤后的可见集合中移动活动项,Enter 确认,Escape 关闭,Tab 离开
  • 中文输入法组合期间不确认选择(isComposing / keyCode 229 时忽略键盘提交)
  • 过滤不会清除已接受的选择;被过滤掉的已选项仍保留标签
  • 弹层内不出现第二个 Tab 入口,关闭后移除 aria-activedescendant
  • 组合输入期间保留已经渲染的行,直到 compositionend 才提交一次过滤;会话内的 Enter 不会确认选择(同时检查会话状态、isComposing 与 keyCode 229)
  • 必填只由校验阻止无效提交,不阻止用户把值编辑为空;native 与 aria 两种校验都按同一份约束判断
  • 支持禁用状态(is-disabled / disabled-keys)、输入过滤式搜索、与原生表单集成(name / form / form-value)与自定义值(allows-custom-value)
  • 未实测读屏软件的变更播报与逐项读屏行为,因此不声称已通过该层验收

相关组件

ListBox 列表框 · Popover 弹出框 · Input 输入框