feat: migrate Tag, TagGroup and ErrorMessage with collection and focus contracts

Port the fixed HeroUI baseline tag/tag-group/error-message components onto the
existing collection selection/registry protocols instead of a second state model.

- TagGroup owns selection, disabled scope, removal requests and name/description
  wiring; TagGroupList renders the role=grid node and Tag renders role=row.
- Removal has one path: a press inside the remove subtree is a removal request,
  so custom remove buttons work without a second event source; focus follows the
  next neighbour only after the item is actually gone.
- ErrorMessage keeps the baseline slot marker and reuses the field part ids
  without gaining validation or aria-invalid semantics.
- Ship the 12 tag-group and 2 error-message previews, both doc pages, navigation,
  a 35-case behavioural suite and independent tarball consumption checks.
This commit is contained in:
maoyingjie
2026-10-25 10:45:43 +08:00
parent 3532cc6aad
commit af6bfda3ee
40 changed files with 3790 additions and 4 deletions
+124
View File
@@ -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)):
#### 基础类 [!toc]
- `.hv-error-message` - 基础错误消息样式,danger 色与文本换行
#### 插槽类 [!toc]
- `[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)
+285
View File
@@ -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="section"` 演示同一能力。
## 自定义样式
### 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)):
#### 基础类 [!toc]
- `.hv-tag-group` - TagGroup 根容器
- `.hv-tag-group__list` - 标签列表容器
- `.hv-tag` - 标签基础样式
- `.hv-tag__remove-button` - 移除按钮触发器
#### 插槽类 [!toc]
- `.hv-tag-group [data-slot="description"]` - Description 插槽样式
- `.hv-tag-group [data-slot="error-message"]` - ErrorMessage 插槽样式
#### 尺寸类 [!toc]
- `.hv-tag--sm` - 小尺寸标签
- `.hv-tag--md` - 中尺寸标签(默认)
- `.hv-tag--lg` - 大尺寸标签
#### 变体类 [!toc]
- `.hv-tag--default` - 默认变体
- `.hv-tag--surface` - 带 Surface 背景的变体
#### 状态类 [!toc]
- `.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"`,行的可访问名称来自其文本内容。
- 方向键在标签之间移动焦点(RTL 下左右互换),`Home` / `End` 跳到第一个 / 最后一个标签;禁用标签会被跳过。
- `Enter` 与 `Space` 切换选中(`Space` 在 `keyup` 时激活,调用方可通过 `preventDefault` 取消)。
- `Backspace` / `Delete` 在允许移除时请求移除当前聚焦标签。
- 移除确认后焦点移到原位置的下一个标签,删除最后一项时回到列表本身;父级拒绝删除时标签与焦点都保留。
- 列表节点声明 `aria-atomic="false"` 与 `aria-relevant="additions text"`,集合内容的增删会被读屏软件播报。
## 相关组件
[Chip 标签](./chip) · [Description 描述](./description) · [ErrorMessage 错误消息](./error-message) · [Label 标签](./label)