Files
heroui-vue/docs/migrations/B02-combobox-autocomplete.md
T
maoyingjie fa73cafd20 docs: audit ComboBox and Autocomplete contracts and shared gaps
Record the fixed-baseline part/prop/preview inventory for both families, the
composition differences that prevent treating them as one component, the reuse
plan against the existing collection/field/overlay parts, and the two blocking
protocol gaps (collection filtering and virtual focus with activedescendant)
with a minimal interface proposal each.

No components are implemented in this commit.
2026-10-25 14:42:03 +08:00

108 lines
16 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# B02 ComboBox / Autocomplete — 审计与设计(阶段一,未实现)
分支 `feat/b02-combobox-autocomplete`,起点 `84c4893`(本地 main,含 B03 修复 `20d34d7` 与合并 `eb10b6a`)。**本文件当前只记录审计、契约与设计决策;组件、文档、演示、测试尚未实现**,不能据此认为本批已交付。
固定基线 HeroUI 3.2.6 / `ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960`。建议开发端口 5182(已确认空闲;5174/5176/5181/5183 被其他工作区占用)。
## 1. 源契约:ComboBox(5 部件)
源文件:`packages/react/src/components/combo-box/combo-box.tsx`、`packages/styles/components/combo-box.css`、`packages/styles/src/components/combo-box/combo-box.styles.ts`、中文正文 `(pickers)/combo-box.mdx`、演示 `demos/cn/combo-box`(24 例)。
| 部件 | 源实现 | 关键 Props / 标记 |
| --------------------- | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ComboBox` | RAC `ComboBox` + `FieldSlotsGate` | 全部 RAC ComboBox props + `variant`(`primary`/`secondary`,默认 primary)、`fullWidth`(默认 false)、`menuTrigger`(默认 `focus`);`data-slot="combo-box"` |
| `ComboBox.InputGroup` | 集合槽注入器:除末个子元素外为 siblings,末位必须是 `Trigger`;渲染 RAC `Group` + 内层 `dom.div` | `className`、`render`;两处 `data-slot="combo-box-input-group"` |
| `ComboBox.Value` | RAC `ComboBoxValue` | `placeholder`、`children`(含 render 函数);`data-slot="combo-box-value"` |
| `ComboBox.Trigger` | RAC `Button` | 默认子元素 `IconChevronDown`(`data-slot="combo-box-trigger-default-icon"`);`data-open`、`data-slot="combo-box-trigger"`;隐藏时返回 null |
| `ComboBox.Popover` | RAC `Popover` + `SurfaceContext{variant:"default"}` | `placement`(默认 `bottom`);`data-slot="combo-box-popover"` |
变体槽:`base / inputGroup / popover / trigger / value`;`fullWidth` 追加 `combo-box--full-width`、`combo-box__input-group--full-width`。
CSS 要点:根 `flex flex-col gap-1`;无效时隐藏 `[data-slot="description"]`;`[data-slot="input"]` 为 `min-w-0 flex-1`,紧跟 trigger 时 `pe-7`,并额外提供 `:focus`(不只是 `focus-visible`)的 `status-focused-field`;trigger 绝对定位 `end-0 top-1/2 pe-2`,`data-open` 时图标 `rotate-180`;popover `min-w-(--trigger-width)`、`border-radius: min(32px, var(--radius-3xl))`、进出场动画与 `data-placement` 位移;popover 内 `[data-slot="list-box"]` 为 `p-1.5`,item `px-2.5` 且单选的 checkmark 不做 transition。
**公开预览清单(24,正文顺序)**:default(用法)、full-width、with-description、required、disabled、with-disabled-options、with-sections、controlled、controlled-input-value、asynchronous-loading、default-selected-key、allows-custom-value、custom-indicator、custom-value、custom-filtering、render-function、menu-trigger、form-value、validation-behavior、custom-validation、read-only、multiple-selection、on-surface、custom-styles。每例都有对应 `.tsx`,无孤儿文件。
各例关键差异(供实现后逐项核销):default 6 项(aardvark/cat/dog/kangaroo/panda/snake → 土豚/猫/狗/袋鼠/熊猫/蛇);full-width 3 项且外层 `w-[400px]`;with-sections 12 项分北美洲/欧洲/亚洲三组 + `Separator`;controlled 5 项;controlled-input-value 受控 `inputValue`;asynchronous-loading 用 `useAsyncList` + SWAPI 真实网络、`allowsEmptyCollection`、`Collection` + `ListBoxLoadMoreItem` + `Spinner` + `EmptyState`;menu-trigger 三块(focus/input/manual);form-value 两个 ComboBox(`formValue="key"` name=animal、`formValue="text"` name=animal-text)+ Form + FieldError,输出 `key:…。文本:…。`;validation-behavior 两个 Form(native/aria);custom-validation `validate` 对 `selectedKey` 判定(空 → 请选择一种动物;snake → 不允许选择蛇);read-only `isReadOnly` + `defaultSelectedKey="cat"`;multiple-selection 需同时在根与 `ListBox` 传 `selectionMode="multiple"` 并放 `ComboBox.Value placeholder="未选择任何动物"`;on-surface 用 Surface + `variant="secondary"`;custom-styles 用 `data-[focused=true]`/`data-[selected=true]` 定制 item。
## 2. 源契约:Autocomplete(7 部件)
源文件:`packages/react/src/components/autocomplete/autocomplete.tsx`、`packages/styles/components/autocomplete.css`、`packages/styles/src/components/autocomplete/autocomplete.styles.ts`、中文正文 `(pickers)/autocomplete.mdx`、演示 `demos/cn/autocomplete`(24 例)。
| 部件 | 源实现 | 关键 Props / 标记 |
| -------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `Autocomplete`(Root) | RAC **Select** + `FieldSlotsGate` | RAC Select props + `fullWidth`、`variant`(primary/secondary)、`onClear`;`data-slot="autocomplete"` |
| `Autocomplete.Trigger` | RAC `Group` | 点击时 `state.toggle()`(点在 ClearButton 内不切换);`data-slot="autocomplete-trigger"` |
| `Autocomplete.Value` | RAC `SelectValue` | render 值 `{defaultChildren, isPlaceholder, state, selectedItems, selectedText}`;`data-slot="autocomplete-value"` |
| `Autocomplete.Indicator` | RAC `Button` + 默认 `IconChevronDown` | 有子元素时 clone 并注入 class/`data-slot`/`data-open`;`data-slot="autocomplete-indicator"`、默认图标 `autocomplete-default-indicator` |
| `Autocomplete.Popover` | RAC `Popover` + SurfaceContext | `placement`(默认 bottom)、`--trigger-width` 由 ResizeObserver 写入;`data-slot="autocomplete-popover"` |
| `Autocomplete.Filter` | RAC `Autocomplete` | `filter(text, input)`、受控 `inputValue`、`onInputChange`;`data-slot="autocomplete-filter"` |
| `Autocomplete.ClearButton` | 原生 `button` | `aria-label="Clear selection"`、`tabIndex=-1`、`data-empty`、`disabled=isDisabled`;`data-slot="autocomplete-clear-button"` |
变体槽:`base / clearButton / filter / indicator / popover / trigger / value`;`fullWidth` 追加 `autocomplete--full-width`、`autocomplete__trigger--full-width`;`variant` 追加 `autocomplete--primary` / `autocomplete--secondary`。
CSS 要点:trigger 是 `min-h-9 rounded-field border bg-field px-3 py-2 text-sm shadow-field` 的可点击容器(不是 input),`:has(.autocomplete__indicator)` 时 `pe-7`,hover 排除 clear-button hover,invalid 用 `status-invalid-field`;`--secondary` 变体把背景改为 `--default`(无阴影);value `flex-1 text-start text-base sm:text-sm`,`data-placeholder` 用 field-placeholder 色,并隐藏 `list-box-item-indicator`;indicator 绝对定位 `end-2`,`data-open` 时 `rotate-180`;popover `flex w-(--trigger-width) max-w-(--trigger-width) flex-col ... pt-2`,进出场动画,内含 `[data-slot="list-box"]` `max-h-[320px] min-h-0 overflow-y-auto p-1.5`、`[data-slot="search-field"]` `px-3 py-1`、`[data-slot="empty-state"]` 居中 muted;虚拟行需要 `[role="presentation"] > [data-slot="list-box-item"] { width: calc(100% - var(--spacing) * 3) }`;clear-button 24px 盒、`data-empty` 时 `opacity-0 pointer-events-none`。
**公开预览清单(24,正文顺序)**:default(用法)、variants、full-width、with-description、required、disabled、with-disabled-options、allows-empty-collection、with-sections、multiple-select、controlled、controlled-multiple、controlled-open-state、asynchronous-filtering、custom-indicator、custom-value、on-surface、virtualization、**高级示例**:user-selection、user-selection-multiple、location-search、tag-group-selection、email-recipients、custom-styles。每例都有对应 `.tsx`。
## 3. 两者不是同一组件(组合差异)
| 维度 | ComboBox | Autocomplete |
| ---------- | --------------------------------------------------------- | -------------------------------------------------------------- |
| 底座语义 | RAC `ComboBox`(可编辑输入 + listbox) | RAC `Select` + `Autocomplete`(选择器 + 弹层内搜索框) |
| 值输入方式 | 输入框文本即过滤条件,选中项回写输入框文本 | 触发器只显示值,搜索框在弹层内;触发文本不参与过滤 |
| 触发器 | `InputGroup` 内 `Input` + 绝对定位 `Trigger` 按钮 | `Trigger`(可点击容器)+ `Value` + `ClearButton` + `Indicator` |
| 已选值展示 | `Value` 仅多选常用;单选值在输入框里 | `Value` 是主要展示;多选常配合 `TagGroup` |
| 空集合 | `allowsEmptyCollection`(配合 async) | `allowsEmptyCollection`(允许 0 项仍可交互) |
| 表单值 | `formValue="key"\|"text"`,`allowsCustomValue` 时强制文本 | 单一 `name` 提交 key |
| 自定义值 | `allowsCustomValue`(输入任意文本) | 不支持(值来自集合) |
| 过滤入口 | 根 `defaultFilter` | `Autocomplete.Filter filter` / 受控 `inputValue` |
结论:必须分别实现两套部件与状态语义,不能用一个“可筛选输入框”顶替两者。
## 4. 复用计划(现有基建)
- 字段/校验:`useChoiceField` + `FieldError`/`Label`/`Description`(B03 已验证的 field 协议),`useChoiceValue` 处理受控/拒绝/默认值。
- 集合:`collection/selection`(已接受值)、`collection/registry`(挂载成员、DOM 顺序)、`collectionOwnerKey`(ListBox 已消费:keys/mode/disabled/`request`/`action`/`leave`/`connect`)。
- 弹层:`components/popover/Popover` + `useOverlayState`(Select 已用;非模态、Teleport、外部点击、退出动画)。
- 列表:公共 `ListBox`/`ListBoxItem`/`ListBoxSection`(演示已经直接使用)。
- 输入:`Input`(ComboBox)与 `SearchField`/`SearchFieldGroup`/`SearchFieldInput`(Autocomplete 弹层内搜索)。
- 展示值:`TagGroup`/`Tag`(多选 Value 渲染)、`Avatar`、`EmptyState`、`Spinner`、`Surface`。
## 5. 阻断性共享缺口(需决策,未擅自改协议)
两个缺口都在集成人维护的 `collection` 与 `list-box` 目录内,本批按约定**不静默修改**。
### 5.1 集合过滤(ComboBox `defaultFilter`、Autocomplete `Filter filter`)
- 复现:`<ComboBox><Input/><Trigger/></ComboBox><Popover><ListBox>静态 Item…</ListBox></Popover>`;输入“猫”后源实现把不匹配项从集合中移除,键盘导航也只在匹配项之间移动。当前 `ListBox` 的 `known()/eligible()` 只按 `disabled` 过滤,没有“不可见”概念;静态子节点也无法被上层过滤。
- 最小接口建议(`CollectionOwner`,可选、缺省不改变现状):`visible?: (key: CollectionKey) => boolean`。`ListBox` 的 `eligible()/available()` 尊重它,`ListBoxItem` 在为 false 时不渲染(或 `hidden`,但需要同时不参与导航与 `aria-setsize`)。
- 受影响:`collection/owner.ts`、`list-box/ListBox.vue`、`list-box/ListBoxItem.vue`、`list-box/context.ts`;`Select` 传入 `undefined` 时行为不变。
- 现有可绕过范围:数据驱动示例(`ListBox :items`)可由调用方自行过滤(async/hybrid/virtualization 三例即如此),但源示例里的大多数是**静态子节点 + 库内过滤**,绕不过。
### 5.2 虚拟焦点 / `aria-activedescendant`(两者的键盘协作)
- 复现:ARIA combobox 模式要求 DOM 焦点留在输入框(ComboBox 的 `Input`、Autocomplete 弹层内的 `SearchField.Input`),用 `aria-activedescendant` 指向高亮项。当前 `ListBox` 一律把 DOM 焦点移到条目(`focus(key)` → `element.focus()`,条目为 roving `tabindex`),并把键盘处理挂在列表根上。焦点一旦离开输入框,连续输入就会中断;且在弹层里会多出第二个 Tab 入口。
- 最小接口建议:`CollectionOwner` 增加可选 `virtualFocus?: () => boolean` 与 `activeKey?: Ref<Key | undefined>`;虚拟焦点开启时 `ListBox` 不移动 DOM 焦点、不设 roving `tabindex`,仅把 `data-focused`/`data-active` 标记给活动项,`aria-activedescendant` 由调用方(ComboBox/Autocomplete)在输入框上写出。
- 受影响:同上四个文件;`Select` 保持现状(它本来就应该把焦点移入列表)。
- 替代方案(需确认):由集成人先在 main 上补这两个可选成员,本批直接消费;或授权本批在分支内实现并随报告逐条说明(B03 的经验是共享改动会在合并时被逐项复核)。
## 6. 建议的实施拆分
本批源规模:12 个部件、48 个基线示例、2 页中文正文(754 行)、约 20 组行为契约(输入/选择/过滤/异步/IME/虚拟化/表单/校验)。相对 B03(3 个家族 14 例)大约 3–4 倍。
建议顺序(若同意继续):
1. **B02a ComboBox**:先落地 5.1 + 5.2 的可选协议(若授权),实现 5 部件 + 24 例 + 文档 + 测试 + 独立消费 + 浏览器(IME、menuTrigger 三态、formValue、validate、多选 Value)。
2. **B02b Autocomplete**:在稳定的过滤/虚拟焦点机制上实现 7 部件 + 24 例 + 文档 + 测试;多选 Value 按源文档的 `selectedText` 提示处理(不复制 item children)。
若必须一次交回,建议至少先落地 5.1/5.2 的协议决策,再开始写 48 个演示,避免返工。
## 7. 待确认的框架差异(先记录,实现时逐条核销)
1. `ListBox` 的空态是 `empty` 插槽,源是 `renderEmptyState`;演示中 `<ListBox />` 自闭合 0 项(allows-empty-collection)需要对应写法。
2. 源 `ComboBox.Value` 在多选时按集合分隔符连接 item children;Vue 侧不复制 children,改为 `selectedText` 等作用域值(与源文档给 `Autocomplete.Value` 的 `selectedText` 建议一致)。
3. 源 `render` prop → Vue `as` / `as-child`(与 B01/B03 一致);`render-function` 示例保持源 `div` 根 + `data-custom="foo"`。
4. 源 `Collection`(`@react-stately`)与 `Virtualizer`:Vue 侧用 `ListBox :items` + `virtualized` 能力,异步用调用方状态;不引入第二个集合内核。
5. 源 `useFilter` 是公开导出;Vue 侧是否需要提供等价 `useFilter`(contains/startsWith/endsWith + sensitivity)需要在实现时决定并写进 API 表,不能用私有实现假装等价。
6. `IconChevronDown` / `CloseIcon` 默认图形:沿用本库既有基线图标做法(不自绘 SVG)。