fix(pickers): rework B02 against the public review

Restore native clearing, unify the form/field protocol with the other choice
families (per-key entries, disabled and removed keys omitted, shared form
owner), synchronise ComboBox text by key identity, keep filtering out of open
composition sessions, and stop stealing focus when a controlled close is
rejected. Also merge caller refs/ARIA/disabled, implement asChild, autoFocus
and ComboBox variant, use a locale-aware base matcher, and put the indicator
class and slot on the icon element the ported CSS selects.
This commit is contained in:
maoyingjie
2026-10-25 20:40:24 +08:00
parent 3e9be13b19
commit e34b9debdf
24 changed files with 1808 additions and 236 deletions
+32 -28
View File
@@ -205,7 +205,7 @@ Autocomplete 组件支持两种视觉变体:
### 表面样式
在 [Surface](/components/surface) 组件内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
在 [Surface](/components/surface) 组件内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体(根类名为 `.hv-autocomplete--secondary`;弹层内的 `SearchField` 需要时再单独声明自己的变体)。
<ComponentPreview name="autocomplete-on-surface" />
@@ -319,30 +319,32 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
### Autocomplete
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ------------------------------------------------------------ | ------------------ | ----------------------------------------------------------------------------------- |
| `placeholder` | `string` | `'Select an item'` | 为空时显示的占位文本 |
| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 |
| `allows-empty-collection` | `boolean` | `false` | 是否允许空集合 |
| `is-open` | `boolean` | - | 弹层打开状态(受控,`v-model:open` 亦可) |
| `default-open` | `boolean` | `false` | 弹层默认打开状态(非受控) |
| `disabled-keys` | `Array<string \| number>` | - | 禁用项的 key |
| `is-disabled` | `boolean` | `false` | 是否禁用 |
| `model-value` | `string \| number \| Array<string \| number> \| null` | - | 当前值(受控,`v-model`);多选时为 key 数组 |
| `value` | `string \| number \| Array<string \| number> \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array<string \| number> \| null` | - | 初始值(非受控);多选时为 key 数组 |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `name` | `string` | - | 表单提交字段名(输出隐藏输入,多选以逗号连接) |
| `form` | `string` | - | 关联的表单 id |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体:`primary` 为默认带阴影样式;`secondary` 为低强调无阴影,适用于 Surface 内 |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
| `validate` | `(value) => string \| string[] \| true \| null \| undefined` | - | 校验函数;`native` 时提交会展示错误(上游无此 prop) |
| `validation-behavior` | `'native' \| 'aria'` | `'native'` | 原生校验阻止提交,或只用 ARIA 标记(上游无此 prop) |
| 默认插槽 | `{ isOpen, selectedKeys }` | - | 组件内容;作用域提供当前状态 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ------------------------------------------------------------ | ------------------ | --------------------------------------------------------------------------------------------------- |
| `placeholder` | `string` | `'Select an item'` | 为空时显示的占位文本 |
| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 |
| `allows-empty-collection` | `boolean` | `false` | 是否允许空集合 |
| `items` | `Array<{ id, textValue, isDisabled? }>` | - | 对应上游 `items: Iterable<T>`:关闭态的已选文本与提交校验都取自它,不传时以 `ListBox` 子项为集合 |
| `disabled` | `boolean` | `false` | `is-disabled` 的等价别名 |
| `is-open` | `boolean` | - | 弹层打开状态(受控,`v-model:open` 亦可) |
| `default-open` | `boolean` | `false` | 弹层默认打开状态(非受控) |
| `disabled-keys` | `Array<string \| number>` | - | 禁用项的 key |
| `is-disabled` | `boolean` | `false` | 是否禁用 |
| `model-value` | `string \| number \| Array<string \| number> \| null` | - | 当前值(受控,`v-model`);多选时为 key 数组 |
| `value` | `string \| number \| Array<string \| number> \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array<string \| number> \| null` | - | 初始值(非受控);多选时为 key 数组 |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `name` | `string` | - | 表单提交字段名(输出隐藏输入,多选以逗号连接) |
| `form` | `string` | - | 关联的表单 id |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体:`primary` 为默认带阴影样式;`secondary` 为低强调无阴影,适用于 Surface 内 |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 把首个插槽元素当作根节点:保留其属性、事件、ref 与子节点,并合并根类名与 `data-slot="autocomplete"` |
| `validate` | `(value) => string \| string[] \| true \| null \| undefined` | - | 校验函数;`native` 时提交会展示错误(上游无此 prop) |
| `validation-behavior` | `'native' \| 'aria'` | `'native'` | 原生校验阻止提交,或只用 ARIA 标记(上游无此 prop) |
| 默认插槽 | `{ isOpen, selectedKeys }` | - | 组件内容;作用域提供当前状态 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
事件:
@@ -354,6 +356,8 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
| `openChange` | `boolean` | 弹层开关变化 |
| `clear` | - | 清除已选值 |
提交层:单选输出一个同名条目,多选按每个 key 输出一个同名条目(`getAll(name)` 为数组);禁用项、被 `disabled-keys` 禁用以及已从集合删除的 key 不提交;集合从未提供过该 key(受控的关闭态值)时按调用方给出的值提交。`form` 同时作用于原生提交层与 reset 校验层。
### AutocompleteTrigger
| Prop | 类型 | 默认值 | 描述 |
@@ -387,7 +391,7 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
| 默认插槽 | `Component` | - | 自定义清除图标 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`aria-label` 默认 `Clear selection`,`tabindex="-1"`,无选择时 `data-empty="true"` 并隐藏。
`aria-label` 默认 `Clear selection`,`tabindex="-1"`,无选择时 `data-empty="true"` 并隐藏。调用方 `click` 处理器先执行:它调用 `preventDefault()` 时内部清除被取消;原生 `disabled` 与根上的禁用状态任一成立都会禁用按钮并跳过清除。
`@click` 与内部清除处理会合并执行(Vue 事件数组);模板 ref 通过 `defineExpose` 暴露组件实例,需要 DOM 时取 `$el`。
@@ -415,7 +419,7 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
| 默认插槽 | `Component` | - | 必须包含 `SearchField`(内含 `SearchFieldInput`)与 `ListBox` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
过滤文本由 `AutocompleteFilter` 拥有,并通过集合的过滤协议影响列表的显示与键盘导航;它不会清除已接受的选择。
过滤文本由 `AutocompleteFilter` 拥有,并通过集合的过滤协议影响列表的显示与键盘导航;它不会清除已接受的选择。组合输入期间列表保持原样,`compositionend` 后再提交一次过滤;会话内的 `Enter` 不确认选择。
受控文本用 `:input-value` + `@input-change`(或 `v-model:input-value`):
@@ -441,7 +445,7 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
上游页面导出的 `useFilter` hook(`contains` / `startsWith` / `endsWith` 与 `sensitivity`)在本库没有对应导出。替代方式:
- `AutocompleteFilter` 的默认谓词等价于 `useFilter({ sensitivity: "base" }).contains`(不区分大小写的包含匹配)
- `AutocompleteFilter` 的默认谓词等价于 `useFilter({ sensitivity: "base" }).contains`:按 `ConfigProvider` 的 locale 折叠大小写与变音符号,`cafe` 能命中 `Café`
- 需要 `startsWith` / `endsWith` 等语义时,直接给 `:filter="(text, input) => …"` 传函数
- 数据源或调用方已过滤时(异步、虚拟化示例),传 `:filter="() => true"` 避免二次过滤
+44 -39
View File
@@ -157,7 +157,7 @@ import {
<ComponentPreview name="combo-box-render-function" />
上游用 React `render` 函数替换根 DOM 元素;Vue 中对应 `as`(自定义根元素标签)与 `as-child`(自定义组件作为唯一根),示例通过根元素属性演示同一能力。
上游用 React `render` 函数替换根 DOM 元素;Vue 中对应 `as`(自定义根元素标签)与 `as-child`(把首个插槽元素当作根节点)。`as-child` 时该元素保留自己的属性、事件、ref 与子节点,并额外获得根类名与 `data-slot="combo-box"`,其余部件渲染在它内部;`a` 之外的可聚焦性由调用方负责。
### 菜单触发方式
@@ -171,7 +171,9 @@ import {
### 表单值
使用 `form-value` 控制提交表单时提交选中项的 key 还是文本。默认值为 `key`;`allows-custom-value` 为真时始终提交文本。多选时多个值以英文逗号连接。
使用 `form-value` 控制提交表单时提交选中项的 key 还是文本。默认值为 `key`;`allows-custom-value` 为真时始终提交当前输入文本。
提交层遵循本库选择类字段的统一约定:单选输出一个同名条目,多选按每个 key 输出一个同名条目(`FormData.getAll(name)` 得到数组,不再拼成逗号串);禁用或被 `disabled-keys` 禁用的项、以及从集合中删除的项都不提交;没有任何可提交值时整个字段不出现在 `FormData` 中。`form` 同时作用于可见输入与提交层,因此外部表单的校验与 reset 归属一致。
<ComponentPreview name="combo-box-form-value" />
@@ -276,43 +278,44 @@ ComboBox 组件使用以下 CSS 类([查看源码样式](https://github.com/he
### ComboBox
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ------------------------------------------------------------ | ------------------ | --------------------------------------------------------------------------------------------- |
| `input-value` | `string` | - | 当前输入值(受控,可用 `v-model:input-value`) |
| `default-input-value` | `string` | `''` | 默认输入值(非受控) |
| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 |
| `model-value` | `string \| number \| Array<string \| number> \| null` | - | 当前值(受控,`v-model`);多选时为 key 数组 |
| `value` | `string \| number \| Array<string \| number> \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array<string \| number> \| null` | - | 初始值(非受控);多选时为 key 数组 |
| `selected-key` | `string \| number \| null` | - | 单选当前 key(受控别名) |
| `default-selected-key` | `string \| number \| null` | - | 单选初始 key(非受控别名) |
| `items` | `Array<{ id, textValue, isDisabled? }>` | - | 数据驱动渲染;不传时以 `ListBox` 的静态子项作为集合 |
| `disabled-keys` | `Array<string \| number>` | - | 禁用项的 key |
| `open` / `is-open` | `boolean` | - | 弹层打开状态(受控,可用 `v-model:open`) |
| `default-open` | `boolean` | `false` | 弹层默认打开状态(非受控) |
| `default-filter` | `(text: string, inputValue: string) => boolean` | 包含匹配 | 自定义过滤函数 |
| `is-disabled` | `boolean` | `false` | 是否禁用 |
| `is-read-only` | `boolean` | `false` | 只读:值可聚焦但不可修改 |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `validate` | `(value) => string \| string[] \| true \| null \| undefined` | - | 校验函数;`validation-behavior="native"` 时提交会展示错误,实时校验请改用 `is-invalid` |
| `validation-behavior` | `'native' \| 'aria'` | `'native'` | 原生校验阻止提交,或只用 ARIA 标记 |
| `name` | `string` | - | 表单提交时的字段名(输出隐藏输入) |
| `form` | `string` | - | 关联的表单 id |
| `form-value` | `'key' \| 'text'` | `'key'` | 提交 key 还是文本;`allows-custom-value` 时始终提交文本 |
| `auto-complete` | `string` | `'off'` | 输入框 `autocomplete` |
| `auto-focus` | `boolean` | `false` | 挂载时聚焦 |
| `allows-custom-value` | `boolean` | `false` | 允许输入不在列表中的值 |
| `allows-empty-collection` | `boolean` | `false` | 无匹配项时仍可打开弹层 |
| `menu-trigger` | `'focus' \| 'input' \| 'manual'` | `'focus'` | 打开弹层的交互方式 |
| `should-focus-wrap` | `boolean` | `false` | 方向键是否在首尾循环 |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体:`primary` 为默认带阴影样式;`secondary` 为低强调无阴影,适用于 Surface 内 |
| `placeholder` | `string` | `'Select an item'` | `ComboBoxValue` 的缺省占位文本(上游根上没有该 prop,由 `ComboBoxValue` 的 placeholder 表达) |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
| 默认插槽 | `{ isOpen, inputValue, selectedKey, selectedKeys }` | - | 组件内容;作用域提供当前状态 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| Prop | 类型 | 默认值 | 描述 |
| ------------------------- | ------------------------------------------------------------ | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `input-value` | `string` | - | 当前输入值(受控,可用 `v-model:input-value`) |
| `default-input-value` | `string` | `''` | 默认输入值(非受控) |
| `selection-mode` | `'single' \| 'multiple'` | `'single'` | 单选或多选 |
| `model-value` | `string \| number \| Array<string \| number> \| null` | - | 当前值(受控,`v-model`);多选时为 key 数组 |
| `value` | `string \| number \| Array<string \| number> \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array<string \| number> \| null` | - | 初始值(非受控);多选时为 key 数组 |
| `selected-key` | `string \| number \| null` | - | 单选当前 key(受控别名) |
| `default-selected-key` | `string \| number \| null` | - | 单选初始 key(非受控别名) |
| `items` | `Array<{ id, textValue, isDisabled? }>` | - | 数据驱动渲染;不传时以 `ListBox` 的静态子项作为集合 |
| `disabled-keys` | `Array<string \| number>` | - | 禁用项的 key |
| `open` / `is-open` | `boolean` | - | 弹层打开状态(受控,可用 `v-model:open`) |
| `default-open` | `boolean` | `false` | 弹层默认打开状态(非受控) |
| `default-filter` | `(text: string, inputValue: string) => boolean` | 不区分大小写与重音的包含匹配 | 自定义过滤函数;默认谓词按 `ConfigProvider` 的 locale 折叠大小写与变音符号(`Café` 可由 `cafe` 命中) |
| `is-disabled` | `boolean` | `false` | 是否禁用 |
| `disabled` | `boolean` | `false` | `is-disabled` 的等价别名 |
| `is-read-only` | `boolean` | `false` | 只读:值可聚焦但不可修改 |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `validate` | `(value) => string \| string[] \| true \| null \| undefined` | - | 校验函数;`validation-behavior="native"` 时提交会展示错误,实时校验请改用 `is-invalid` |
| `validation-behavior` | `'native' \| 'aria'` | `'native'` | 原生校验阻止提交,或只用 ARIA 标记 |
| `name` | `string` | - | 表单提交时的字段名(输出隐藏输入) |
| `form` | `string` | - | 关联的表单 id |
| `form-value` | `'key' \| 'text'` | `'key'` | 提交 key 还是文本;`allows-custom-value` 时始终提交文本 |
| `auto-complete` | `string` | `'off'` | 输入框 `autocomplete` |
| `auto-focus` | `boolean` | `false` | 挂载时聚焦 |
| `allows-custom-value` | `boolean` | `false` | 允许输入不在列表中的值 |
| `allows-empty-collection` | `boolean` | `false` | 无匹配项时仍可打开弹层 |
| `menu-trigger` | `'focus' \| 'input' \| 'manual'` | `'focus'` | 打开弹层的交互方式 |
| `should-focus-wrap` | `boolean` | `false` | 方向键是否在首尾循环 |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体:`primary` 为默认带阴影样式;`secondary` 为低强调无阴影,适用于 Surface 内。根上的取值由内部 `Input` 继承,`Input` 自己显式设置时以子控件为准 |
| `placeholder` | `string` | `'Select an item'` | `ComboBoxValue` 的缺省占位文本(上游根上没有该 prop,由 `ComboBoxValue` 的 placeholder 表达) |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
| 默认插槽 | `{ isOpen, inputValue, selectedKey, selectedKeys }` | - | 组件内容;作用域提供当前状态 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
事件:
@@ -381,6 +384,8 @@ ComboBox 实现 ARIA combobox 模式:
- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交)
- 过滤不会清除已接受的选择;被过滤掉的已选项仍保留标签
- 弹层内不出现第二个 Tab 入口,关闭后移除 `aria-activedescendant`
- 组合输入期间保留已经渲染的行,直到 `compositionend` 才提交一次过滤;会话内的 `Enter` 不会确认选择(同时检查会话状态、`isComposing` 与 `keyCode 229`)
- 必填只由校验阻止无效提交,不阻止用户把值编辑为空;`native` 与 `aria` 两种校验都按同一份约束判断
- 支持禁用状态(`is-disabled` / `disabled-keys`)、输入过滤式搜索、与原生表单集成(`name` / `form` / `form-value`)与自定义值(`allows-custom-value`)
- 未实测读屏软件的变更播报与逐项读屏行为,因此不声称已通过该层验收