Files
heroui-vue/docs/migrations/B02-combobox-autocomplete.md
T
maoyingjie c8b0aa26c3 fix(collection): make virtual window, reveal and confirmation honest
- Give the verification instance a real constrained scroll owner and a row
  height that matches the rendered row, and feed ListBox the full data schema so
  a row that was never mounted can still be filtered.
- Move reveal validity in front of every side effect and re-check after each
  await: latest token, mounted component, enabled row, still visible and open.
  Waiting for a virtualized row now requires connected DOM, not just a
  registration, and closing the collection cancels pending work.
- Reconcile the shared active key reactively: a removed, disabled, filtered-out
  or closed active row is cleared, while the accepted value stays as history.
  The composite re-validates before confirming, so a stale candidate cannot be
  submitted.
- Read the caller's cancellation before the focus guard so a plain virtual-focus
  press keeps its pressed state, and use one nested-interactive boundary for
  both pointer and click, covering contenteditable variants and custom roles.
- Render the empty channel for static slot collections too, keeping hidden
  members mounted.

Adds the seven-case round-two probe and twelve regressions; browser checks cover
the real virtual window, remote filtering, viewport-contained navigation and
filter restoration.
2026-10-25 16:40:00 +08:00

248 lines
36 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`)。按用户 2026-10-25 的分段安排推进:**B02a 最小共享协议扩展(已实现)→ B02b ComboBox → B02c Autocomplete → B02d 收口**。每段独立提交,不合 main、不推送。
固定基线 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. 共享缺口与 B02a 已落地的可选协议
两个缺口都在集成人维护的 `collection` 与 `list-box` 目录内。用户已明确授权本批做**支持过滤与输入端虚拟焦点所必需的最小扩展**(覆盖原提示词中禁止修改这些共享文件的限制),并保留 ListBox/Select 的默认行为。实现见第 8 节。
### 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)。
## 8. B02a:最小共享协议扩展(已实现并单独提交)
范围:只增加可选的私有协议成员与消费端分支,**不新增 ListBox 公共 prop、不复制集合内核、不改 Select/字段/弹层内核**。
### 8.1 协议增量
`collection/owner.ts`:
```ts
export interface CollectionView {
filter?: (record: CollectionRecord) => boolean; // 展示与导航过滤
virtualFocus?: () => boolean; // 焦点留在组合体输入框
activeKey?: Ref<CollectionKey | undefined>; // 由组合体驱动的活动项
}
export interface OwnedCollection {
items: () => CollectionRecord[]; // 完整数据
focus: (edge: "first" | "last" | "selected") => Promise<void>;
optionId?: (key: CollectionKey) => string | undefined; // aria-activedescendant 用
reveal?: (key: CollectionKey) => Promise<void>; // 只滚动/等挂载,不取焦点
}
export interface CollectionOwner extends CollectionView {
/* 原有成员不变 */
}
```
四个成员全部可选;未提供的调用方(独立 ListBox、Select)行为完全不变。
### 8.2 数据层次(对应“完整数据 / 可见数据 / 可导航项 / 已挂载节点”)
| 层次 | 来源 | 用途 |
| ---------- | ------------------------------------- | ------------------------------------------------- |
| 完整数据 | `known()`(`items` + registry 合并) | 已接受值、文本标签、表单提交、`disabledKeys` 判定 |
| 可见数据 | `visibleKnown()` = `known()` + filter | 渲染与 `aria-setsize/posinset`(虚拟化时) |
| 可导航项 | `eligible()` = 可见数据 - 禁用 | 方向键/Home/End/typeahead/全选/选区范围 |
| 已挂载节点 | `registry.ordered()`(DOM 顺序) | 邻项、焦点目标、`optionId` |
过滤**不会**清除已接受值,也不会卸载行:`ListBoxItem` 输出 `data-filtered="true"`(CSS `display:none`,见 `list-box-item.css` 新规则),因此文本与选择都保留。`hidden` 属性同时存在,但 `display:flex` 会覆盖它,故以属性选择器规则为准。
### 8.3 虚拟焦点
- `ListBox` 在 `virtualFocus()` 为真时:根 `tabindex=-1`、条目恒为 `tabindex=-1`(不再有 roving tab stop)、`focus()` 不调用条目的 `element.focus()`,只更新活动项、必要时滚动并 `await nextTick()`;`keydown/keyup` 直接返回,键盘由组合体输入框负责。
- `reveal(key)` 与 `focus(key)` 共用 `scrollRowIntoView`;`reveal` 只滚动并等待一帧,供组合体在**行挂载后**再读 `optionId`(虚拟化时序)。
- `ListBoxItem` 的 `isFocused`/`data-focused` 在虚拟焦点下取 `active === id`;`isFocusVisible` 要求键盘意图(`keyboard`)成立。
- `active` 在虚拟焦点下直接读 `owner.activeKey.value`,ListBox 不另存一份活动状态;`focus()` 在用户点击条目时把该 key 写回 `activeKey`(值相同则不写),不建立互相 watch 的环。
### 8.4 验证页
`apps/playground/src/FilterVirtualLab.vue`(`http://localhost:5182/?filter-virtual`),载体是 `packages/vue/tests/fixtures/FilterVirtualCombobox.vue` —— 一个**最小组合框验证夹具**(不是公共 ComboBox,也不会被导出)。三个实例:过滤 + 虚拟焦点、受控拒绝、关闭过滤的对照。
### 8.5 自动化结果(B02a)
`tests/collection-filter-virtual.test.ts`(10 项,已加入 `test:unit` 列表):
1. 输入“狗”后只剩 1 行可见,4 行 `data-filtered` 且计算样式 `display: none`,行仍挂载、文本完整。
2. 先选中“猫”再把“猫”过滤掉:已选文本仍是“猫”,该行仍 `aria-selected="true"`。
3. 过滤后方向键只在可见且非禁用的行之间移动(只剩“鸟”时反复按下仍为“鸟”)。
4. 输入框保持 `document.activeElement`;两次 ArrowDown 后 `aria-activedescendant` 指向**已挂载**的 `data-slot="list-box-item"` 节点,该节点可聚焦属性为 `-1`、`data-focused="true"`、处于可见状态;列表根 `tabindex=-1`。
5. 过滤使可见集合变化后 `aria-activedescendant` 同步更新;无匹配时移除该属性且无可见行。
6. 点击行不会把焦点从输入框移走。
7. 受控拒绝:`proposal` 发出两次(`cat`、`dog`),`update:modelValue` 未发出、已接受值保持 null;父级改为接受后同一提案可再次提交成功。
8. IME:composition 期间的输入不触发过滤,`isComposing` 的 Enter 不确认选择;compositionend 后才过滤。
9. `reveal()` 不抢外部焦点,并给出已挂载行的 `optionId`(该行的文本可查)。
10. 独立 ListBox 回归:仍移动真实焦点、保持一个 Tab 入口、无 `data-filtered`。
全量:`pnpm typecheck` 通过;`pnpm test` **75 文件 / 995 项**通过(含既有 ListBox/Select/其它套件,无新增失败);`pnpm build:playground`、`pnpm build:docs` 通过;Prettier 与 `git diff --check` 通过。
### 8.6 浏览器证据(5182,Chromium/IAB)
- 输入“狗”:可见行 `["狗"]`,4 行 `data-filtered` 且 `display: none`,计数器“已隐藏:4”。
- `document.activeElement` 仍是 `[data-test="harness-input"]`;`aria-activedescendant="v-1"`,该节点 `data-slot="list-box-item"`、文本“狗”、`data-focused="true"`、`tabindex="-1"`、`display: flex`;列表根 `tabindex="-1"`;被过滤行 `tabindex="-1"`。
- 清空输入后两次 ArrowDown → 活动项“鸟”,Enter → 已选“鸟”、输入框回写“鸟”;再输入“猫”后可见行只剩“猫”,已选仍为“鸟”,且“鸟”行保持 `aria-selected="true"`(过滤不影响已接受值)。
- 受控拒绝实例:ArrowDown + Enter 后已选仍为“无”、无 `aria-selected`、焦点仍在输入框。
- 关闭过滤实例:输入“狗”后“已隐藏:0”、5 行可见。
**未验证**:真实 IME 组合输入(自动化后端无法产生系统 IME 序列,只用 `CompositionEvent`/`isComposing` 覆盖);虚拟化窗口内 `reveal → 挂载 → optionId` 的浏览器时序(jsdom 无布局,留到 B02d 与 ComboBox 一起在浏览器验证);读屏软件。
### 8.7 待复核要点
- 是否接受把 `optionId`/`reveal` 放在 `OwnedCollection`(集合侧实现)而不是 `CollectionOwner`(组合体实现);`reveal` 的语义是“滚动并等待挂载”,不改变选择。
- `data-filtered` 采用 CSS `display:none`(而非卸载)以保住文本与选择;如果集成人更希望卸载,需要同时提供“完整数据”的独立来源,否则被过滤的已选项会失去标签。
- `virtualFocus` 下 ListBox 完全不处理键盘(`keydown/keyup` 提前返回);组合体必须自己实现方向键、Enter、Esc、Tab 与 IME 守卫。
## 9. B02a 复核返修(六类问题,第二轮)
按 [B02a 复核](/private/tmp/B02a-review.md) 集中修复,独立提交。复核探针已固化到 `tests/b02a-review.probe.test.ts` 并加入 `test:unit`,5 项从全红转为全绿;本轮再加 8 项自有回归,合计 `tests/collection-filter-virtual.test.ts` 18 项。
| # | 问题 | 修复 | 证据 |
| --- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1 | 真实指针点击丢输入焦点(落到 body) | `ListBoxItem` 在虚拟焦点下对 `pointerdown` 与 `mousedown` 的默认焦点转移做 `preventDefault`;嵌套交互控件(`button/a/input/select/textarea/[contenteditable]`)例外;调用方已 `preventDefault` 时不再插手;pointerdown 的按下态逻辑保留 | 浏览器:填“狗”后**真实点击**选项,`document.activeElement` 仍为 input,已选“狗”;单测断言 `pointerdown`/`mousedown` 均 `defaultPrevented`,嵌套 `<button>` 不被取消 |
| 2 | 虚拟窗口仍用完整 items | `visibleItems` 作为 `source`,`useVirtualList` 窗口/总高度、`scrollRowIntoView` 索引、`aria-posinset/setsize` 全部改为可见集合;完整数据仍由 `known()` 提供;`empty` 插槽改为“可见集合为空”时渲染(items 模式) | 浏览器:40 行 → 过滤“第 7”后 `renderedSlots` 40→1、wrapper 高度 1280px→**32px**、`setsize` 40→**1**、`posinset` 1、“已隐藏:39”,焦点仍在输入框;单测断言 6→1 且无固定高度占位 |
| 3 | 普通长列表 reveal/focus 不滚动 | `revealRow()` 区分两条路径:虚拟化按索引滚窗口并 `waitForMountedRow`(最多 3 帧,`requestAnimationFrame` 优先,非浏览器回退 `nextTick`);非虚拟化等一帧后对已挂载行 `scrollIntoView({block:"nearest"})`。`focus` 的虚拟分支复用同一路径,并用 `revealRequest` 令牌丢弃被过滤/关闭/卸载作废的旧请求 | 单测:`scrollIntoView` spy 在无布局环境下被调用且不抢焦点;探针同项通过 |
| 4 | 新增 `hidden` 覆盖调用方属性 | 根因是 `mergeProps` 在后一个值为 `undefined` 时会**删除**前一个键。改为仅在“被过滤且调用方未设置 hidden”时才附加 `hidden`(条件展开,不写 `undefined` 键);`data-filtered` 仍由样式表 `display:none` 负责 | 探针“preserves caller hidden”通过;新增断言:可见行不带 `hidden`,被过滤行同时有 `hidden` 与 `data-filtered` |
| 5 | 指针后键盘意图无法恢复 | `CollectionView` 增加可选 `keyboardIntent?: Ref<boolean>`;`ListBox` 直接**复用**该 ref(不再 `ref(true)`),指针按下仍写 false,组合体在键盘事件里写 true,单一状态源 | 单测与浏览器均通过:指针点击行后再 ArrowDown,活动行同时有 `data-focused="true"` 与 `data-focus-visible="true"` |
| 6 | 验证夹具关联串实例、关闭后残留 activedescendant | 夹具改用 `useId()` 生成 `label/input/list` 三个实例唯一 id;新增 `resolveActiveId()`(要求开放、key 存在、`optionId` 已挂载、未被 `data-filtered` 隐藏)并用 `watchEffect` 统一从“键盘/指针/过滤/开关”任一来源同步;关闭即移除属性 | 浏览器:34 个 id 全部唯一;Escape 后 `aria-expanded="false"` 且 `aria-activedescendant` 为 null;指针点击“鸟”后 activedescendant 同步为“鸟”;过滤掉活动行或关闭时属性消失(单测覆盖三种场景) |
### 9.1 其他变更
- `ListBox` 模板重构为:非 `items` 模式渲染默认插槽;`items` 模式先按可见集合决定是否渲染 `empty`,虚拟化只渲染可见窗口,非虚拟化仍渲染**全部**行(由 `data-filtered` 隐藏),因此复核要求“非虚拟集合保留被过滤成员 DOM”成立。
- 夹具新增 `virtualized` 模式与 `empty` 通道,验证页新增第 4 个实例(40 行 + 虚拟窗口)用于过滤/窗口/高度/posinset 的浏览器烟测。
- 夹具的 `action` 不再关闭弹层(注释说明原因):关闭行为属于 B02b 的 ComboBox,保留开放状态才能观察指针→活动键→activedescendant 的同步。
### 9.2 本轮验证
- `pnpm typecheck` 通过;`pnpm test` **76 文件 / 1008 项**(既有 995 - 10 + 18 + 复核探针 5 = 1008,含全部 ListBox/Select 回归);`pnpm build:playground`、`pnpm build:docs` 通过;Prettier 与 `git diff --check` 通过。
- 浏览器(5182):真实点击保焦点、指针后键盘意图恢复、Escape 清除 activedescendant、40→1 的窗口/高度/posinset 收缩、多实例 id 唯一,均已逐项核对。
### 9.3 仍然未验证
- 真实系统 IME(自动化后端无法产生系统组合输入,只有 `CompositionEvent`/`isComposing` 覆盖)。
- 读屏软件播报。
- 验证夹具自身的滚动容器:库里 `.hv-list-box` 的 `overflow: clip` 会盖掉示例给的 `overflow-y-auto`(夹具与公共样式的组合问题,不影响协议;B02b 的 ComboBox 用 Popover 容器承载滚动,需单独核对)。这一条已记录,不当作已解决。
## 10. B02a 第二轮复核返修(五类问题,第三轮)
按 [第二轮返修说明](/private/tmp/B02a-round2-review.md) 集中修复,独立提交。复核探针固化为 `tests/b02a-round2-review.probe.test.ts`(7 项,已加入 `test:unit`),从全红转为全绿;自有回归增至 `tests/collection-filter-virtual.test.ts` 26 项。
| # | 问题 | 修复 |
| --- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| R1 | 虚拟化验证不成立:容器 `overflow: clip`、`max-h` 未生效、40 行全挂载、远端活动项在视口外;夹具传 `text` 而完整数据读 `textValue`,未挂载项无法被过滤 | 验证页给自己的虚拟实例**显式内联**受限滚动容器(`max-height: 144px; overflow-y: auto`,不全局改 ListBox 样式),实测 `clientHeight 144 / scrollHeight 1448 / overflow auto`;行高契约统一为 `rowHeight=36`(与 `.hv-list-box-item` 的 36px 一致),实测行高 36;夹具新增 `listItems` 映射(`id/textValue/isDisabled`)作为 `items` 传入,100 条数据中未挂载的 `Row 99` 可被过滤(原来回退空文本失败) |
| R2 | 请求令牌在滚动之后才检查,过期请求仍滚一次 | `revealRow(key, valid)` 把有效性检查推进到**每个副作用之前**与每个 await/帧之后;`isValid` 覆盖最新令牌、未卸载、未禁用、仍在可见集合、且 `owner.isOpen?.() !== false`;虚拟化等待改为「已注册且 `element.isConnected`」的有界帧轮询;新增 `CollectionView.isOpen`(关闭即取消) |
| R3 | 动态移除/禁用的活动项仍可提交 | ListBox 新增响应式协调:虚拟焦点下活动 key 不再属于可见且启用的集合、或集合已关闭时,把共享 `activeKey` 清空(不自行另选,选择权留给组合体);夹具的确认动作再校验「仍在 eligible 且 open」,通过才发 `proposal`/接受;已接受值作为历史保留并记住接受时的标签 |
| R4 | 保焦点破坏按下态;嵌套控件仍触发父选项;`contenteditable` 判断不全 | `onPointerdown` 先读调用方是否已取消,再调焦点守卫,合法按下态得以保留;抽出 `NESTED_INTERACTIVE` 选择器(含 `a[href]`、`summary`、`[role=button/link/menuitem/checkbox/radio/switch/tab/option]`、`[contenteditable]:not([contenteditable="false"])`,覆盖 `""`/`true`/`plaintext-only`)并**同时用于 pointerdown 与 click**:来自嵌套控件的点击不再激活父选项,控件自己的事件不被拦截 |
| R5 | 静态插槽集合没有过滤空状态 | ListBox 模板改为:静态分支也渲染 `empty` 通道(`visibleIsEmpty` 且集合已稳定,避免首帧误报),隐藏成员仍保留以留住元数据;`items` 非虚拟/虚拟两种模式继续用可见集合判空 |
### 10.1 浏览器证据(5182,Chromium/IAB,40 行虚拟实例)
- 打开后:`clientHeight 144`、`scrollHeight 1448`、`overflow-y auto`、`max-height 144px`、**挂载行 10**(4 可见 + overscan),行高 **36**,虚拟内容总高 1440。
- `ArrowUp` 从第 0 行回绕到第 39 行:`scrollTop 0 → 1304`、挂载行 8、**活动行位于受限视口内**(相对顶部 104,viewport 144),焦点仍在输入框。
- 输入“第 39”:挂载行 **1**、`scrollHeight 44`、活动项为“第 39 行”、焦点仍在输入框(未挂载过的远端项也能命中)。
- 清空过滤:挂载行恢复 10、`scrollHeight 1448`、`scrollTop 0`;手动 `scrollTo(0)` 后 `aria-activedescendant` 仍指向已挂载、未过滤的 `role="option"`。
### 10.2 本轮验证汇总
`pnpm typecheck` 通过;`pnpm test` **77 文件 / 1024 项**通过(26 项自有 + 5 项第一轮探针 + 7 项第二轮探针,其余为既有 ListBox/Select 等回归);`pnpm build:docs`、`pnpm build:playground`、Prettier、`git diff --check` 通过。
### 10.3 仍未验证
- 真实系统 IME 与读屏软件(自动化后端无法产生系统组合输入;本轮不新增门槛)。
- 指针按下态的浏览器原生序列(自动化只能给出 click,`pointerdown` 单独按下态由 jsdom 覆盖);嵌套控件点击隔离同样以 jsdom 覆盖(浏览器侧需注入 DOM,未做)。