feat(collection): add optional filtering and virtual focus for composite pickers

ComboBox and Autocomplete need two capabilities the collection protocol did not
express: filtering a collection by an external input value, and keeping DOM focus
in the composite's input while an active option is tracked for
aria-activedescendant.

Both are opt-in members on CollectionOwner plus optionId/reveal on the owned
collection. Standalone ListBox and Select provide none of them and keep their
previous behaviour: DOM focus still moves into the list, one roving tab stop, no
row filtering.

Filtered rows stay mounted with data-filtered (display:none) so their text and
accepted selection survive; only display, aria positions and keyboard navigation
see the visible subset. Under virtual focus the list never takes DOM focus,
never exposes a roving tab stop, and leaves keyboard handling to the composite;
reveal() scrolls a virtualized window and waits for the row to mount before the
composite reads its id.

Includes the playground verification page, a ten-case suite and the B02a section
of the migration report.
This commit is contained in:
maoyingjie
2026-10-25 14:53:49 +08:00
parent fa73cafd20
commit 1b972a78ba
11 changed files with 849 additions and 30 deletions
+86 -4
View File
@@ -1,6 +1,6 @@
# B02 ComboBox / Autocomplete — 审计与设计(阶段一,未实现)
# B02 ComboBox / Autocomplete — 审计、协议扩展与实现
分支 `feat/b02-combobox-autocomplete`,起点 `84c4893`(本地 main,含 B03 修复 `20d34d7` 与合并 `eb10b6a`)。**本文件当前只记录审计、契约与设计决策;组件、文档、演示、测试尚未实现**,不能据此认为本批已交付。
分支 `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 被其他工作区占用)。
@@ -68,9 +68,9 @@ CSS 要点:trigger 是 `min-h-9 rounded-field border bg-field px-3 py-2 text-s
- 输入:`Input`(ComboBox)与 `SearchField`/`SearchFieldGroup`/`SearchFieldInput`(Autocomplete 弹层内搜索)。
- 展示值:`TagGroup`/`Tag`(多选 Value 渲染)、`Avatar`、`EmptyState`、`Spinner`、`Surface`。
## 5. 阻断性共享缺口(需决策,未擅自改协议)
## 5. 共享缺口与 B02a 已落地的可选协议
两个缺口都在集成人维护的 `collection` 与 `list-box` 目录内,本批按约定**不静默修改**。
两个缺口都在集成人维护的 `collection` 与 `list-box` 目录内。用户已明确授权本批做**支持过滤与输入端虚拟焦点所必需的最小扩展**(覆盖原提示词中禁止修改这些共享文件的限制),并保留 ListBox/Select 的默认行为。实现见第 8 节。
### 5.1 集合过滤(ComboBox `defaultFilter`、Autocomplete `Filter filter`)
@@ -105,3 +105,85 @@ CSS 要点:trigger 是 `min-h-9 rounded-field border bg-field px-3 py-2 text-s
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 守卫。