merge: consolidate validated migration history into primary checkout
# Conflicts: # AGENTS.md # docs/compatibility.md # docs/verification.md # package.json
This commit is contained in:
@@ -0,0 +1,466 @@
|
||||
---
|
||||
title: Autocomplete 自动完成
|
||||
description: 结合选择与过滤,让用户从选项列表中搜索并选择
|
||||
component: autocomplete
|
||||
group: 组件
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Autocomplete,
|
||||
AutocompleteClearButton,
|
||||
AutocompleteFilter,
|
||||
AutocompleteIndicator,
|
||||
AutocompletePopover,
|
||||
AutocompleteTrigger,
|
||||
AutocompleteValue,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
SearchField,
|
||||
SearchFieldGroup,
|
||||
SearchFieldInput,
|
||||
SearchFieldSearchIcon,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Autocomplete class="w-[256px]" placeholder="选择州/省">
|
||||
<Label>州</Label>
|
||||
<AutocompleteTrigger>
|
||||
<AutocompleteValue />
|
||||
<AutocompleteClearButton />
|
||||
<AutocompleteIndicator />
|
||||
</AutocompleteTrigger>
|
||||
<AutocompletePopover>
|
||||
<AutocompleteFilter>
|
||||
<SearchField aria-label="搜索州名">
|
||||
<SearchFieldGroup>
|
||||
<SearchFieldSearchIcon />
|
||||
<SearchFieldInput placeholder="搜索州名…" />
|
||||
</SearchFieldGroup>
|
||||
</SearchField>
|
||||
<ListBox>
|
||||
<ListBoxItem id="california" text-value="California">
|
||||
California
|
||||
</ListBoxItem>
|
||||
<ListBoxItem id="texas" text-value="Texas">Texas</ListBoxItem>
|
||||
</ListBox>
|
||||
</AutocompleteFilter>
|
||||
</AutocompletePopover>
|
||||
</Autocomplete>
|
||||
</template>
|
||||
```
|
||||
|
||||
<ComponentPreview name="autocomplete-default" />
|
||||
|
||||
触发器是唯一 Tab 入口;打开后焦点进入弹层内的搜索框,由它负责过滤、`aria-activedescendant` 与方向键导航,列表只维护活动行。
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Autocomplete,
|
||||
AutocompleteClearButton,
|
||||
AutocompleteFilter,
|
||||
AutocompleteIndicator,
|
||||
AutocompletePopover,
|
||||
AutocompleteTrigger,
|
||||
AutocompleteValue,
|
||||
Description,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
SearchField,
|
||||
SearchFieldGroup,
|
||||
SearchFieldInput,
|
||||
SearchFieldSearchIcon,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Autocomplete placeholder="选择一项">
|
||||
<Label>分类</Label>
|
||||
<AutocompleteTrigger>
|
||||
<AutocompleteValue />
|
||||
<AutocompleteClearButton />
|
||||
<AutocompleteIndicator />
|
||||
</AutocompleteTrigger>
|
||||
<Description>说明</Description>
|
||||
<AutocompletePopover>
|
||||
<AutocompleteFilter>
|
||||
<SearchField aria-label="搜索分类">
|
||||
<SearchFieldGroup>
|
||||
<SearchFieldSearchIcon />
|
||||
<SearchFieldInput placeholder="搜索…" />
|
||||
</SearchFieldGroup>
|
||||
</SearchField>
|
||||
<ListBox>
|
||||
<ListBoxItem id="news" text-value="资讯">
|
||||
资讯
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</ListBox>
|
||||
</AutocompleteFilter>
|
||||
</AutocompletePopover>
|
||||
</Autocomplete>
|
||||
</template>
|
||||
```
|
||||
|
||||
上游的对象式子部件 `Autocomplete.Trigger` / `.Value` / `.Indicator` / `.Popover` / `.Filter` / `.ClearButton` 在本库中是具名导出 `AutocompleteTrigger` / `AutocompleteValue` / `AutocompleteIndicator` / `AutocompletePopover` / `AutocompleteFilter` / `AutocompleteClearButton`。
|
||||
|
||||
## 示例
|
||||
|
||||
### 变体
|
||||
|
||||
Autocomplete 组件支持两种视觉变体:
|
||||
|
||||
- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景
|
||||
- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内
|
||||
|
||||
<ComponentPreview name="autocomplete-variants" />
|
||||
|
||||
### 宽度充满
|
||||
|
||||
<ComponentPreview name="autocomplete-full-width" />
|
||||
|
||||
### 带描述
|
||||
|
||||
<ComponentPreview name="autocomplete-with-description" />
|
||||
|
||||
### 必填
|
||||
|
||||
<ComponentPreview name="autocomplete-required" />
|
||||
|
||||
### 禁用
|
||||
|
||||
<ComponentPreview name="autocomplete-disabled" />
|
||||
|
||||
### 含禁用选项
|
||||
|
||||
<ComponentPreview name="autocomplete-with-disabled-options" />
|
||||
|
||||
### 允许空选项
|
||||
|
||||
`allows-empty-collection` 属性允许集合为空时仍可使用自动完成,适用于列表初始为空或全部被过滤掉的场景。
|
||||
|
||||
<ComponentPreview name="autocomplete-allows-empty-collection" />
|
||||
|
||||
### 分组选项
|
||||
|
||||
<ComponentPreview name="autocomplete-with-sections" />
|
||||
|
||||
### 多选
|
||||
|
||||
<ComponentPreview name="autocomplete-multiple-select" />
|
||||
|
||||
::: warning
|
||||
多选模式下,上游 `Autocomplete.Value` 会把每个选中项在 `ListBox.Item` 中的 children 复制到触发器中,并以符合当前语言环境的分隔符连接:仅包含文本的选项显示为纯文本,但复选框、头像等可见内容会随每个选中项各出现一次;函数形式的 children 也始终以 `isSelected: false` 调用。
|
||||
|
||||
本库不复制子树,而是给 `AutocompleteValue` 的作用域插槽提供 `selectedItems`(`{ key, textValue }[]`)与 `selectedText`(纯文本),因此不存在上述重复渲染问题,但函数形式的插槽同样不携带 `isSelected`。
|
||||
:::
|
||||
|
||||
如需以纯文本显示选中值,请使用 `selectedText`:
|
||||
|
||||
```vue
|
||||
<AutocompleteValue>
|
||||
<template #default="{ isPlaceholder, defaultChildren, selectedText }">
|
||||
{{ isPlaceholder ? defaultChildren : selectedText }}
|
||||
</template>
|
||||
</AutocompleteValue>
|
||||
```
|
||||
|
||||
如需可移除标签等更丰富的布局,可基于 `selectedItems` 自行渲染内容,示例见「高级示例 / 标签组选择」。
|
||||
|
||||
### 受控组件
|
||||
|
||||
<ComponentPreview name="autocomplete-controlled" />
|
||||
|
||||
### 受控多选
|
||||
|
||||
<ComponentPreview name="autocomplete-controlled-multiple" />
|
||||
|
||||
### 受控展开状态
|
||||
|
||||
<ComponentPreview name="autocomplete-controlled-open-state" />
|
||||
|
||||
### 异步搜索
|
||||
|
||||
<ComponentPreview name="autocomplete-asynchronous-filtering" />
|
||||
|
||||
### 自定义指示器
|
||||
|
||||
<ComponentPreview name="autocomplete-custom-indicator" />
|
||||
|
||||
### 自定义展示值
|
||||
|
||||
用 `AutocompleteValue` 的作用域插槽自定义展示值。
|
||||
|
||||
<ComponentPreview name="autocomplete-custom-value" />
|
||||
|
||||
### 表面样式
|
||||
|
||||
在 [Surface](/components/surface) 组件内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体(根类名为 `.hv-autocomplete--secondary`;弹层内的 `SearchField` 需要时再单独声明自己的变体)。
|
||||
|
||||
<ComponentPreview name="autocomplete-on-surface" />
|
||||
|
||||
### 虚拟滚动
|
||||
|
||||
集合使用 `ListBox` 的 `items` + `virtualized` + `row-height` 渲染可见窗口,仅挂载视口内的行。
|
||||
|
||||
<ComponentPreview name="autocomplete-virtualization" />
|
||||
|
||||
## 高级示例
|
||||
|
||||
### 用户选择
|
||||
|
||||
<ComponentPreview name="autocomplete-user-selection" />
|
||||
|
||||
### 用户多选
|
||||
|
||||
<ComponentPreview name="autocomplete-user-selection-multiple" />
|
||||
|
||||
### 地点搜索
|
||||
|
||||
<ComponentPreview name="autocomplete-location-search" />
|
||||
|
||||
### 标签组选择
|
||||
|
||||
<ComponentPreview name="autocomplete-tag-group-selection" />
|
||||
|
||||
### 邮件收件人
|
||||
|
||||
<ComponentPreview name="autocomplete-email-recipients" />
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="autocomplete-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
可使用 `@layer components` 指令自定义 Autocomplete 组件类。
|
||||
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-autocomplete {
|
||||
@apply flex flex-col gap-1;
|
||||
}
|
||||
|
||||
.hv-autocomplete__trigger {
|
||||
@apply rounded-lg border border-border bg-surface p-2;
|
||||
}
|
||||
|
||||
.hv-autocomplete__value {
|
||||
@apply text-current;
|
||||
}
|
||||
|
||||
.hv-autocomplete__clear-button {
|
||||
@apply text-muted hover:text-foreground;
|
||||
}
|
||||
|
||||
.hv-autocomplete__indicator {
|
||||
@apply text-muted;
|
||||
}
|
||||
|
||||
.hv-autocomplete__popover {
|
||||
@apply rounded-lg border border-border bg-surface p-2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
||||
|
||||
### CSS 类
|
||||
|
||||
Autocomplete 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/autocomplete.css)):
|
||||
|
||||
#### 基础类
|
||||
|
||||
- `.hv-autocomplete` - 根容器
|
||||
- `.hv-autocomplete__trigger` - 触发自动完成的按钮(本库渲染为 `role="button"` 的容器)
|
||||
- `.hv-autocomplete__value` - 值或占位符
|
||||
- `.hv-autocomplete__clear-button` - 清除已选值的按钮
|
||||
- `.hv-autocomplete__indicator` - 下拉指示图标
|
||||
- `.hv-autocomplete__popover` - 弹层容器
|
||||
- `.hv-autocomplete__filter` - 过滤包装器
|
||||
|
||||
#### 变体类
|
||||
|
||||
- `.hv-autocomplete--primary` - 带阴影的主变体(默认)
|
||||
- `.hv-autocomplete--secondary` - 无阴影的次变体,适用于 Surface 内
|
||||
|
||||
#### 状态类
|
||||
|
||||
- `.hv-autocomplete[data-invalid="true"]` - 无效状态
|
||||
- `.hv-autocomplete__trigger[data-focus-visible="true"]` - 触发器聚焦
|
||||
- `.hv-autocomplete__trigger[data-disabled="true"]` - 触发器禁用
|
||||
- `.hv-autocomplete__value[data-placeholder="true"]` - 占位符状态
|
||||
- `.hv-autocomplete__clear-button[data-empty="true"]` - 无选择时隐藏清除按钮
|
||||
- `.hv-autocomplete__indicator[data-open="true"]` - 打开时图标旋转 180°
|
||||
|
||||
### 交互状态
|
||||
|
||||
- **Hover**:触发器上 `:hover` 或 `[data-hovered="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` | 是否允许空集合 |
|
||||
| `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 绑定) |
|
||||
|
||||
事件:
|
||||
|
||||
| 事件 | 载荷 | 描述 |
|
||||
| ------------------- | ----------------------------------------------------- | ---------------------- |
|
||||
| `update:modelValue` | `string \| number \| Array<string \| number> \| null` | 值变化(多选时为数组) |
|
||||
| `change` | 同上 | 值变化 |
|
||||
| `update:open` | `boolean` | 弹层开关变化 |
|
||||
| `openChange` | `boolean` | 弹层开关变化 |
|
||||
| `clear` | - | 清除已选值 |
|
||||
|
||||
提交层:单选输出一个同名条目,多选按每个 key 输出一个同名条目(`getAll(name)` 为数组);禁用项、被 `disabled-keys` 禁用以及已从集合删除的 key 不提交;集合从未提供过该 key(受控的关闭态值)时按调用方给出的值提交。`form` 同时作用于原生提交层与 reset 校验层。
|
||||
|
||||
### AutocompleteTrigger
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | --------------------------- | ------- | -------------------------------------------------------- |
|
||||
| `is-disabled` | `boolean` | `false` | 是否禁用触发器 |
|
||||
| 默认插槽 | `Component` | - | 通常为 `AutocompleteValue` + `ClearButton` + `Indicator` |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
触发器渲染为 `role="button"` 的可点击容器,是组件唯一的 Tab 入口(`tabindex="0"`;禁用时 `-1`),`Enter` / `Space` / `ArrowDown` 打开,`Escape` 关闭,`Delete` / `Backspace` 清除。
|
||||
|
||||
### AutocompleteValue
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------- | ----------------------------------------------------------------- | ------ | ----------------------------- |
|
||||
| 默认插槽 | `{ isPlaceholder, defaultChildren, selectedText, selectedItems }` | - | 自定义展示值 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
### AutocompleteIndicator
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------- | --------------------------- | ------ | -------------------------------- |
|
||||
| 默认插槽 | `Component` | - | 自定义指示器图形(默认为下箭头) |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
指示器是装饰元素(`aria-hidden`),激活由触发器负责。
|
||||
|
||||
### AutocompleteClearButton
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------- | --------------------------- | ------ | ----------------------------- |
|
||||
| 默认插槽 | `Component` | - | 自定义清除图标 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
`aria-label` 默认 `Clear selection`,`tabindex="-1"`,无选择时 `data-empty="true"` 并隐藏。调用方 `click` 处理器先执行:它调用 `preventDefault()` 时内部清除被取消;原生 `disabled` 与根上的禁用状态任一成立都会禁用按钮并跳过清除。
|
||||
|
||||
`@click` 与内部清除处理会合并执行(Vue 事件数组);模板 ref 通过 `defineExpose` 暴露组件实例,需要 DOM 时取 `$el`。
|
||||
|
||||
### AutocompletePopover
|
||||
|
||||
| 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`)。
|
||||
|
||||
### AutocompleteFilter
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| --------------------- | ------------------------------- | ---------------------- | ------------------------------------------------------------- |
|
||||
| `filter` | `(text, inputValue) => boolean` | 不区分大小写的包含匹配 | 自定义过滤函数 |
|
||||
| `input-value` | `string` | - | 受控搜索文本 |
|
||||
| `default-input-value` | `string` | `''` | 非受控初始搜索文本 |
|
||||
| 默认插槽 | `Component` | - | 必须包含 `SearchField`(内含 `SearchFieldInput`)与 `ListBox` |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
过滤文本由 `AutocompleteFilter` 拥有,并通过集合的过滤协议影响列表的显示与键盘导航;它不会清除已接受的选择。组合输入期间列表保持原样,`compositionend` 后再提交一次过滤;会话内的 `Enter` 不确认选择。
|
||||
|
||||
受控文本用 `:input-value` + `@input-change`(或 `v-model:input-value`):
|
||||
|
||||
| 事件 | 载荷 | 描述 |
|
||||
| ------------------- | -------- | ------------ |
|
||||
| `update:inputValue` | `string` | 搜索文本变化 |
|
||||
| `inputChange` | `string` | 搜索文本变化 |
|
||||
|
||||
### 作用域插槽与渲染信息
|
||||
|
||||
`AutocompleteValue` 的默认插槽提供:
|
||||
|
||||
| 字段 | 类型 | 描述 |
|
||||
| ----------------- | ------------------------------ | -------------------------------------------- |
|
||||
| `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`:按 `ConfigProvider` 的 locale 折叠大小写与变音符号,`cafe` 能命中 `Café`
|
||||
- 需要 `startsWith` / `endsWith` 等语义时,直接给 `:filter="(text, input) => …"` 传函数
|
||||
- 数据源或调用方已过滤时(异步、虚拟化示例),传 `:filter="() => true"` 避免二次过滤
|
||||
|
||||
## 无障碍
|
||||
|
||||
Autocomplete 实现带过滤的 ARIA select 模式:
|
||||
|
||||
- 触发器是唯一 Tab 入口,并带 `aria-haspopup="listbox"` / `aria-expanded` / `aria-controls`
|
||||
- 打开后焦点进入弹层内搜索框(`role="combobox"`),`aria-activedescendant` 指向已挂载且可见的活动选项
|
||||
- `ArrowDown` / `ArrowUp` / `Home` / `End` 在过滤后的可见集合中移动,`Enter` 确认,`Escape` 关闭,`Tab` 离开
|
||||
- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交)
|
||||
- 过滤不会清除已接受的选择;关闭后焦点回到触发器,弹层内不出现第二个 Tab 入口
|
||||
- 支持禁用状态(`is-disabled` / `disabled-keys`)、带过滤的搜索、与原生表单集成(`name` / `form`)
|
||||
- 未实测读屏软件的变更播报与逐项读屏行为,因此不声称已通过该层验收
|
||||
|
||||
## 相关组件
|
||||
|
||||
[ListBox 列表框](./list-box) · [Popover 弹出框](./popover) · [Input 输入框](./input)
|
||||
@@ -0,0 +1,309 @@
|
||||
---
|
||||
title: Calendar 日历
|
||||
description: 可组合日期选择器,支持月、周、日视图、导航与年份选择
|
||||
component: calendar
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import { Calendar } from "@heroui-vue/components";
|
||||
</script>
|
||||
```
|
||||
|
||||
<ComponentPreview name="calendar-basic" />
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Calendar,
|
||||
CalendarHeader,
|
||||
CalendarHeading,
|
||||
CalendarNavButton,
|
||||
CalendarGrid,
|
||||
CalendarGridHeader,
|
||||
CalendarGridBody,
|
||||
CalendarHeaderCell,
|
||||
CalendarCell,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
<template>
|
||||
<Calendar aria-label="活动日期">
|
||||
<CalendarHeader>
|
||||
<CalendarHeading />
|
||||
<CalendarNavButton slot="previous" />
|
||||
<CalendarNavButton slot="next" />
|
||||
</CalendarHeader>
|
||||
<CalendarGrid>
|
||||
<CalendarGridHeader v-slot="{ day }">
|
||||
<CalendarHeaderCell>{{ day }}</CalendarHeaderCell>
|
||||
</CalendarGridHeader>
|
||||
<CalendarGridBody v-slot="{ date }">
|
||||
<CalendarCell :date="date" />
|
||||
</CalendarGridBody>
|
||||
</CalendarGrid>
|
||||
</Calendar>
|
||||
</template>
|
||||
```
|
||||
|
||||
## 示例
|
||||
|
||||
### 禁用
|
||||
|
||||
<ComponentPreview name="calendar-disabled" />
|
||||
|
||||
### 只读
|
||||
|
||||
<ComponentPreview name="calendar-read-only" />
|
||||
|
||||
### 默认值
|
||||
|
||||
<ComponentPreview name="calendar-default-value" />
|
||||
|
||||
### 年份选择
|
||||
|
||||
`CalendarYearPickerTrigger`、`CalendarYearPickerGrid` 及其 body/cell 子组件提供集成的年份导航模式。
|
||||
|
||||
<ComponentPreview name="calendar-year-picker" />
|
||||
|
||||
### 受控组件
|
||||
|
||||
使用受控的 `v-model` 与 `v-model:focusedValue` 进行外部状态协调与自定义快捷操作。
|
||||
|
||||
<ComponentPreview name="calendar-controlled" />
|
||||
|
||||
### 日期范围限制
|
||||
|
||||
<ComponentPreview name="calendar-min-max-dates" />
|
||||
|
||||
### 不可用日期
|
||||
|
||||
使用 `isDateUnavailable` 阻止周末、节假日或已预订时段等日期。
|
||||
|
||||
<ComponentPreview name="calendar-unavailable-dates" />
|
||||
|
||||
### 月份周数
|
||||
|
||||
将 `weeksInMonth` 设为固定值(如 `6`)可在月份切换时保持网格高度稳定。在非公历 locale 中请谨慎使用,类似 `firstDayOfWeek`。
|
||||
|
||||
<ComponentPreview name="calendar-weeks-in-month" />
|
||||
|
||||
### 周视图
|
||||
|
||||
设置 `:visible-duration="{ weeks: n }"` 可一次显示一周或多周。导航按可见周范围前进。显示多周时使用 `pageBehavior="single"` 可每次移动一周。
|
||||
|
||||
<ComponentPreview name="calendar-week-view" />
|
||||
|
||||
### 日视图
|
||||
|
||||
设置 `:visible-duration="{ days: n }"` 可显示连续日期的滚动窗口。导航按可见日范围前进。显示多天时使用 `pageBehavior="single"` 可每次移动一天。
|
||||
|
||||
<ComponentPreview name="calendar-day-view" />
|
||||
|
||||
### 多选
|
||||
|
||||
设置 `selection-mode="multiple"` 允许选择多个日期。`modelValue`、`defaultValue` 与 `@change` 使用日期数组。
|
||||
|
||||
<ComponentPreview name="calendar-multiple-selection" />
|
||||
|
||||
### 聚焦值
|
||||
|
||||
使用 `v-model:focusedValue` 与 `@focusChange` 以编程方式控制聚焦日期。
|
||||
|
||||
<ComponentPreview name="calendar-focused-value" />
|
||||
|
||||
### 单元格标记
|
||||
|
||||
可自定义 `CalendarCell` 子节点,并使用 `CalendarCellIndicator` 显示事件等元数据。
|
||||
|
||||
<ComponentPreview name="calendar-with-indicators" />
|
||||
|
||||
### 自定义导航图标
|
||||
|
||||
向 `CalendarNavButton` 传入子节点以替换默认 chevron 图标。
|
||||
|
||||
<ComponentPreview name="calendar-custom-icons" />
|
||||
|
||||
### 多月份展示
|
||||
|
||||
使用 `visibleDuration` 与 `offset` 渲染多个月份网格,适用于预订与规划场景。
|
||||
|
||||
<ComponentPreview name="calendar-multiple-months" />
|
||||
|
||||
### 典型场景
|
||||
|
||||
<ComponentPreview name="calendar-booking-calendar" />
|
||||
|
||||
### 国际化日历
|
||||
|
||||
默认情况下,Calendar 使用用户 locale 的历法系统显示日期。可用 `ConfigProvider` 包裹 Calendar 并设置 [Unicode 历法 locale 扩展](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar#adding_a_calendar_in_the_locale_string) 来覆盖。
|
||||
|
||||
以下示例展示印度历法系统:
|
||||
|
||||
<ComponentPreview name="calendar-international-calendar" />
|
||||
|
||||
**Note:** `@change` 事件始终返回与 `value` 或 `defaultValue` 相同历法系统的日期(未提供 value 时为公历),无论显示 locale 如何。这确保应用逻辑在单一历法系统下一致运行,同时仍以用户偏好的格式显示日期。
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="calendar-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-calendar {
|
||||
@apply w-72 rounded-2xl border border-border bg-surface p-3 shadow-sm;
|
||||
}
|
||||
|
||||
.hv-calendar__heading {
|
||||
@apply text-sm font-semibold text-default-foreground;
|
||||
}
|
||||
|
||||
.hv-calendar__cell[data-selected="true"] {
|
||||
@apply bg-accent text-accent-foreground;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||||
|
||||
### CSS 类
|
||||
|
||||
Calendar 在 `packages/styles/components/calendar.css` 与 `packages/styles/components/calendar-year-picker.css` 中使用以下类:
|
||||
|
||||
- `.hv-calendar` - 根容器
|
||||
- `.hv-calendar__header` - 包含导航按钮与标题的头部行
|
||||
- `.hv-calendar__heading` - 当前月份标签
|
||||
- `.hv-calendar__nav-button` - 上/下月导航控件
|
||||
- `.hv-calendar__grid` - 主日期网格
|
||||
- `.hv-calendar__grid-header` - 星期标题行包装器
|
||||
- `.hv-calendar__grid-body` - 日期行包装器
|
||||
- `.hv-calendar__header-cell` - 星期标题单元格
|
||||
- `.hv-calendar__cell` - 可交互的日期单元格
|
||||
- `.hv-calendar__cell-indicator` - 日期单元格内的点指示器
|
||||
- `.hv-calendar-year-picker__trigger` - 年份选择器切换按钮
|
||||
- `.hv-calendar-year-picker__trigger-heading` - 年份选择器触发器内的标题文本
|
||||
- `.hv-calendar-year-picker__trigger-indicator` - 年份选择器触发器内的指示图标
|
||||
- `.hv-calendar-year-picker__year-grid` - 可选年份的覆盖网格
|
||||
- `.hv-calendar-year-picker__year-cell` - 单个年份选项
|
||||
|
||||
### 交互状态
|
||||
|
||||
Calendar 同时支持伪类与 状态 data 属性:
|
||||
|
||||
- **Selected**:`[data-selected="true"]`
|
||||
- **Today**:`[data-today="true"]`
|
||||
- **Unavailable**:`[data-unavailable="true"]`
|
||||
- **Outside month**:`[data-outside-month="true"]`
|
||||
- **Hovered**:`:hover` 或 `[data-hovered="true"]`
|
||||
- **Pressed**:`:active` 或 `[data-pressed="true"]`
|
||||
- **Focus visible**:`:focus-visible` 或 `[data-focus-visible="true"]`
|
||||
- **Disabled**:`:disabled` 或 `[data-disabled="true"]`
|
||||
|
||||
## API 参考
|
||||
|
||||
### Calendar
|
||||
|
||||
Calendar 保留固定基线的主要属性与部件,以 Vue 模型、具名部件和作用域插槽表达;不继承 React 专属属性。
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ----------------------- | ---------------------------------------------------------------------- | ----------------------- | --------------------------------------------------------------------------------------- |
|
||||
| `selectionMode` | `'single' \| 'multiple'` | `'single'` | 是否可选择单个或多个日期 |
|
||||
| `value` | `DateValue \| null` 或 `DateValue[] \| null` | - | 受控选中日期。`selectionMode` 为 `multiple` 时使用数组 |
|
||||
| `defaultValue` | `DateValue \| null` 或 `DateValue[] \| null` | - | 初始选中日期(非受控) |
|
||||
| `@change` | `(value: DateValue \| null)` 或 `(value: DateValue[] \| null) => void` | - | 选择变化时调用 |
|
||||
| `focusedValue` | `DateValue` | - | 受控聚焦日期 |
|
||||
| `@focusChange` | `(value: DateValue) => void` | - | 焦点移至其他日期时调用 |
|
||||
| `minValue` | `DateValue` | 历法感知的 `1900-01-01` | 最早可选日期 |
|
||||
| `maxValue` | `DateValue` | 历法感知的 `2099-12-31` | 最晚可选日期 |
|
||||
| `weeksInMonth` | `number` | - | 月份中的周数,覆盖 locale 默认值 |
|
||||
| `isDateUnavailable` | `(date: DateValue) => boolean` | - | 标记日期为不可用 |
|
||||
| `firstDayOfWeek` | `'sun' \| 'mon' \| 'tue' \| 'wed' \| 'thu' \| 'fri' \| 'sat'` | - | 覆盖 locale 默认的一周起始日 |
|
||||
| `pageBehavior` | `'visible' \| 'single'` | `'visible'` | 翻页按可见时长还是单个单位前进 |
|
||||
| `selectionAlignment` | `'start' \| 'center' \| 'end'` | `'center'` | 初始渲染时可见范围与选择的对齐方式 |
|
||||
| `isDisabled` | `boolean` | `false` | 禁用交互与选择 |
|
||||
| `isReadOnly` | `boolean` | `false` | 内容可读但不可更改选择 |
|
||||
| `isInvalid` | `boolean` | `false` | 标记日历为无效以显示验证 UI |
|
||||
| `visibleDuration` | `{months?: number; weeks?: number; days?: number}` | `{months: 1}` | 可见时间范围。月视图用 `{ months: n }`,周视图用 `{ weeks: n }`,日视图用 `{ days: n }` |
|
||||
| `defaultYearPickerOpen` | `boolean` | `false` | 内部年份选择器初始打开状态 |
|
||||
| `isYearPickerOpen` | `boolean` | - | 受控年份选择器打开状态 |
|
||||
| `@yearPickerOpenChange` | `(isOpen: boolean) => void` | - | 年份选择器打开状态变化时调用 |
|
||||
|
||||
### Composition Parts
|
||||
|
||||
| Component | Description |
|
||||
| ------------------------------------ | -------------------------------------------------------------------------- |
|
||||
| `CalendarHeader` | 导航与标题的头部容器 |
|
||||
| `CalendarHeading` | 可见范围的格式化标题。支持 `offset`(多月布局)与 `format`(月/年/日选项) |
|
||||
| `CalendarNavButton` | 上/下月导航控件(`slot="previous"` 或 `slot="next"`) |
|
||||
| `CalendarGrid` | 单个月份的日期网格(多月布局支持 `offset`) |
|
||||
| `CalendarGridHeader` | 星期标题容器 |
|
||||
| `CalendarGridBody` | 日期单元格 body 容器 |
|
||||
| `CalendarHeaderCell` | 星期标签单元格 |
|
||||
| `CalendarCell` | 单个日期单元格 |
|
||||
| `CalendarCellIndicator` | 自定义元数据的可选指示元素 |
|
||||
| `CalendarYearPickerTrigger` | 切换年份选择器模式的触发器 |
|
||||
| `CalendarYearPickerTriggerHeading` | 年份选择器触发器内的本地化标题内容 |
|
||||
| `CalendarYearPickerTriggerIndicator` | 年份选择器触发器内的切换图标 |
|
||||
| `CalendarYearPickerGrid` | 覆盖式年份选择网格容器 |
|
||||
| `CalendarYearPickerGridBody` | 年份网格单元格的 body 渲染器 |
|
||||
| `CalendarYearPickerCell` | 单个年份选项单元格 |
|
||||
|
||||
### Year Picker Parts
|
||||
|
||||
年份选择器支持日期格式与窗口选项。独立部件直接从包根导入,不需要 React 上下文。
|
||||
|
||||
| Component | Prop | 类型 | 默认值 | 描述 |
|
||||
| ---------------------------------- | -------------- | ---------------------------- | ------------------- | -------------------------------------------------------------------------- |
|
||||
| `CalendarYearPickerTriggerHeading` | `format` | `Intl.DateTimeFormatOptions` | - | 自定义月/年标签(如 `{month: 'short'}`) |
|
||||
| `CalendarYearPickerTriggerHeading` | `offset` | `{months?: number}` | - | 相对聚焦日期偏移标题(多月布局) |
|
||||
| `CalendarYearPickerGrid` | `format` | `Intl.DateTimeFormatOptions` | `{year: 'numeric'}` | 自定义年份单元格标签(纪元、历法等) |
|
||||
| `CalendarYearPickerGrid` | `visibleYears` | `number` | min–max 跨度 | 滑动窗口中显示的年份数。同时设置 `minValue` 与 `maxValue` 时默认为完整范围 |
|
||||
|
||||
### CalendarCell 作用域插槽
|
||||
|
||||
`CalendarCell` 的默认作用域插槽提供以下状态:
|
||||
|
||||
| Prop | 类型 | 描述 |
|
||||
| ---------------- | --------- | ---------------------- |
|
||||
| `formattedDate` | `string` | 单元格的本地化日期标签 |
|
||||
| `isSelected` | `boolean` | 日期是否选中 |
|
||||
| `isUnavailable` | `boolean` | 日期是否不可用 |
|
||||
| `isDisabled` | `boolean` | 单元格是否禁用 |
|
||||
| `isOutsideMonth` | `boolean` | 日期是否属于相邻月份 |
|
||||
|
||||
有关支持的历法系统及其标识符的完整列表,请参阅:
|
||||
|
||||
- [React Aria Calendar Implementations](https://react-aria.adobe.com/internationalized/date/Calendar#implementations)
|
||||
- [React Aria International Calendars](https://react-aria.adobe.com/Calendar#international-calendars)
|
||||
|
||||
### Related packages
|
||||
|
||||
- [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) — 所有日期组件使用的日期类型(`CalendarDate`、`CalendarDateTime`、`ZonedDateTime`)与工具
|
||||
- 本库 `ConfigProvider` — 覆盖子树的 locale
|
||||
- 本库 `ConfigProvider` — 统一 locale 与布局方向
|
||||
|
||||
## Vue 模型、DOM 与当前差异
|
||||
|
||||
- 单选使用 `v-model`(`DateValue | null`),多选设置 `selection-mode="multiple"` 并使用日期数组。`value` 为受控别名;显式 `modelValue` 优先,`defaultValue` 只捕获初始值。`@change` 与 `update:modelValue` 提供复制后的提案,父拒绝不会改变选中显示。
|
||||
- 焦点使用 `v-model:focusedValue`,年份面板使用 `v-model:isYearPickerOpen`。年份选择只改变聚焦年份,不改变已选日期。`defaultFocusedValue` 固定非受控初始视图;跨时区 SSR 建议传入明确日期。
|
||||
- `locale`、`dir` 优先于 ConfigProvider;`timeZone` 用于“今天”标记。显示历法与公开模型历法分离;默认上下界按公历 1900-01-01 至 2099-12-31 转换到显示历法,与上游部分历法的近似年份偏移算法有差异。
|
||||
- 根为 `role="group"`,网格为语义 table/grid,CalendarCell 输出 td/gridcell + 原生 button。`class`、ARIA、DOM 事件落在真正可交互按钮;取消 Cell 的键盘或点击事件可阻止默认处理,祖先需使用捕获事件取消。`isDateDisabled` 完全禁用日期;`isDateUnavailable` 保留焦点但阻止选择。
|
||||
- 左右/上下键移动日期,Home/End 到周边界,PageUp/PageDown 移动月份,Shift+PageUp/PageDown 移动年份;Enter/Space 选择。年份层为三列,方向键与 Home/End 移动,Escape 关闭并恢复触发器。
|
||||
- Calendar 本身不产生原生命名表单输入,也不消费 Label/FieldError 的字段协议;表单字段与 Picker 后续共享 DateField 基建。当前尚未迁移 RangeCalendar、DatePicker 和 TimeField,不把这些能力当作已完成。
|
||||
- `weeksInMonth` 是行数下限,不裁掉真实月份日期。`visibleYears` 是以聚焦年份为中心的窗口;默认显示完整 min/max 年份范围。复杂 era 可通过年份插槽中的 `date` 传给 CalendarYearPickerCell,避免仅凭数字年份混淆。
|
||||
- 键盘适配层针对安装版 Reka 缺少 week/day view、年份层及原生取消缺口使用本地日期视图算法;不运行 React,也不承诺完整 React Aria 类型或全部读屏行为等价。
|
||||
|
||||
## 相关组件
|
||||
|
||||
[DateField 日期字段](./date-field) · [Select 选择器](./select) · [Popover 弹出框](./popover)
|
||||
|
||||
Root 默认插槽还提供 `value`、`isDisabled`、`isReadOnly`、`isInvalid`、`focusedValue`、`visibleRange` 与 `isYearPickerOpen`。Cell 插槽还提供 `date`、`isOutsideVisibleRange`、`isToday`、`isHovered`、`isPressed`、`isFocused`、`isFocusVisible`。使用 `defaultFocusedValue`、`locale`、`dir`、`timeZone`、`isDateDisabled` 可显式设置初始视图、国际化和禁用规则。
|
||||
@@ -0,0 +1,394 @@
|
||||
---
|
||||
title: ComboBox 组合框
|
||||
description: 将文本输入与 ListBox 结合,用户可通过输入查询把选项列表过滤为匹配项
|
||||
component: combo-box
|
||||
group: 组件
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
ComboBox,
|
||||
ComboBoxInputGroup,
|
||||
ComboBoxPopover,
|
||||
ComboBoxTrigger,
|
||||
Input,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ComboBox class="w-[256px]">
|
||||
<Label>最喜欢的动物</Label>
|
||||
<ComboBoxInputGroup>
|
||||
<Input placeholder="搜索动物…" />
|
||||
<ComboBoxTrigger />
|
||||
</ComboBoxInputGroup>
|
||||
<ComboBoxPopover>
|
||||
<ListBox>
|
||||
<ListBoxItem id="cat" text-value="猫">猫</ListBoxItem>
|
||||
<ListBoxItem id="dog" text-value="狗">狗</ListBoxItem>
|
||||
</ListBox>
|
||||
</ComboBoxPopover>
|
||||
</ComboBox>
|
||||
</template>
|
||||
```
|
||||
|
||||
<ComponentPreview name="combo-box-default" />
|
||||
|
||||
输入框既是过滤条件也是控件的真实焦点:`ComboBoxInputGroup` 把 `Input` 组合成 `role="combobox"` 控件,键盘与 `aria-activedescendant` 都落在它身上,弹层里的列表只维护活动行,不抢焦点。
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
ComboBox,
|
||||
ComboBoxInputGroup,
|
||||
ComboBoxPopover,
|
||||
ComboBoxTrigger,
|
||||
ComboBoxValue,
|
||||
Description,
|
||||
Header,
|
||||
Input,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
ListBoxSection,
|
||||
Separator,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ComboBox>
|
||||
<Label>分类</Label>
|
||||
<ComboBoxInputGroup>
|
||||
<Input placeholder="搜索…" />
|
||||
<ComboBoxTrigger />
|
||||
</ComboBoxInputGroup>
|
||||
<!-- 展示已选中的值,主要用于多选 -->
|
||||
<ComboBoxValue />
|
||||
<Description>说明</Description>
|
||||
<ComboBoxPopover>
|
||||
<ListBox>
|
||||
<ListBoxItem id="news" text-value="资讯">
|
||||
<Label>资讯</Label>
|
||||
<Description>最新消息</Description>
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
<ListBoxSection>
|
||||
<Header>更多</Header>
|
||||
<ListBoxItem id="travel" text-value="旅行">
|
||||
<Label>旅行</Label>
|
||||
</ListBoxItem>
|
||||
</ListBoxSection>
|
||||
</ListBox>
|
||||
</ComboBoxPopover>
|
||||
</ComboBox>
|
||||
</template>
|
||||
```
|
||||
|
||||
上游的对象式子部件 `ComboBox.InputGroup` / `.Trigger` / `.Value` / `.Popover` 在本库中是具名导出 `ComboBoxInputGroup` / `ComboBoxTrigger` / `ComboBoxValue` / `ComboBoxPopover`。
|
||||
|
||||
## 示例
|
||||
|
||||
### 宽度充满
|
||||
|
||||
<ComponentPreview name="combo-box-full-width" />
|
||||
|
||||
### 带描述
|
||||
|
||||
<ComponentPreview name="combo-box-with-description" />
|
||||
|
||||
### 必填
|
||||
|
||||
<ComponentPreview name="combo-box-required" />
|
||||
|
||||
### 禁用
|
||||
|
||||
<ComponentPreview name="combo-box-disabled" />
|
||||
|
||||
### 含禁用选项
|
||||
|
||||
<ComponentPreview name="combo-box-with-disabled-options" />
|
||||
|
||||
### 分组选项
|
||||
|
||||
<ComponentPreview name="combo-box-with-sections" />
|
||||
|
||||
### 受控组件
|
||||
|
||||
<ComponentPreview name="combo-box-controlled" />
|
||||
|
||||
### 受控输入值
|
||||
|
||||
<ComponentPreview name="combo-box-controlled-input-value" />
|
||||
|
||||
### 异步加载
|
||||
|
||||
<ComponentPreview name="combo-box-asynchronous-loading" />
|
||||
|
||||
### 默认选中项
|
||||
|
||||
<ComponentPreview name="combo-box-default-selected-key" />
|
||||
|
||||
### 允许自定义值
|
||||
|
||||
<ComponentPreview name="combo-box-allows-custom-value" />
|
||||
|
||||
### 自定义指示器
|
||||
|
||||
<ComponentPreview name="combo-box-custom-indicator" />
|
||||
|
||||
### 自定义展示值
|
||||
|
||||
<ComponentPreview name="combo-box-custom-value" />
|
||||
|
||||
### 自定义过滤
|
||||
|
||||
<ComponentPreview name="combo-box-custom-filtering" />
|
||||
|
||||
### 渲染函数
|
||||
|
||||
<ComponentPreview name="combo-box-render-function" />
|
||||
|
||||
上游用 React `render` 函数替换根 DOM 元素;Vue 中对应 `as`(自定义根元素标签)与 `as-child`(把首个插槽元素当作根节点)。`as-child` 时该元素保留自己的属性、事件、ref 与子节点,并额外获得根类名与 `data-slot="combo-box"`,其余部件渲染在它内部;`a` 之外的可聚焦性由调用方负责。
|
||||
|
||||
### 菜单触发方式
|
||||
|
||||
使用 `menu-trigger` prop 控制 Popover 何时打开:
|
||||
|
||||
- `focus`(默认):输入框获得焦点时打开 Popover
|
||||
- `input`:用户编辑输入文本时打开 Popover
|
||||
- `manual`:仅当用户按下触发按钮或使用方向键时打开 Popover
|
||||
|
||||
<ComponentPreview name="combo-box-menu-trigger" />
|
||||
|
||||
### 表单值
|
||||
|
||||
使用 `form-value` 控制提交表单时提交选中项的 key 还是文本。默认值为 `key`;`allows-custom-value` 为真时始终提交当前输入文本。
|
||||
|
||||
提交层遵循本库选择类字段的统一约定:单选输出一个同名条目,多选按每个 key 输出一个同名条目(`FormData.getAll(name)` 得到数组,不再拼成逗号串);禁用或被 `disabled-keys` 禁用的项、以及从集合中删除的项都不提交;没有任何可提交值时整个字段不出现在 `FormData` 中。`form` 同时作用于可见输入与提交层,因此外部表单的校验与 reset 归属一致。
|
||||
|
||||
<ComponentPreview name="combo-box-form-value" />
|
||||
|
||||
### 校验行为
|
||||
|
||||
使用 `validation-behavior` 控制校验信息的展示方式:
|
||||
|
||||
- `native`(默认):值为空或无效时阻止表单提交
|
||||
- `aria`:实时显示错误,不阻止提交
|
||||
|
||||
<ComponentPreview name="combo-box-validation-behavior" />
|
||||
|
||||
### 自定义校验
|
||||
|
||||
使用 `validate` 在值无效时返回错误信息,有效时返回 `true`。
|
||||
|
||||
<ComponentPreview name="combo-box-custom-validation" />
|
||||
|
||||
### 只读
|
||||
|
||||
使用 `is-read-only` 将 ComboBox 设为只读。选中值可以获得焦点,但不能修改。
|
||||
|
||||
<ComponentPreview name="combo-box-read-only" />
|
||||
|
||||
### 多选
|
||||
|
||||
设置 `selection-mode="multiple"` 以允许选择多个选项。在多选模式下,使用 `ComboBoxValue` 展示已选项,并同时给内部的 `ListBox` 传入 `selection-mode="multiple"`。选择通过 `v-model` / `default-value`(数组)以及 `@change`(或 `@update:model-value`)控制;`@selection-change` 在多选下只携带首个 key,数组请用前两者。
|
||||
|
||||
<ComponentPreview name="combo-box-multiple-selection" />
|
||||
|
||||
### 表面样式
|
||||
|
||||
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
|
||||
|
||||
<ComponentPreview name="combo-box-on-surface" />
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="combo-box-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
可使用 `@layer components` 指令自定义 ComboBox 组件类。
|
||||
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-combo-box {
|
||||
@apply flex flex-col gap-1;
|
||||
}
|
||||
|
||||
.hv-combo-box__input-group {
|
||||
@apply relative inline-flex items-center;
|
||||
}
|
||||
|
||||
.hv-combo-box__trigger {
|
||||
@apply absolute end-0 text-muted;
|
||||
}
|
||||
|
||||
.hv-combo-box__popover {
|
||||
@apply rounded-lg border border-border bg-surface p-2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
||||
|
||||
### CSS 类
|
||||
|
||||
ComboBox 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/combo-box.css)):
|
||||
|
||||
#### 基础类
|
||||
|
||||
- `.hv-combo-box` - ComboBox 根容器
|
||||
- `.hv-combo-box__input-group` - 输入框与触发按钮的容器
|
||||
- `.hv-combo-box__value` - 已选值的展示区域(用于多选)
|
||||
- `.hv-combo-box__trigger` - 打开 Popover 的按钮
|
||||
- `.hv-combo-box__popover` - Popover 容器
|
||||
|
||||
#### 状态类
|
||||
|
||||
- `.hv-combo-box[data-invalid="true"]` - 无效状态(隐藏 Description)
|
||||
- `.hv-combo-box[data-disabled="true"]` - 禁用状态
|
||||
- `.hv-combo-box__trigger[data-focus-visible="true"]` - 触发器聚焦
|
||||
- `.hv-combo-box__trigger[data-disabled="true"]` - 触发器禁用
|
||||
- `.hv-combo-box__trigger[data-open="true"]` - 展开状态(图标旋转 180°)
|
||||
- `.hv-combo-box__value[data-placeholder="true"]` - 占位符状态
|
||||
- `.hv-combo-box [data-slot="input"]:focus` - 输入框聚焦(组合框里额外提供非 `focus-visible` 的聚焦态)
|
||||
|
||||
### 交互状态
|
||||
|
||||
- **Hover**:触发器上 `:hover` 或 `[data-hovered="true"]`
|
||||
- **Focus**:触发器上 `:focus-visible` 或 `[data-focus-visible="true"]`
|
||||
- **Disabled**:ComboBox 上 `[data-disabled="true"]`,触发器上 `[data-disabled="true"]`
|
||||
- **Open**:触发器上 `[data-open="true"]`
|
||||
|
||||
## API 参考
|
||||
|
||||
### 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` | 不区分大小写与重音的包含匹配 | 自定义过滤函数;默认谓词按 `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 绑定) |
|
||||
|
||||
事件:
|
||||
|
||||
| 事件 | 载荷 | 描述 |
|
||||
| -------------------- | ----------------------------------------------------- | ----------------------------------- |
|
||||
| `update:modelValue` | `string \| number \| Array<string \| number> \| null` | 值变化(多选时为数组) |
|
||||
| `change` | 同上 | 值变化 |
|
||||
| `update:selectedKey` | `string \| number \| null` | 单选 key 变化 |
|
||||
| `selectionChange` | `string \| number \| null` | 单选 key 变化;多选时只携带首个 key |
|
||||
| `update:inputValue` | `string` | 输入值变化 |
|
||||
| `inputChange` | `string` | 输入值变化 |
|
||||
| `update:open` | `boolean` | 弹层开关变化 |
|
||||
| `openChange` | `boolean` | 弹层开关变化 |
|
||||
|
||||
`aria-label`、`aria-labelledby`、`aria-describedby` 落在真正的 `role="combobox"` 输入框上,而不是根容器。
|
||||
|
||||
### 作用域插槽与渲染状态
|
||||
|
||||
默认插槽提供 `{ isOpen, inputValue, selectedKey, selectedKeys }`。上游的 `Render Props` 表格里还有 `state` 与 `selectedItem`:本库不暴露 React Aria 的 state 对象,选中项数据可从 `records`/`selectedKeys` 派生(示例见 `@update:model-value` 与 `ComboBoxValue` 的作用域插槽),该差异不另设等价 prop。
|
||||
|
||||
### ComboBoxInputGroup
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------- | --------------------------- | ------ | ---------------------------------------------- |
|
||||
| 默认插槽 | `Component` | - | 必须包含本库 `Input`;可再放 `ComboBoxTrigger` |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
### ComboBoxValue
|
||||
|
||||
渲染已选中的值;没有选中项时渲染占位符。多选时以逗号连接各选中项文本。
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | ---------------------------------- | -------------------- | ----------------------------- |
|
||||
| `placeholder` | `string`(上游为 `ReactNode`) | 根上的 `placeholder` | 未选中任何项时的占位文本 |
|
||||
| 默认插槽 | `{ isPlaceholder, selectedItems }` | - | 自定义已选值展示 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
### ComboBoxTrigger
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | --------------------------- | ------- | ------------------------------ |
|
||||
| `is-disabled` | `boolean` | `false` | 是否禁用触发器 |
|
||||
| 默认插槽 | `Component` | - | 自定义触发器内容(默认下箭头) |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
触发器默认 `tabindex="-1"`:整个组合框保留输入框一个 Tab 入口。
|
||||
|
||||
### ComboBoxPopover
|
||||
|
||||
| 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` | - | 弹层内容(通常为 `ListBox`) |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
## 无障碍
|
||||
|
||||
ComboBox 实现 ARIA combobox 模式:
|
||||
|
||||
- 真实焦点始终留在输入框,`aria-activedescendant` 指向已挂载且可见的活动选项
|
||||
- `ArrowDown` / `ArrowUp` / `Home` / `End` 在过滤后的可见集合中移动活动项,`Enter` 确认,`Escape` 关闭,`Tab` 离开
|
||||
- 中文输入法组合期间不确认选择(`isComposing` / `keyCode 229` 时忽略键盘提交)
|
||||
- 过滤不会清除已接受的选择;被过滤掉的已选项仍保留标签
|
||||
- 弹层内不出现第二个 Tab 入口,关闭后移除 `aria-activedescendant`
|
||||
- 组合输入期间保留已经渲染的行,直到 `compositionend` 才提交一次过滤;会话内的 `Enter` 不会确认选择(同时检查会话状态、`isComposing` 与 `keyCode 229`)
|
||||
- 必填只由校验阻止无效提交,不阻止用户把值编辑为空;`native` 与 `aria` 两种校验都按同一份约束判断
|
||||
- 支持禁用状态(`is-disabled` / `disabled-keys`)、输入过滤式搜索、与原生表单集成(`name` / `form` / `form-value`)与自定义值(`allows-custom-value`)
|
||||
- 未实测读屏软件的变更播报与逐项读屏行为,因此不声称已通过该层验收
|
||||
|
||||
## 相关组件
|
||||
|
||||
[ListBox 列表框](./list-box) · [Popover 弹出框](./popover) · [Input 输入框](./input)
|
||||
@@ -0,0 +1,421 @@
|
||||
---
|
||||
title: Dropdown 下拉菜单
|
||||
description: 展示用户可选择的操作或选项列表
|
||||
component: dropdown
|
||||
group: 组件
|
||||
---
|
||||
|
||||
> 基于固定 HeroUI 3.2.6(`ce90ea3`)迁移:React 对象部件改为具名导出,`Selection`/`Set` 改为 `MenuKey[]`,回调改为 Vue 事件;预览、复制与高亮来自同一份 SFC。
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
DropdownRoot,
|
||||
DropdownTrigger,
|
||||
DropdownPopover,
|
||||
DropdownMenu,
|
||||
DropdownItem,
|
||||
Button,
|
||||
Label,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
```
|
||||
|
||||
<ComponentPreview name="dropdown-default" />
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Button,
|
||||
Description,
|
||||
DropdownItem,
|
||||
DropdownItemIndicator,
|
||||
DropdownMenu,
|
||||
DropdownPopover,
|
||||
DropdownRoot,
|
||||
DropdownSection,
|
||||
DropdownSubmenuIndicator,
|
||||
DropdownSubmenuTrigger,
|
||||
DropdownTrigger,
|
||||
Header,
|
||||
Kbd,
|
||||
Label,
|
||||
Separator,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DropdownRoot>
|
||||
<DropdownTrigger>
|
||||
<Button />
|
||||
</DropdownTrigger>
|
||||
<DropdownPopover>
|
||||
<DropdownMenu>
|
||||
<DropdownItem>
|
||||
<Label />
|
||||
<Description />
|
||||
<Kbd slot="keyboard" />
|
||||
<DropdownItemIndicator />
|
||||
</DropdownItem>
|
||||
<Separator />
|
||||
<DropdownSection>
|
||||
<Header />
|
||||
<DropdownItem />
|
||||
</DropdownSection>
|
||||
<DropdownSubmenuTrigger>
|
||||
<DropdownItem>
|
||||
<Label />
|
||||
<DropdownSubmenuIndicator />
|
||||
</DropdownItem>
|
||||
<DropdownPopover>
|
||||
<DropdownMenu>
|
||||
<DropdownItem />
|
||||
</DropdownMenu>
|
||||
</DropdownPopover>
|
||||
</DropdownSubmenuTrigger>
|
||||
</DropdownMenu>
|
||||
</DropdownPopover>
|
||||
</DropdownRoot>
|
||||
</template>
|
||||
```
|
||||
|
||||
## 示例
|
||||
|
||||
### 带图标
|
||||
|
||||
<ComponentPreview name="dropdown-with-icons" />
|
||||
|
||||
### 带描述
|
||||
|
||||
<ComponentPreview name="dropdown-with-descriptions" />
|
||||
|
||||
### 含禁用项
|
||||
|
||||
<ComponentPreview name="dropdown-with-disabled-items" />
|
||||
|
||||
### 分组选项
|
||||
|
||||
<ComponentPreview name="dropdown-with-sections" />
|
||||
|
||||
### 多选
|
||||
|
||||
<ComponentPreview name="dropdown-with-multiple-selection" />
|
||||
|
||||
### 受控组件
|
||||
|
||||
<ComponentPreview name="dropdown-controlled" />
|
||||
|
||||
### 受控展开状态
|
||||
|
||||
<ComponentPreview name="dropdown-controlled-open-state" />
|
||||
|
||||
### 单选
|
||||
|
||||
<ComponentPreview name="dropdown-with-single-selection" />
|
||||
|
||||
### 单选自定义指示器
|
||||
|
||||
<ComponentPreview name="dropdown-single-with-custom-indicator" />
|
||||
|
||||
### 分组级选择
|
||||
|
||||
<ComponentPreview name="dropdown-with-section-level-selection" />
|
||||
|
||||
### 带快捷键
|
||||
|
||||
<ComponentPreview name="dropdown-with-keyboard-shortcuts" />
|
||||
|
||||
### 含子菜单
|
||||
|
||||
<ComponentPreview name="dropdown-with-submenus" />
|
||||
|
||||
### 自定义子菜单指示器
|
||||
|
||||
<ComponentPreview name="dropdown-with-custom-submenu-indicator" />
|
||||
|
||||
### 自定义触发器
|
||||
|
||||
<ComponentPreview name="dropdown-custom-trigger" />
|
||||
|
||||
### 长按触发
|
||||
|
||||
<ComponentPreview name="dropdown-long-press-trigger" />
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="dropdown-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
可使用 `@layer components` 指令自定义 Dropdown 组件类。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-dropdown {
|
||||
@apply flex flex-col gap-1;
|
||||
}
|
||||
|
||||
.hv-dropdown__trigger {
|
||||
@apply outline-none;
|
||||
}
|
||||
|
||||
.hv-dropdown__popover {
|
||||
@apply rounded-3xl border border-border bg-overlay p-2;
|
||||
}
|
||||
|
||||
.hv-dropdown__menu {
|
||||
@apply flex flex-col gap-1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||||
|
||||
### CSS 类
|
||||
|
||||
Dropdown 组件使用以下 CSS 类(类名统一加 `hv-` 前缀):
|
||||
|
||||
#### 基础类
|
||||
|
||||
- `.hv-dropdown` — Dropdown 根容器(Vue 端渲染的包裹元素)
|
||||
- `.hv-dropdown__trigger` — 触发 Dropdown 的按钮或元素
|
||||
- `.hv-dropdown__popover` — Popover 容器
|
||||
- `.hv-dropdown__menu` — Popover 内的菜单容器
|
||||
|
||||
#### 状态类
|
||||
|
||||
- `.hv-dropdown__trigger[data-focus-visible="true"]` — 触发器聚焦状态
|
||||
- `.hv-dropdown__trigger[data-disabled="true"]` — 触发器禁用状态
|
||||
- `.hv-dropdown__trigger[data-pressed="true"]` — 触发器按下状态
|
||||
- `.hv-dropdown__popover[data-entering="true"]` — 进入动画状态
|
||||
- `.hv-dropdown__popover[data-exiting="true"]` — 退出动画状态
|
||||
- `.hv-dropdown__popover[data-side="top|bottom|left|right"]` — 定位方向(进入动画的位移方向)
|
||||
- `.hv-dropdown__menu[data-selection-mode="single"]` — 单选模式
|
||||
- `.hv-dropdown__menu[data-selection-mode="multiple"]` — 多选模式
|
||||
|
||||
### 菜单组件类
|
||||
|
||||
Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类名也可用于自定义:
|
||||
|
||||
#### 菜单类
|
||||
|
||||
- `.hv-menu` — 菜单容器
|
||||
- `[data-slot="separator"]` — 菜单内的分隔线元素
|
||||
|
||||
#### 菜单项类
|
||||
|
||||
- `.hv-menu-item` — 菜单项容器
|
||||
- `.hv-menu-item__indicator` — 选中指示器(对勾或圆点)
|
||||
- `[data-slot="menu-item-indicator--checkmark"]` — 对勾指示器 SVG
|
||||
- `[data-slot="menu-item-indicator--dot"]` — 圆点指示器 SVG
|
||||
- `.hv-menu-item__indicator--submenu` — 子菜单指示器(箭头)
|
||||
- `.hv-menu-item--default` — 默认样式变体
|
||||
- `.hv-menu-item--danger` — 危险样式变体
|
||||
|
||||
#### 菜单项状态类
|
||||
|
||||
- `.hv-menu-item[data-focus-visible="true"]` — 聚焦状态(键盘焦点)
|
||||
- `.hv-menu-item[data-focused]` — 聚焦状态
|
||||
- `.hv-menu-item[data-pressed]` — 按下状态
|
||||
- `.hv-menu-item[data-hovered]` — 悬停状态
|
||||
- `.hv-menu-item[data-selected="true"]` — 选中状态
|
||||
- `.hv-menu-item[data-disabled]` — 禁用状态(存在即禁用)
|
||||
- `.hv-menu-item[data-has-submenu="true"]` — 带子菜单的项
|
||||
- `.hv-menu-item[data-selection-mode="single"]` — 单选模式
|
||||
- `.hv-menu-item[data-selection-mode="multiple"]` — 多选模式
|
||||
- `.hv-menu-item[aria-checked="true"]` — 已勾选(ARIA)
|
||||
|
||||
#### 菜单分区类
|
||||
|
||||
- `.hv-menu-section` — 菜单分区容器
|
||||
|
||||
### 交互状态
|
||||
|
||||
组件同时支持 CSS 伪类与 data 属性:
|
||||
|
||||
- **Hover**:触发器与菜单项上 `:hover` 或 `[data-hovered]`
|
||||
- **Focus**:触发器与菜单项上 `:focus-visible` 或 `[data-focus-visible="true"]`;菜单项另有 `[data-focused]`
|
||||
- **Disabled**:触发器上 `:disabled`/`[aria-disabled="true"]`,菜单项上 `[data-disabled]`
|
||||
- **Pressed**:菜单项与触发器上 `:active` 或 `[data-pressed]`
|
||||
- **Selected**:菜单项上 `[data-selected="true"]` 或 `[aria-checked="true"]`
|
||||
- **Open/Close**:`DropdownPopover` 上 `[data-state="open"]`;进入/退出期间另有 `[data-entering="true"]` / `[data-exiting="true"]`
|
||||
|
||||
## API 参考
|
||||
|
||||
所有部件接受 `class`、`style`、ARIA 与原生事件(`DropdownSubmenuTrigger` 会把它们合并到它的条目元素上)。选择集合使用 Vue 数组模型(`MenuKey[]`),不使用 React `Set`/`Selection`。标记「Vue 扩展」的属性在官方 HeroUI 中没有对应项。
|
||||
|
||||
弹层默认通过 Portal 挂载到 `document.body`(可用 `to` 指定目标),层级为 `--z-index-overlay`,因此不会被祖先的 `overflow`、`contain` 或 `isolation` 裁切;面板高度受底层可用的浮动高度约束并自动滚动。
|
||||
|
||||
### DropdownRoot
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------------- | ------------------------ | --------- | --------------------------------------------- |
|
||||
| `open` | `boolean` | — | 设置菜单展开状态(受控)。官方名 `isOpen` |
|
||||
| `defaultOpen` | `boolean` | — | 设置菜单默认展开状态(非受控)。 |
|
||||
| `trigger` | `'press' \| 'longPress'` | `'press'` | 触发菜单的交互类型。 |
|
||||
| `update:open` | 事件 `(open: boolean)` | — | 展开状态变化时触发。官方名 `onOpenChange` |
|
||||
| `modal` | `boolean` | `true` | **Vue 扩展**:模态焦点管理 |
|
||||
| `dir` | `'ltr' \| 'rtl'` | — | **Vue 扩展**:文本方向 |
|
||||
| `longPressDuration` | `number` | `500` | **Vue 扩展**:长按时长 ms;移动超过 10px 取消 |
|
||||
|
||||
长按模式下 Enter/Space 仍可键盘打开,并且真实触发器上的 `keydown` 处理器先执行,可 `preventDefault()` 取消打开;`pointerup`/`pointercancel`/`pointerleave`/卸载会清理计时。
|
||||
|
||||
### DropdownTrigger
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| --------- | --------- | ------ | ---------------------------------- |
|
||||
| `asChild` | `boolean` | `true` | 将触发合并到唯一子节点(默认行为) |
|
||||
| `as` | `string` | — | 自定义触发元素标签 |
|
||||
|
||||
默认插槽会收到 `{ isOpen }`。页面里的 `Button` props 写在内部的 `Button` 上。子节点若不是原生可聚焦元素(例如 Avatar),组件会补 `role="button"` 与 `tabindex="0"`,保持官方「触发器可键盘操作」的行为。
|
||||
|
||||
### DropdownPopover
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------------------------ | ----------------------- | ---------- | ---------------------------------------------------- |
|
||||
| `placement` | `OverlayPlacement` | `'bottom'` | 相对于触发器的位置(复用本库 Popover 的取值)。 |
|
||||
| `offset` | `number` | `0` | 与触发器的距离。 |
|
||||
| `crossOffset` | `number` | `0` | 交叉轴偏移。 |
|
||||
| `shouldFlip` | `boolean` | `true` | 空间不足时翻转。 |
|
||||
| `containerPadding` | `number` | `12` | 与视口边缘保留的距离。 |
|
||||
| `isKeyboardDismissDisabled` | `boolean` | `false` | 禁用 Escape 关闭。 |
|
||||
| `shouldCloseOnInteractOutside` | `(event) => boolean` | — | 返回 `false` 时外部交互不关闭菜单。 |
|
||||
| `to` | `string \| HTMLElement` | — | Portal 目标,默认 `document.body`。 |
|
||||
| `side` / `align` | `string` | — | **底层扩展**:直接指定浮动方向,优先于 `placement`。 |
|
||||
| `sideOffset` / `alignOffset` | `number` | — | **底层扩展**:覆盖 `offset` / `crossOffset`。 |
|
||||
| `avoidCollisions` | `boolean` | — | **底层扩展**:覆盖 `shouldFlip`。 |
|
||||
| `collisionPadding` | `number` | — | **底层扩展**:覆盖 `containerPadding`。 |
|
||||
| `loop` | `boolean` | — | **底层扩展**:焦点循环。 |
|
||||
|
||||
默认插槽会收到 `{ isEntering, isExiting, placement }`。`onKeydown`/`onEscapeKeyDown`/`onInteractOutside`/`onOpenAutoFocus`/`onAnimationend` 与调用方处理器按 Vue 数组语义合并,调用方先执行,`preventDefault()` 后内部逻辑不再动作。
|
||||
|
||||
### DropdownMenu
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ----------------------------------------- | ---------------------------------- | -------- | ---------------------------------------------------- |
|
||||
| `selectionMode` | `'none' \| 'single' \| 'multiple'` | `'none'` | 是否启用单选、多选或不启用选择。 |
|
||||
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
|
||||
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
|
||||
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key。 |
|
||||
| `disallowEmptySelection` | `boolean` | `false` | 禁止用户清空已有选择。 |
|
||||
| `shouldCloseOnSelect` | `boolean \| (key) => boolean` | — | 菜单级关闭策略;默认动作项与单选关闭、多选保持打开。 |
|
||||
| `shouldFocusWrap` | `boolean` | — | 键盘导航在首尾循环。 |
|
||||
| `autoFocus` | `boolean` | `true` | 打开时聚焦菜单。 |
|
||||
| `escapeKeyBehavior` | `'clearSelection' \| 'none'` | `'none'` | `clearSelection` 时 Escape 清空选择且不关闭菜单。 |
|
||||
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
|
||||
| `action` | 事件 `(key: MenuKey)` | — | 激活菜单项时触发。官方名 `onAction` |
|
||||
| `close` | 事件 `()` | — | 该层菜单关闭时触发。官方名 `onClose` |
|
||||
|
||||
默认插槽渲染条目;`empty` 插槽在该菜单没有条目时渲染(对应官方 `renderEmptyState`)。数据驱动的 `items` 用 `v-for` 表达。
|
||||
|
||||
### DropdownSection
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ----------------------------------------- | ------------------------ | ------- | -------------------------------------------- |
|
||||
| `selectionMode` | `'single' \| 'multiple'` | — | 该分组内菜单项的选择模式;省略则继承菜单。 |
|
||||
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
|
||||
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
|
||||
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key,与菜单级 `disabledKeys` 叠加。 |
|
||||
| `disallowEmptySelection` | `boolean` | `false` | 禁止用户清空该分组的选择。 |
|
||||
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
|
||||
|
||||
分组内嵌套的 `Header` 会自动成为分组的可访问名称来源(`aria-labelledby`)。分组内再嵌套的 `Menu` 会重置选择作用域,不会继承外层分组的 owner。
|
||||
|
||||
### DropdownItem
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------------------------------------------------------- | ----------------------- | ----------- | ------------------------------------------------------------------------- |
|
||||
| `id` | `MenuKey` | — | 业务 key(选择与动作使用)。DOM id 为实例级,与业务 key 无关。 |
|
||||
| `textValue` | `string` | — | 用于首字母导航的文本内容。 |
|
||||
| `variant` | `'default' \| 'danger'` | `'default'` | 菜单项视觉变体。 |
|
||||
| `isDisabled` | `boolean` | `false` | 禁用该项。 |
|
||||
| `closeOnSelect` | `boolean` | — | 覆盖关闭策略;`false` 保持打开,`true` 关闭。官方名 `shouldCloseOnSelect` |
|
||||
| `href` | `string` | — | 渲染为链接型菜单项(真实 `<a>`)。 |
|
||||
| `target` / `rel` / `download` / `hrefLang` / `referrerPolicy` | — | — | 透传到链接元素。 |
|
||||
| `select` | 事件 `(key: MenuKey)` | — | **Vue 扩展**:条目激活回调 |
|
||||
|
||||
默认插槽会收到 `{ isSelected, isFocused, isDisabled, isPressed }`,对应官方的 Render Props。禁用链接不会导航;`href` 项的原生导航不受 `click` 处理器影响(除非调用 `preventDefault()`)。
|
||||
|
||||
### DropdownItemIndicator
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------ | ---------------------- | ------------- | ---------- |
|
||||
| `type` | `'checkmark' \| 'dot'` | `'checkmark'` | 指示器类型 |
|
||||
|
||||
默认插槽会收到 `{ isSelected, isIndeterminate, isFocused, isDisabled, isPressed }`(菜单项没有不确定态,`isIndeterminate` 恒为 `false`):
|
||||
|
||||
```vue
|
||||
<DropdownItemIndicator v-slot="{ isSelected }">
|
||||
<MyCheckIcon v-if="isSelected" />
|
||||
</DropdownItemIndicator>
|
||||
```
|
||||
|
||||
### DropdownSubmenuIndicator
|
||||
|
||||
| 内容 | 说明 |
|
||||
| ---- | ------------------------------------ |
|
||||
| 默认 | 官方内置的 chevron-right 图标 |
|
||||
| 插槽 | 自定义指示器内容;带子菜单的项才渲染 |
|
||||
|
||||
### DropdownSubmenuTrigger
|
||||
|
||||
| Slots / Props | 说明 |
|
||||
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| 默认插槽 | 第一个子节点是触发条目,其余子节点渲染进子菜单内容 |
|
||||
| `class`/ARIA | 合并到触发条目元素上 |
|
||||
| 面板参数 | 嵌套 `DropdownPopover` 的 `offset`/`crossOffset`/`shouldFlip`/`containerPadding`/`loop`/`to`、`class`/`style`/`id`/`data-*`/ARIA 与事件都会传给子菜单面板;`isKeyboardDismissDisabled` 为真时 Esc 既不关子层也不关父层,`shouldCloseOnInteractOutside` 返回 `false` 时外部交互不关闭 |
|
||||
|
||||
每层子菜单独立持有 open 状态并各自 Portal;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。子菜单的 `side`/`align` 由底层固定为「贴着父项向右展开」(RTL 反向),只有偏移与碰撞参数可调。
|
||||
|
||||
### Render Props
|
||||
|
||||
在 `DropdownItem` 中使用作用域插槽时,会收到以下值:
|
||||
|
||||
| Prop | 类型 | 描述 |
|
||||
| ------------ | --------- | ---------------------- |
|
||||
| `isSelected` | `boolean` | 该项是否选中。 |
|
||||
| `isFocused` | `boolean` | 该项是否聚焦。 |
|
||||
| `isDisabled` | `boolean` | 该项是否禁用。 |
|
||||
| `isPressed` | `boolean` | 该项是否处于按下状态。 |
|
||||
|
||||
### 独立 Menu
|
||||
|
||||
`Menu` 可以在 `DropdownPopover` 之外直接使用:渲染为静态内联列表(`data-slot="menu"`),保留 `role="menu"`、条目语义、键盘导航与选择;不会锁定页面滚动、不会拦截外部指针事件、不会抢焦点,也不做浮动定位。
|
||||
|
||||
## 无障碍
|
||||
|
||||
Dropdown 组件实现 ARIA 菜单模式,并提供:
|
||||
|
||||
- 完整键盘导航(方向键、Home/End、首字母导航,`shouldFocusWrap` 控制首尾循环)
|
||||
- 屏幕阅读器对操作与选中变化的播报:`aria-label`/`aria-labelledby`/`aria-describedby` 落在真正的 `role="menu"` 节点上
|
||||
- 合理的焦点管理(关闭后焦点回到触发器,Esc 逐层关闭)
|
||||
- 禁用态支持(`[data-disabled]` + `aria-disabled`)
|
||||
- 长按交互支持
|
||||
- 子菜单导航
|
||||
|
||||
菜单使用 `role="menu"` / `menuitem`(选择项为 `menuitemradio` / `menuitemcheckbox`),每个弹层只有一个 `role="menu"`;禁用项跳过,不进入 Tab 顺序。
|
||||
|
||||
## 上游差异
|
||||
|
||||
- 部件导出为具名组件(`DropdownRoot` / `DropdownTrigger` / …),触发器需要显式包裹,官方是把 `Button` 直接放在 `Dropdown` 下。
|
||||
- `Selection`/`Set` 改为 `MenuKey[]`;`onAction`/`onSelectionChange`/`onClose` 改为 Vue 事件;`isOpen`/`onOpenChange` 改为 `open` + `v-model:open`。
|
||||
- 官方 `items` + 渲染函数的数据驱动集合用 `v-for` 表达;`renderEmptyState` 对应 `empty` 插槽。不提供 React Collection 对象模型。
|
||||
- 子菜单悬停展开延迟由底层固定为 100ms,未暴露官方 `delay`(默认 200ms);子菜单面板的 `side`/`align` 由底层固定,只有偏移与碰撞参数可调。
|
||||
- `DropdownTrigger` 的作用域插槽只提供 `{ isOpen }`,`DropdownPopover` 只提供 `{ isEntering, isExiting, placement }`;官方 Button/Popover 的完整 render props 未全部覆盖。
|
||||
- `DropdownRoot` 额外渲染一层 `.hv-dropdown` 包裹元素(官方该类没有 DOM 宿主),用于承载根级 class 与方向。
|
||||
- 条目 `id` 是业务 key,DOM id 为实例级;官方把 key 直接作为 DOM id(Vue 端避免数字 `0` 与字符串 `"0"` 冲突)。
|
||||
- 条目 `onClick` 中 `preventDefault()` 会取消动作、选择与关闭;`stopImmediatePropagation()` 会终止后续处理器,均为原生事件语义。
|
||||
|
||||
## 相关组件
|
||||
|
||||
- [Button 按钮](./button)
|
||||
- [Popover 弹出层](./popover)
|
||||
- [Separator 分隔线](./separator)
|
||||
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: ErrorMessage 错误消息
|
||||
description: 用于展示错误的底层错误消息组件
|
||||
component: error-message
|
||||
group: 组件
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import { ErrorMessage } from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ErrorMessage>请至少选择一个分类</ErrorMessage>
|
||||
</template>
|
||||
```
|
||||
|
||||
<ComponentPreview name="error-message-basic" />
|
||||
|
||||
`ErrorMessage` 是基于固定基线 `Text` 组件 `errorMessage` slot 的底层组件,用于在 **非表单组件**(如 `TagGroup`、`Calendar` 及其他集合类组件)中展示错误消息。
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Description,
|
||||
ErrorMessage,
|
||||
Label,
|
||||
Tag,
|
||||
TagGroup,
|
||||
TagGroupList,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<TagGroup>
|
||||
<Label>分类</Label>
|
||||
<TagGroupList>
|
||||
<Tag id="news">资讯</Tag>
|
||||
</TagGroupList>
|
||||
<Description>说明</Description>
|
||||
<ErrorMessage>请至少选择一个分类</ErrorMessage>
|
||||
</TagGroup>
|
||||
</template>
|
||||
```
|
||||
|
||||
## 何时使用
|
||||
|
||||
`ErrorMessage` **不绑定表单**,是用于非表单上下文的通用错误展示组件。
|
||||
|
||||
- **推荐用于** 非表单组件(如 `TagGroup`、`Calendar`、集合类组件)
|
||||
- **表单字段** 建议使用 [FieldError](./field-error),它提供表单专用校验特性与自动错误处理,遵循标准化表单校验模式
|
||||
|
||||
## ErrorMessage 与 FieldError
|
||||
|
||||
| 组件 | 使用场景 | 表单集成 | 示例组件 |
|
||||
| -------------- | ---------- | -------- | ------------------------------------ |
|
||||
| `ErrorMessage` | 非表单组件 | 否 | `TagGroup`、`Calendar` |
|
||||
| `FieldError` | 表单字段 | 是 | `TextField`、`NumberField`、`Select` |
|
||||
|
||||
表单校验建议使用 `FieldError`,它遵循标准化表单校验模式并提供表单专用特性。
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="error-message-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
可使用 `@layer components` 指令自定义 ErrorMessage 组件类。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-error-message {
|
||||
@apply text-sm font-medium text-danger;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
||||
|
||||
### CSS 类
|
||||
|
||||
ErrorMessage 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/error-message.css)):
|
||||
|
||||
#### 基础类
|
||||
|
||||
- `.hv-error-message` - 基础错误消息样式,danger 色与文本换行
|
||||
|
||||
#### 插槽类
|
||||
|
||||
- `[slot="errorMessage"]` - 与固定基线一致的 DOM 标记,可用作 CSS 选择器
|
||||
- `[data-slot="error-message"]` - 组件标记
|
||||
|
||||
## API 参考
|
||||
|
||||
### ErrorMessage
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------- | --------------------------- | ------ | ----------------------------- |
|
||||
| `id` | `string` | 自动 | 元素 id;位于字段内时参与描述 |
|
||||
| 默认插槽 | `Component` | - | 错误消息内容 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
## 说明
|
||||
|
||||
`ErrorMessage` 渲染为 `<p>`,带 `slot="errorMessage"` 与 `data-slot="error-message"` 标记;在 Vue 中 `slot` 是普通 DOM 属性(CSS 钩子),不是插槽分配——直接把 `ErrorMessage` 放进父组件的默认插槽即可。显示与隐藏由调用方根据自身状态控制,组件本身不做校验。
|
||||
|
||||
## 无障碍
|
||||
|
||||
- 使用语义化 HTML 展示错误文本
|
||||
- 放在字段或标签组内时,其 `id` 会进入所属容器的 `aria-describedby`
|
||||
- 错误状态使用主题的 danger 色,保持足够对比度
|
||||
- 上游经 `Text` 的 `errorMessage` slot 提供节点;本库保留同样的 DOM 标记,但不改写 `TagGroup` 的 `aria-invalid`
|
||||
|
||||
## 相关组件
|
||||
|
||||
[FieldError 字段错误](./field-error) · [Description 描述](./description) · [TagGroup 标签组](./tag-group)
|
||||
@@ -0,0 +1,285 @@
|
||||
---
|
||||
title: TagGroup 标签组
|
||||
description: 可聚焦的标签列表,支持键盘导航、选择与移除。
|
||||
component: tag-group
|
||||
group: 组件
|
||||
---
|
||||
|
||||
## 用法
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import { Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<TagGroup aria-label="标签" selection-mode="single">
|
||||
<TagGroupList>
|
||||
<Tag id="news">资讯</Tag>
|
||||
<Tag id="travel">旅行</Tag>
|
||||
</TagGroupList>
|
||||
</TagGroup>
|
||||
</template>
|
||||
```
|
||||
|
||||
<ComponentPreview name="tag-group-basic" />
|
||||
|
||||
`TagGroup` 是标签集合的根部件,负责选择状态、禁用范围、移除请求与名称/描述关联;`TagGroupList` 渲染真正的 `role="grid"` 容器,`Tag` 渲染 `role="row"` 条目。上游的对象式子部件 `TagGroup.List`、`Tag.RemoveButton` 在本库中是具名导出 `TagGroupList`、`TagRemoveButton`。
|
||||
|
||||
## 组件结构
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Description,
|
||||
ErrorMessage,
|
||||
Label,
|
||||
Tag,
|
||||
TagGroup,
|
||||
TagGroupList,
|
||||
TagRemoveButton,
|
||||
} from "@heroui-vue/components";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<TagGroup>
|
||||
<Label>分类</Label>
|
||||
<TagGroupList>
|
||||
<Tag id="news">
|
||||
资讯
|
||||
<TagRemoveButton />
|
||||
</Tag>
|
||||
</TagGroupList>
|
||||
<Description>说明</Description>
|
||||
<ErrorMessage>错误</ErrorMessage>
|
||||
</TagGroup>
|
||||
</template>
|
||||
```
|
||||
|
||||
`Label`、`Description`、`ErrorMessage` 直接放在 `TagGroup` 的默认插槽中:它们的 `id` 会作为列表节点的 `aria-labelledby` / `aria-describedby`,不要再写 React 的 `slot="..."` 形式。
|
||||
|
||||
## 示例
|
||||
|
||||
### 尺寸
|
||||
|
||||
<ComponentPreview name="tag-group-sizes" />
|
||||
|
||||
### 变体
|
||||
|
||||
<ComponentPreview name="tag-group-variants" />
|
||||
|
||||
### 禁用
|
||||
|
||||
<ComponentPreview name="tag-group-disabled" />
|
||||
|
||||
### 选择模式
|
||||
|
||||
<ComponentPreview name="tag-group-selection-modes" />
|
||||
|
||||
### 受控组件
|
||||
|
||||
<ComponentPreview name="tag-group-controlled" />
|
||||
|
||||
### 带错误信息
|
||||
|
||||
<ComponentPreview name="tag-group-with-error-message" />
|
||||
|
||||
### 带列表数据
|
||||
|
||||
<ComponentPreview name="tag-group-with-list-data" />
|
||||
|
||||
### 带前缀
|
||||
|
||||
<ComponentPreview name="tag-group-with-prefix" />
|
||||
|
||||
### 带移除按钮
|
||||
|
||||
<ComponentPreview name="tag-group-with-remove-button" />
|
||||
|
||||
### 渲染函数
|
||||
|
||||
<ComponentPreview name="tag-group-render-function" />
|
||||
|
||||
上游用 React `render` 函数替换根 DOM 元素;Vue 中对应 `as`(自定义根元素标签)与 `as-child`(使用自定义组件作为唯一根)。本库示例用 `as="div"` 演示同一能力。
|
||||
|
||||
## 自定义样式
|
||||
|
||||
### Tailwind CSS
|
||||
|
||||
<ComponentPreview name="tag-group-custom-styles" />
|
||||
|
||||
### 全局 CSS
|
||||
|
||||
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-tag-group {
|
||||
@apply flex flex-col gap-2;
|
||||
}
|
||||
|
||||
.hv-tag-group__list {
|
||||
@apply flex flex-wrap gap-2;
|
||||
}
|
||||
|
||||
.hv-tag {
|
||||
@apply rounded-full px-3 py-1;
|
||||
}
|
||||
|
||||
.hv-tag__remove-button {
|
||||
@apply ml-1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 样式参考
|
||||
|
||||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
||||
|
||||
### CSS 类
|
||||
|
||||
TagGroup 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/tag-group.css) 与 [tag.css](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/tag.css)):
|
||||
|
||||
#### 基础类
|
||||
|
||||
- `.hv-tag-group` - TagGroup 根容器
|
||||
- `.hv-tag-group__list` - 标签列表容器
|
||||
- `.hv-tag` - 标签基础样式
|
||||
- `.hv-tag__remove-button` - 移除按钮触发器
|
||||
|
||||
#### 插槽类
|
||||
|
||||
- `.hv-tag-group [data-slot="description"]` - Description 插槽样式
|
||||
- `.hv-tag-group [data-slot="error-message"]` - ErrorMessage 插槽样式
|
||||
|
||||
#### 尺寸类
|
||||
|
||||
- `.hv-tag--sm` - 小尺寸标签
|
||||
- `.hv-tag--md` - 中尺寸标签(默认)
|
||||
- `.hv-tag--lg` - 大尺寸标签
|
||||
|
||||
#### 变体类
|
||||
|
||||
- `.hv-tag--default` - 默认变体
|
||||
- `.hv-tag--surface` - 带 Surface 背景的变体
|
||||
|
||||
#### 状态类
|
||||
|
||||
- `.hv-tag[data-selected="true"]` - 选中状态
|
||||
- `.hv-tag[data-disabled="true"]` - 禁用状态
|
||||
- `.hv-tag[data-hovered="true"]` - 悬停状态
|
||||
- `.hv-tag[data-pressed="true"]` - 按下状态
|
||||
- `.hv-tag[data-focus-visible="true"]` - 聚焦状态(键盘焦点)
|
||||
|
||||
### 交互状态
|
||||
|
||||
该组件同时支持 CSS 伪类与 data 属性:
|
||||
|
||||
- **悬停**:标签上 `:hover` 或 `[data-hovered="true"]`
|
||||
- **聚焦**:标签上 `:focus-visible` 或 `[data-focus-visible="true"]`
|
||||
- **按下**:标签上 `:active` 或 `[data-pressed="true"]`
|
||||
- **已选中**:标签上 `[data-selected="true"]` 或 `[aria-selected="true"]`
|
||||
- **禁用**:标签上 `[aria-disabled="true"]` 或 `[data-disabled="true"]`
|
||||
|
||||
## API 参考
|
||||
|
||||
### TagGroup
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| -------------------------- | ---------------------------------- | ------------------- | -------------------------------------------------------- |
|
||||
| `selection-mode` | `'none' \| 'single' \| 'multiple'` | `'none'` | 允许的选择类型 |
|
||||
| `model-value` | `Array<string \| number>` | - | 当前选中的 key(受控,`v-model`) |
|
||||
| `selected-keys` | `Array<string \| number>` | - | 当前选中的 key(`model-value` 的别名,受控) |
|
||||
| `default-value` | `Array<string \| number>` | - | 初始选中的 key(非受控) |
|
||||
| `default-selected-keys` | `Array<string \| number>` | - | 初始选中的 key(非受控,`default-value` 的别名) |
|
||||
| `disabled-keys` | `Array<string \| number>` | `[]` | 禁用标签的 key |
|
||||
| `is-disabled` | `boolean` | `false` | 是否禁用整个 TagGroup |
|
||||
| `disallow-empty-selection` | `boolean` | `false` | 是否阻止用户清空已有选择 |
|
||||
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 组内标签尺寸 |
|
||||
| `variant` | `'default' \| 'surface'` | `'default'` | 标签视觉变体 |
|
||||
| `dir` | `'ltr' \| 'rtl'` | 继承 ConfigProvider | 水平方向键的导航方向 |
|
||||
| `as` | `string` | `'div'` | 自定义根元素标签 |
|
||||
| `as-child` | `boolean` | `false` | 使用唯一子元素作为根节点并合并属性 |
|
||||
| 默认插槽 | `Component` | - | 组内容(Label、TagGroupList、Description、ErrorMessage) |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
事件:
|
||||
|
||||
| 事件 | 载荷 | 描述 |
|
||||
| ------------------- | ------------------------- | ---------------------------------------------------- |
|
||||
| `update:modelValue` | `Array<string \| number>` | 选择变化时发出(配合 `v-model`) |
|
||||
| `selectionChange` | `Array<string \| number>` | 选择变化时发出 |
|
||||
| `remove` | `Array<string \| number>` | 用户请求移除标签时发出;不包含在数据中删除条目的行为 |
|
||||
|
||||
`aria-label`、`aria-labelledby`、`aria-describedby` 会落在 `role="grid"` 的列表节点,而不是外层容器。
|
||||
|
||||
### TagGroupList
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------ | --------------------------------- | ------- | ------------------------------------------------------------- |
|
||||
| `items` | `Array<{ id: string \| number }>` | - | 数据驱动渲染;每项需要 `id`,作用域插槽接收 `item` 与 `index` |
|
||||
| `as` | `string` | `'div'` | 自定义列表元素标签 |
|
||||
| `as-child` | `boolean` | `false` | 使用唯一子元素作为列表节点并合并属性 |
|
||||
| 默认插槽 | `{ item, index }` | - | 标签列表内容;提供 `items` 时按数据渲染 |
|
||||
| `empty` 插槽 | - | - | `items` 为空时的内容 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
### Tag
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | --------------------------- | ------- | ------------------------------------------------------------ |
|
||||
| `id` | `string \| number` | - | 标签唯一标识(必填,集合 key) |
|
||||
| `text-value` | `string` | - | 标签内容的字符串表示,用于无障碍名称与移除按钮名称 |
|
||||
| `is-disabled` | `boolean` | `false` | 是否禁用该标签 |
|
||||
| `as` | `string` | `'div'` | 自定义行元素标签 |
|
||||
| `as-child` | `boolean` | `false` | 使用唯一子元素作为行节点并合并属性;组件需透传属性与默认插槽 |
|
||||
| 默认插槽 | `{ ...TagRenderState }` | - | 标签内容;作用域提供渲染状态 |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
**提示:** `size`、`variant` 由父级 `TagGroup` 继承,不能在单个 `Tag` 上直接设置。
|
||||
|
||||
### TagRemoveButton
|
||||
|
||||
| Prop | 类型 | 默认值 | 描述 |
|
||||
| ------------- | --------------------------- | ------- | ------------------------------------------ |
|
||||
| `text-value` | `string` | - | 供无障碍名称使用的标签文本;未传时取父 Tag |
|
||||
| `is-disabled` | `boolean` | `false` | 是否禁用移除按钮 |
|
||||
| 默认插槽 | `Component` | - | 自定义移除按钮内容(默认为关闭图标) |
|
||||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
|
||||
|
||||
**提示:** 为 `TagGroup` 监听 `remove` 时:
|
||||
|
||||
- **自动渲染**:若 `Tag` 的子节点中未包含 `TagRemoveButton`,会自动渲染默认移除按钮。
|
||||
- **自定义按钮**:若在 `Tag` 下提供了 `TagRemoveButton`,将替换自动渲染的按钮。
|
||||
- **自定义图标**:可向 `TagRemoveButton` 传入自定义插槽内容改变外观。
|
||||
|
||||
默认按钮的可访问名称为 `Remove tag {标签文本}`;移除按钮的点击不会触发选择变化。
|
||||
|
||||
### 作用域插槽状态
|
||||
|
||||
`Tag` 的默认插槽提供以下渲染状态:
|
||||
|
||||
| 字段 | 类型 | 描述 |
|
||||
| ---------------- | --------- | ---------------------- |
|
||||
| `isSelected` | `boolean` | 标签是否选中 |
|
||||
| `isDisabled` | `boolean` | 标签是否禁用 |
|
||||
| `isHovered` | `boolean` | 标签是否悬停 |
|
||||
| `isPressed` | `boolean` | 标签是否按下 |
|
||||
| `isFocused` | `boolean` | 标签是否聚焦 |
|
||||
| `isFocusVisible` | `boolean` | 标签是否为可见键盘焦点 |
|
||||
| `allowsRemoving` | `boolean` | 组是否允许移除 |
|
||||
|
||||
上游 `children` 渲染函数对应 Vue 的作用域插槽。
|
||||
|
||||
## 无障碍
|
||||
|
||||
- 列表节点为 `role="grid"`,每个标签为 `role="row"`,行包含 `gridcell`,可访问名称来自其文本内容。
|
||||
- 方向键在标签之间移动焦点(RTL 下左右互换),`Home` / `End` 跳到第一个 / 最后一个标签;禁用标签会被跳过。
|
||||
- `Enter` 与 `Space` 切换选中(`Space` 在 `keyup` 时激活,调用方可通过 `preventDefault` 取消)。
|
||||
- `Backspace` / `Delete` 在允许移除时请求移除当前聚焦标签。
|
||||
- 移除确认后焦点移到原位置的下一个标签,删除最后一项时回到列表本身;父级拒绝删除时标签与焦点都保留。
|
||||
- 列表保留 `aria-atomic="false"` 与 `aria-relevant="additions text"` 标记;独立的 polite 状态区域在确认移除后更新公告,父级拒绝移除时不公告。实际读屏效果尚未验证。
|
||||
|
||||
## 相关组件
|
||||
|
||||
[Chip 标签](./chip) · [Description 描述](./description) · [ErrorMessage 错误消息](./error-message) · [Label 标签](./label)
|
||||
Reference in New Issue
Block a user