fix(collection): close the six review gaps in filtering and virtual focus

- Keep the composite input focused on real pointer presses: cancel the default
  focus transfer for plain row presses only, leaving nested controls and already
  cancelled callers alone.
- Drive the virtual window, total height, reveal index and aria positions from
  the visible subset, and render the empty channel when filtering removes every
  row. Non-virtualized rows stay mounted and are hidden, so their text and
  accepted selection survive.
- Scroll mounted rows for reveal/focus outside virtualization, wait for a
  virtualized row to mount (frame-based, bounded), and drop reveal requests that
  filtering, closing or unmounting made obsolete.
- Stop overwriting caller `hidden`: mergeProps deletes an earlier key when a
  later value is undefined, so the filter now only adds `hidden` when the caller
  did not set it.
- Share keyboard modality through an optional `keyboardIntent` ref so a pointer
  press no longer leaves the active row without data-focus-visible.
- Give the verification harness instance-unique ids and derive
  aria-activedescendant from open state, mounted id and filter visibility.

Adds the reviewer probe plus eight regressions, and a virtualized harness
instance for the browser smoke of filtering plus window/height/positions.
This commit is contained in:
maoyingjie
2026-10-25 16:28:33 +08:00
parent 1b972a78ba
commit 50ad3ca180
9 changed files with 511 additions and 55 deletions
@@ -187,3 +187,33 @@ export interface CollectionOwner extends CollectionView {
- 是否接受把 `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 容器承载滚动,需单独核对)。这一条已记录,不当作已解决。