feat(combo-box): migrate ComboBox with input-owned focus and filtering

Port the fixed-baseline ComboBox onto the B02a collection protocol: the input
keeps real DOM focus, the list only tracks an active row, and filtering reuses
the owner filter instead of a second collection kernel.

- Five parts: root (field text control, value/input/open models, form value,
  owner wiring), InputGroup (composes the caller's Input into the combobox
  control and anchors the popover), Trigger, Value, Popover.
- Closed-state metadata is planned from the slot tree, so filtering, the initial
  value label and form-value="text" work before the list has mounted.
- Selection text syncs on selection changes only, so allows-custom-value typing
  is not overwritten; read-only and disabled are injected onto the composed
  input because it reads the text-field protocol, not the field context.
- 24 baseline previews, the Chinese doc page, navigation, 19 behaviour tests and
  6 demo/document contracts. Async demo uses a local async source with abort,
  loading and incremental append (documented adaptations).

Not included: independent tarball consumption (B02d).
This commit is contained in:
maoyingjie
2026-10-25 16:59:16 +08:00
parent ff486fb665
commit 46e4df7e85
46 changed files with 4322 additions and 2 deletions
@@ -249,3 +249,52 @@ export interface CollectionOwner extends CollectionView {
## 11. B02a 阶段复核通过
以 [B02a 阶段验收](B02a-acceptance.md) 为当前结论。集成人补齐静态空集合、嵌套控件按下状态及探针独立运行的 scrollTo 环境初始化;最新全套为 77 文件 / 1026 项,无未处理错误。仅放行 B02a,在当前任务分支连续完成 B02b、B02c、B02d,公共组件和完整示例尚未验收。
## 11. B02b:ComboBox 全部部件、文档与示例(已实现)
起点 `ff486fb`(含 B02a 验收 `B02a-acceptance.md`)。5 个部件、24 个基线示例、1 页中文文档、导航、19 项行为测试与 6 项演示/文档测试全部落地。**未做**独立产物消费(按四段安排在 B02d 统一完成)。
### 11.1 部件与映射
| 部件 | 实现要点 |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ComboBox` | 字段上下文(`kind: "text"`,`Label` 渲染 `<label for>`)、值/输入值/开关三套受控模型、集合 owner(`filter` / `virtualFocus` / `activeKey` / `keyboardIntent` / `isOpen`)、隐藏表单输入、`form-value` key/text |
| `ComboBoxInputGroup` | 组合调用方的 `Input`:注入 `role="combobox"`、`aria-expanded` / `-controls` / `-activedescendant` / `-autocomplete` / `-haspopup` / `-describedby`、`required`、`readonly`、`disabled`、值绑定与键盘处理;同时把它注册为弹层的 anchor |
| `ComboBoxTrigger` | 原生按钮、`data-open`、`tabindex=-1`(整个组合框只保留输入框一个 Tab 入口)、默认基线 chevron(展开时 CSS 旋转) |
| `ComboBoxValue` | 已选值文本(逗号连接)与 `data-placeholder`,支持作用域插槽 `{ isPlaceholder, selectedItems }` |
| `ComboBoxPopover` | 复用现有 `PopoverContent`(非模态、Teleport、外部点击、退出动画),`--trigger-width` / anchor point 变量,`onOpenAutoFocus` 阻止焦点进入弹层 |
**键盘与焦点所有权**:真实焦点始终在输入框;`ArrowDown/Up`、`Home/End`、`Enter`、`Escape`、`Tab` 与 IME(`isComposing` / `keyCode 229`)一律由组合体处理,集合在 `virtualFocus` 下不处理键盘、不移动 DOM 焦点、不提供 roving Tab;`aria-activedescendant` 指向真实挂载且可见的 option。
**关闭态元数据**:`metadata.ts` 的 `planComboBoxItems` 从插槽树收集 `ListBox` / `ListBoxItem`(含 `items` 驱动的渲染函数、`ListBoxSection`、`disabledKeys`),使关闭时也能过滤、显示初始值标签、解析 `form-value="text"`;`items` 属性优先,插槽计划不完整时回退到已挂载 registry。
### 11.2 示例与文档
24 个示例全部来自 `apps/docs/demos/combo-box-*.vue`,顺序与固定正文一致(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)。文档页 `apps/docs/components/combo-box.md` 含用法、组件结构、24 个预览、样式参考、5 个部件的 API 表与事件表、无障碍;导航加入 `pickers` 分类。
示例适配(逐项记录,未以占位替代):
1. `asynchronous-loading`:源示例请求 SWAPI;改为本地异步源(300ms、每查询一个 `AbortController`、加载中状态、`加载更多` 增量追加、空状态),状态机与源一致。
2. `ListBoxLoadMoreItem` 在本库不存在:用显式「加载更多」按钮 + `Spinner` 状态行替代(与既有 `select-asynchronous-loading` 同做法)。
3. `required` / `custom-validation`:源的 `alert` 改为页内 `role="status"` 文案(沿用 `select-required` 约定),仍读取真实 `FormData`。
4. `custom-value`:源示例本身没有 `ComboBox.Value`,保持同样的结构(选中项文本回写输入框)。
5. `render-function`:React `render` → 根元素 `as="div" data-custom="foo"`。
### 11.3 验证
- `pnpm typecheck` 通过;`pnpm test` **79 文件 / 1051 项**通过(新增 `tests/combo-box.test.ts` 19 项、`tests/combo-box-demos.test.ts` 6 项,均已加入 `test:unit`);`pnpm build:docs`、Prettier、`git diff --check` 通过。
- 行为测试覆盖:单一 combobox 节点与 `label for`、`aria-*` 连线、聚焦打开、输入过滤、Enter 确认并回写文本、Escape 关闭、IME 不确认、文本与选中不匹配时清除、`allows-custom-value` 保留输入、父级拒绝后仍未选中且可再次提案、外部改值同步、禁用 key 跳过、禁用不打开、只读不可改、多选值与过滤后保留已接受键、隐藏输入提交 key/text、`required` 原生阻止提交、Description/FieldError 进入 `aria-describedby`、`menu-trigger` 三态、触发器切换。
- 演示测试:预览清单与 24 个文件一一对应、无 `className`/`@gravity-ui/icons`/自绘 `<path>`、每个示例都渲染真实 combobox(含唯一 id 与 `aria-expanded`)、代表示例能打开并给出有效 `aria-activedescendant`、初始选中项的文本进入输入框、form-value 示例输出两个隐藏输入。
- 浏览器(5182,Chromium/IAB):24 个示例共 28 个 combobox;首个示例输入“猫”后 `expanded=true`、焦点仍在输入框、`aria-activedescendant` 指向 `role="option"` 的“猫”、列表 6 个 option 中 2 个可见(“猫”“熊猫”为包含匹配)、列表 `tabindex=-1`;`ArrowDown`+`Enter` 后输入框回写“猫”、列表关闭、焦点仍在输入;`Escape` 关闭并移除 `aria-activedescendant`;三个预选示例的输入框显示“猫”;禁用示例的输入框原生 `disabled`;`ComboBoxValue` 显示占位文本“未选择任何动物”;触发器 `tabindex=-1`;form-value 示例输出 `animal` / `animal-text` 隐藏输入。
### 11.4 本轮新增经验
- 关闭态的集合元数据必须由插槽计划提供:`ListBox` 未挂载时 registry 为空,过滤、初始标签与 `form-value="text"` 都会失效。
- 选中值的文本同步只能跟「选择变化」走,不能跟「输入值变化」走,否则 `allows-custom-value` 下用户输入会被立刻覆盖。
- 只读/禁用必须显式注入到组合出来的 `Input`(它读的是 `TextField` 上下文,不认 `fieldContextKey`)。
### 11.5 仍未验证
- 弹层内浮层选项的真实鼠标点击:本 IAB 会话对 floating 定位的 option 报 `not actionable`,改用键盘在页面验证,指针保焦点行为由 B02a 的 harness 与单测覆盖(记录为环境限制,未写成已通过)。
- 真实系统 IME、读屏软件、逐像素视觉对比。
- 独立 tarball 产物消费(B02d)。