feat(autocomplete): migrate Autocomplete with popover-owned filtering
Port the fixed-baseline Autocomplete onto the B02a collection protocol. The trigger is the single Tab entry; opening moves focus to the search box inside the popover, which owns filtering, aria-activedescendant and keyboard navigation while the list only tracks an active row. - Seven parts: root (field, value/open models, form value, owner wiring), trigger (anchor + activation), value (scoped slot with selectedItems and selectedText instead of copied item subtrees), indicator, clear button, popover (measured trigger width), filter (owns the search text and composes the inner SearchFieldInput into the combobox control). - Filter exposes input-value/update:inputValue/inputChange, matching ComboBox. - Confirming the active row in multiple mode now toggles it instead of replacing the accepted set (ComboBox shared the same bug). - ComboBox shows the accepted label on the first render via a display value that falls back to the selected record, so preselected fields are readable before the selection watcher runs (including SSR). - Popovers measure the anchor with a ResizeObserver because Reka does not publish the anchor size; the caller's root class is merged again so the trigger fills the field width. - 24 baseline previews, Chinese doc page, navigation, 17 behaviour tests, 5 demo/document contracts and an independent tarball consumption script for both pickers. Not included: the full bundler/NodeNext type matrix (B02d).
This commit is contained in:
@@ -298,3 +298,55 @@ export interface CollectionOwner extends CollectionView {
|
||||
- 弹层内浮层选项的真实鼠标点击:本 IAB 会话对 floating 定位的 option 报 `not actionable`,改用键盘在页面验证,指针保焦点行为由 B02a 的 harness 与单测覆盖(记录为环境限制,未写成已通过)。
|
||||
- 真实系统 IME、读屏软件、逐像素视觉对比。
|
||||
- 独立 tarball 产物消费(B02d)。
|
||||
|
||||
## 12. B02c:Autocomplete 全部部件、文档与示例(已实现)
|
||||
|
||||
### 12.1 部件与映射
|
||||
|
||||
| 部件 | 实现要点 |
|
||||
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `Autocomplete` | 字段上下文(`kind: "group"`,`Label` 关联到触发器)、值/开关模型、集合 owner(`filter`/`virtualFocus`/`activeKey`/`keyboardIntent`/`isOpen`)、隐藏表单输入 + 原生校验代理、关闭后焦点回到触发器 |
|
||||
| `AutocompleteTrigger` | `role="button"` 的可点击容器,是唯一 Tab 入口;`aria-haspopup`/`-expanded`/`-controls`、`Enter`/`Space`/`ArrowDown` 打开、`Escape` 关闭、`Delete`/`Backspace` 清除;同时注册为弹层 anchor |
|
||||
| `AutocompleteValue` | 值/占位符文本,作用域插槽 `{ isPlaceholder, defaultChildren, selectedText, selectedItems }`(不复制 item 子树) |
|
||||
| `AutocompleteIndicator` | 装饰性 chevron(`aria-hidden`),`data-open` 时由 CSS 旋转 180° |
|
||||
| `AutocompleteClearButton` | 原生按钮,`aria-label="Clear selection"`、`tabindex="-1"`、`data-empty`,无选择时隐藏;点击不触发触发器切换 |
|
||||
| `AutocompletePopover` | 复用 `PopoverContent`(非模态);宽度按 anchor 实测(`ResizeObserver` → `--trigger-width`);`onOpenAutoFocus` 把焦点交给搜索框 |
|
||||
| `AutocompleteFilter` | 拥有搜索文本(`:input-value` / `@input-change`)、`filter` 谓词,并把弹层内 `SearchFieldInput` 组合成 `role="combobox"` 控件(值绑定、ARIA、方向键/Enter/Escape/Tab、IME 守卫) |
|
||||
|
||||
焦点所有权:触发器 → 打开时搜索框(`role="combobox"`,`aria-activedescendant` 指向已挂载可见的 option)→ 选择/关闭后回到触发器;列表在虚拟焦点下不处理键盘、不移动 DOM 焦点。
|
||||
|
||||
### 12.2 示例与文档
|
||||
|
||||
24 个示例(`apps/docs/demos/autocomplete-*.vue`)顺序与固定正文一致: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。文档页 `apps/docs/components/autocomplete.md` 含用法/结构/24 预览/样式参考/7 个部件 API 与事件/作用域插槽说明/无障碍;导航加入 `pickers`。
|
||||
|
||||
示例与框架适配(逐项记录):
|
||||
|
||||
1. 本库无 `useFilter` hook:`AutocompleteFilter` 的默认谓词即不区分大小写的包含匹配;异步与虚拟化示例传 `:filter="() => true"`,因为集合已由数据源/调用方过滤(源示例同样不对集合二次过滤)。
|
||||
2. 无 `Virtualizer` / `ListLayout`:虚拟滚动用 `ListBox` 的 `items` + `virtualized` + `row-height`,行加 `class="h-full"` 填满固定行高。
|
||||
3. 上游多选 `Autocomplete.Value` 会复制 item 子树;本库改为提供 `selectedItems` / `selectedText`,示例用 `TagGroup` 自行渲染标签。
|
||||
4. `asynchronous-filtering`:源请求 SWAPI,改为本地异步源(300ms、每查询一个 `AbortController`、加载中不显示清除按钮、空状态)。
|
||||
5. `user-selection-multiple`:源同时传 `defaultValue` 与受控 `value`,Vue 侧用 `v-model` 初值表达同一行为。
|
||||
6. `location-search`:源的每次 filter 调用都重置定时器,这里合并为一个定时器并在卸载时清理(无行为差异)。
|
||||
|
||||
### 12.3 自验证(本轮)
|
||||
|
||||
- `pnpm typecheck` 通过;`pnpm test` **81 文件 / 1073 项**通过(新增 `tests/autocomplete.test.ts` 17 项、`tests/autocomplete-demos.test.ts` 5 项,均已加入 `test:unit`);`pnpm build:docs`、`pnpm build:playground`、Prettier、`git diff --check` 通过。
|
||||
- 行为测试覆盖:触发器单一 Tab 入口与 ARIA、打开后焦点进入搜索框、输入过滤与活动项、`Enter` 选择后关闭并回焦、`Escape`/`Tab` 关闭不选择、IME 不确认、清除按钮、禁用不可开、禁用项跳过、空结果空状态、`allows-empty-collection`、受控开关(接受与拒绝)、父级拒绝保持值、多选累加与 `aria-selected`、隐藏输入提交 key、`required` 经 `Form` 守卫阻止提交、调用方 `aria-label` 落到触发器与搜索框。
|
||||
- 演示契约:24 个预览与文件一一对应、无 `className`/`@gravity-ui/icons`/自绘 `<path>`、每个示例渲染可命名的触发器、代表示例(default/multiple-select/with-sections/custom-styles/virtualization)能打开并给出有效 `aria-activedescendant`、required 示例输出两个隐藏输入并被 `Form` 拦下。
|
||||
- 浏览器(5182):29 个触发器;首个示例打开后焦点在搜索框(`role=combobox`)、`aria-activedescendant` 指向 `role=option`、6 个 option;输入 “Te” 后仅 Texas 可见且活动项为 Texas、焦点仍在搜索框;多选示例 `Enter` 后保持打开并显示标签;空集合示例打开即显示“未找到结果”;分组示例 12 行 3 组(北美洲/欧洲/亚洲,`scrollHeight` 540);虚拟化示例 1000 行只挂载 12 行、`scrollHeight` 50012/`clientHeight` 320,`End` 后 `scrollTop` 49692 且活动项在视口内并已挂载;2 个禁用触发器 `tabindex=-1`、`aria-disabled`;清除按钮 `aria-label="Clear selection"`/`tabindex=-1`/`data-empty`;弹层宽度 256 = 触发器宽度;浅/深色下触发器背景分别为 `oklch(1 0 0)` / `oklch(0.2103 0.0059 285.89)`;控制台无 error/warning。
|
||||
- 独立 tarball 消费(`/private/tmp/b02-consumer`):`consume-b02-pickers.mjs` 通过——13 个导出、两族 SSR HTML 契约(角色、触发器、表单名、预选标签)、12 条 `hv-autocomplete*` 与 `hv-combo-box*` CSS 选择器、两族变体函数。
|
||||
|
||||
### 12.4 本轮修复的真实缺陷
|
||||
|
||||
1. `Autocomplete` 根的调用方 `class` 被丢弃(`restAttrs` 过滤后未合并)→ 触发器退化为内容宽度(110px)。已改为合并 `attrs.class`,实测 256px。
|
||||
2. 弹层宽度取不到 anchor 尺寸(Reka 未启用 size 中间件,`--reka-popper-anchor-width` 为空)→ 新增 `useTriggerWidth`(`ResizeObserver` 实测)供 ComboBox 与 Autocomplete 共用,实测弹层宽度 = 触发器宽度。
|
||||
3. `AutocompleteTrigger` 未注册为弹层 anchor → 弹层定位与宽度都错误;已注册。
|
||||
4. `AutocompleteFilter` 缺 `input-change` / `update:inputValue` 事件(与 ComboBox 不一致)→ 已补齐,两个受控示例改用该事件,不再依赖原生 `@input`。
|
||||
5. 多选下 `Enter` 确认会替换而不是累加选择(ComboBox 同样存在)→ 两个组件的 `confirmActive` 在 multiple 时改为切换该 key。
|
||||
6. ComboBox 预选值在首帧/SSR 为空 → 输入框显示值新增 `displayInputValue`(无输入文本时回落到已选标签),首帧即可读。
|
||||
|
||||
### 12.5 仍未验证
|
||||
|
||||
- 真实系统 IME、读屏软件、逐像素视觉对比。
|
||||
- 弹层内浮层选项的真实鼠标点击(本 IAB 对 floating option 报 `not actionable`;键盘与 DOM 断言覆盖,指针保焦点由 B02a harness 覆盖)。
|
||||
- 完整独立产物类型正反例矩阵(bundler / NodeNext 的公开类型契约)留在 B02d。
|
||||
|
||||
Reference in New Issue
Block a user