fix(pickers): repair data-slot selectors and align demos with the baseline

The CSS port prefixed data-slot attribute values with hv-, so the compiled
selectors never matched the DOM and the ComboBox trigger chevron never
rotated. Only class names get the prefix; slot values stay byte-identical.

Also commit typed text as the value when allowsCustomValue finds no record,
restore the baseline search-field shell in all 24 Autocomplete demos, and
correct both doc pages against the pinned upstream sources.
This commit is contained in:
maoyingjie
2026-10-25 17:46:32 +08:00
parent 5e688680e9
commit 3e9be13b19
40 changed files with 634 additions and 225 deletions
+79 -56
View File
@@ -118,7 +118,10 @@ import {
### 变体
Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `secondary`(低强调、无阴影,适用于 Surface 内)。
Autocomplete 组件支持两种视觉变体:
- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景
- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内
<ComponentPreview name="autocomplete-variants" />
@@ -144,7 +147,7 @@ Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `se
### 允许空选项
`allows-empty-collection` 允许集合为空时仍可打开并交互。
`allows-empty-collection` 属性允许集合为空时仍可使用自动完成,适用于列表初始为空或全部被过滤掉的场景。
<ComponentPreview name="autocomplete-allows-empty-collection" />
@@ -156,7 +159,13 @@ Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `se
<ComponentPreview name="autocomplete-multiple-select" />
上游提醒:多选时 `Autocomplete.Value` 默认会把每个选中项在 `ListBox.Item` 中的子树复制到触发器里。本库不复制子树,而是给 `AutocompleteValue` 的作用域插槽提供 `selectedItems`(`{ key, textValue }[]`)与 `selectedText`(纯文本),由调用方决定渲染纯文本还是标签:
::: warning
多选模式下,上游 `Autocomplete.Value` 会把每个选中项在 `ListBox.Item` 中的 children 复制到触发器中,并以符合当前语言环境的分隔符连接:仅包含文本的选项显示为纯文本,但复选框、头像等可见内容会随每个选中项各出现一次;函数形式的 children 也始终以 `isSelected: false` 调用。
本库不复制子树,而是给 `AutocompleteValue` 的作用域插槽提供 `selectedItems`(`{ key, textValue }[]`)与 `selectedText`(纯文本),因此不存在上述重复渲染问题,但函数形式的插槽同样不携带 `isSelected`。
:::
如需以纯文本显示选中值,请使用 `selectedText`:
```vue
<AutocompleteValue>
@@ -166,7 +175,7 @@ Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `se
</AutocompleteValue>
```
需要可移除标签时用 `selectedItems` 自行渲染,示例见「多选」。
如需可移除标签等更丰富的布局,可基于 `selectedItems` 自行渲染内容,示例见「高级示例 / 标签组选择」。
### 受控组件
@@ -196,7 +205,7 @@ Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `se
### 表面样式
在 Surface 内使用时请用 `variant="secondary"`。
在 [Surface](/components/surface) 组件内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
<ComponentPreview name="autocomplete-on-surface" />
@@ -237,6 +246,7 @@ Autocomplete 支持两种视觉变体:`primary`(默认,带阴影)与 `se
### 全局 CSS
可使用 `@layer components` 指令自定义 Autocomplete 组件类。
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
@@ -277,7 +287,7 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
#### 基础类
- `.hv-autocomplete` - 根容器
- `.hv-autocomplete__trigger` - 展示值并打开弹层的容器
- `.hv-autocomplete__trigger` - 触发自动完成的按钮(本库渲染为 `role="button"` 的容器)
- `.hv-autocomplete__value` - 值或占位符
- `.hv-autocomplete__clear-button` - 清除已选值的按钮
- `.hv-autocomplete__indicator` - 下拉指示图标
@@ -287,15 +297,13 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
#### 变体类
- `.hv-autocomplete--primary` - 带阴影的主变体(默认)
- `.hv-autocomplete--secondary` - 无阴影的次变体
- `.hv-autocomplete--secondary` - 无阴影的次变体,适用于 Surface 内
#### 状态类
- `.hv-autocomplete[data-invalid="true"]` - 无效状态
- `.hv-autocomplete[data-disabled="true"]` - 禁用状态
- `.hv-autocomplete__trigger[data-focus-visible="true"]` - 触发器聚焦
- `.hv-autocomplete__trigger[data-disabled="true"]` - 触发器禁用
- `.hv-autocomplete__trigger[data-open="true"]` - 展开状态
- `.hv-autocomplete__value[data-placeholder="true"]` - 占位符状态
- `.hv-autocomplete__clear-button[data-empty="true"]` - 无选择时隐藏清除按钮
- `.hv-autocomplete__indicator[data-open="true"]` - 打开时图标旋转 180°
@@ -303,45 +311,48 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
### 交互状态
- **Hover**:触发器上 `:hover` 或 `[data-hovered="true"]`
- **Focus**:触发器与搜索框上 `:focus-visible`
- **Disabled**:根与触发器上 `[data-disabled="true"]`
- **Open**:触发器上 `[data-open="true"]`、指示器上 `[data-open="true"]`
- **Focus**:触发器上 `:focus-visible` 或 `[data-focus-visible="true"]`
- **Disabled**:触发器上 `[data-disabled="true"]`
- **Open**:指示器上 `[data-open="true"]`
## API 参考
### 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 \| null` | - | 当前值(受控,`v-model`) |
| `value` | `string \| number \| Array \| null` | - | 同 `model-value`(受控别名) |
| `default-value` | `string \| number \| Array \| null` | - | 初始值(非受控) |
| `is-required` | `boolean` | `false` | 是否必填 |
| `is-invalid` | `boolean` | `false` | 值是否无效 |
| `name` | `string` | - | 表单提交字段名(输出隐藏输入,多选以逗号连接) |
| `form` | `string` | - | 关联的表单 id |
| `full-width` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 |
| `as` | `string` | `'div'` | 自定义根元素标签 |
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| 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 绑定) |
事件:
| 事件 | 载荷 | 描述 |
| ------------------- | ----------------------------------- | ------------ |
| `update:modelValue` | `string \| number \| Array \| null` | 值变化 |
| `change` | 同上 | 值变化 |
| `update:open` | `boolean` | 弹层开关变化 |
| `openChange` | `boolean` | 弹层开关变化 |
| `clear` | - | 清除已选值 |
| 事件 | 载荷 | 描述 |
| ------------------- | ----------------------------------------------------- | ---------------------- |
| `update:modelValue` | `string \| number \| Array<string \| number> \| null` | 值变化(多选时为数组) |
| `change` | 同上 | 值变化 |
| `update:open` | `boolean` | 弹层开关变化 |
| `openChange` | `boolean` | 弹层开关变化 |
| `clear` | - | 清除已选值 |
### AutocompleteTrigger
@@ -378,17 +389,19 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
`aria-label` 默认 `Clear selection`,`tabindex="-1"`,无选择时 `data-empty="true"` 并隐藏。
`@click` 与内部清除处理会合并执行(Vue 事件数组);模板 ref 通过 `defineExpose` 暴露组件实例,需要 DOM 时取 `$el`。
### AutocompletePopover
| Prop | 类型 | 默认值 | 描述 |
| ------------------- | --------------------------- | ---------- | ----------------------------- |
| `placement` | 位置联合类型 | `'bottom'` | 相对触发器的位置 |
| `offset` | `number` | `8` | 偏移量 |
| `should-flip` | `boolean` | `true` | 空间不足时是否翻转 |
| `container-padding` | `number` | `12` | 距离视口边缘的最小间距 |
| `to` | `string \| HTMLElement` | - | 自定义 Portal 容器 |
| 默认插槽 | `Component` | - | 通常为 `AutocompleteFilter` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| Prop | 类型 | 默认值 | 描述 |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------- | ----------------------------- |
| `placement` | `'bottom' \| 'bottom left' \| 'bottom right' \| 'bottom start' \| 'bottom end' \| 'top' \| 'top left' \| 'top right' \| 'top start' \| 'top end' \| 'left' \| 'left top' \| 'left bottom' \| 'start' \| 'start top' \| 'start bottom' \| 'right' \| 'right top' \| 'right bottom' \| 'end' \| 'end top' \| 'end bottom'` | `'bottom'` | 相对触发器的位置 |
| `offset` | `number` | `8` | 偏移量 |
| `should-flip` | `boolean` | `true` | 空间不足时是否翻转 |
| `container-padding` | `number` | `12` | 距离视口边缘的最小间距 |
| `to` | `string \| HTMLElement` | - | 自定义 Portal 容器 |
| 默认插槽 | `Component` | - | 通常为 `AutocompleteFilter` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
宽度跟随触发器(`--trigger-width`)。
@@ -415,15 +428,23 @@ Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.co
`AutocompleteValue` 的默认插槽提供:
| 字段 | 类型 | 描述 |
| ----------------- | --------------------------- | -------------------------------------------- |
| `isPlaceholder` | `boolean` | 是否没有选中项 |
| `defaultChildren` | `string` | 缺省文本(选中项文本以逗号连接,否则占位符) |
| `selectedText` | `string` | 选中项 `textValue` 的连接文本 |
| `selectedItems` | `Array<{ key, textValue }>` | 选中项 |
| 字段 | 类型 | 描述 |
| ----------------- | ------------------------------ | -------------------------------------------- |
| `isPlaceholder` | `boolean` | 是否没有选中项 |
| `defaultChildren` | `string`(上游为 `ReactNode`) | 缺省文本(选中项文本以逗号连接,否则占位符) |
| `selectedText` | `string` | 选中项 `textValue` 的连接文本 |
| `selectedItems` | `Array<{ key, textValue }>` | 选中项 |
上游 `state.selectedItems`(节点)在本库中是上表的 `selectedItems`(数据);不复制 item 子树,见「多选」说明。
### 过滤函数(`useFilter` 替代)
上游页面导出的 `useFilter` hook(`contains` / `startsWith` / `endsWith` 与 `sensitivity`)在本库没有对应导出。替代方式:
- `AutocompleteFilter` 的默认谓词等价于 `useFilter({ sensitivity: "base" }).contains`(不区分大小写的包含匹配)
- 需要 `startsWith` / `endsWith` 等语义时,直接给 `:filter="(text, input) => …"` 传函数
- 数据源或调用方已过滤时(异步、虚拟化示例),传 `:filter="() => true"` 避免二次过滤
## 无障碍
Autocomplete 实现带过滤的 ARIA select 模式:
@@ -433,7 +454,9 @@ Autocomplete 实现带过滤的 ARIA select 模式:
- `ArrowDown` / `ArrowUp` / `Home` / `End` 在过滤后的可见集合中移动,`Enter` 确认,`Escape` 关闭,`Tab` 离开
- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交)
- 过滤不会清除已接受的选择;关闭后焦点回到触发器,弹层内不出现第二个 Tab 入口
- 支持禁用状态(`is-disabled` / `disabled-keys`)、带过滤的搜索、与原生表单集成(`name` / `form`)
- 未实测读屏软件的变更播报与逐项读屏行为,因此不声称已通过该层验收
## 相关组件
[ComboBox 组合框](./combo-box) · [Select 选择器](./select) · [ListBox 列表框](./list-box) · [SearchField 搜索框](./search-field)
[ListBox 列表框](./list-box) · [Popover 弹出框](./popover) · [Input 输入框](./input)