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
+10
View File
@@ -110,6 +110,11 @@ export const componentItems: DocsItem[] = [
url: "/components/empty-state",
category: "feedback",
},
{
title: "ErrorMessage 错误消息",
url: "/components/error-message",
category: "forms",
},
{
title: "FieldError 字段错误",
url: "/components/field-error",
@@ -260,6 +265,11 @@ export const componentItems: DocsItem[] = [
url: "/components/tabs",
category: "navigation",
},
{
title: "TagGroup 标签组",
url: "/components/tag-group",
category: "collections",
},
{
title: "TextArea 多行文本框",
url: "/components/textarea",
+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)
+29
View File
@@ -0,0 +1,29 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Description,
ErrorMessage,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const selected = ref<TagKey[]>([]);
const isInvalid = computed(() => selected.value.length === 0);
</script>
<template>
<TagGroup v-model="selected" selection-mode="multiple">
<Label>必选分类</Label>
<TagGroupList>
<Tag id="news">新闻</Tag>
<Tag id="travel">旅游</Tag>
<Tag id="gaming">游戏</Tag>
<Tag id="shopping">购物</Tag>
</TagGroupList>
<Description>请至少选择一个分类</Description>
<ErrorMessage>{{ isInvalid ? "请至少选择一个分类" : "" }}</ErrorMessage>
</TagGroup>
</template>
@@ -0,0 +1,34 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Description,
ErrorMessage,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const selected = ref<TagKey[]>([]);
const isInvalid = computed(() => selected.value.length === 0);
</script>
<template>
<TagGroup
v-model="selected"
class="w-full max-w-xs gap-1.5"
selection-mode="multiple"
>
<Label>主题</Label>
<TagGroupList>
<Tag id="api">API</Tag>
<Tag id="design">设计</Tag>
<Tag id="docs">文档</Tag>
</TagGroupList>
<Description>请至少选择一个主题</Description>
<ErrorMessage class="font-medium text-danger">
{{ isInvalid ? "请至少选择一个主题" : "" }}
</ErrorMessage>
</TagGroup>
</template>
+32
View File
@@ -0,0 +1,32 @@
<script setup lang="ts">
import {
IconArticle,
IconPlanet,
IconRocket,
IconShoppingBag,
} from "@tabler/icons-vue";
import { Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
</script>
<template>
<TagGroup aria-label="标签" selection-mode="single">
<TagGroupList>
<Tag id="default-news">
<IconArticle aria-hidden="true" />
资讯
</Tag>
<Tag id="default-travel">
<IconPlanet aria-hidden="true" />
旅行
</Tag>
<Tag id="default-gaming">
<IconRocket aria-hidden="true" />
游戏
</Tag>
<Tag id="default-shopping">
<IconShoppingBag aria-hidden="true" />
购物
</Tag>
</TagGroupList>
</TagGroup>
</template>
+31
View File
@@ -0,0 +1,31 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Description,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const selected = ref<TagKey[]>(["news", "travel"]);
const summary = computed(() =>
selected.value.length > 0 ? selected.value.join(", ") : "无",
);
</script>
<template>
<div class="flex flex-col gap-3">
<TagGroup v-model="selected" selection-mode="multiple">
<Label>分类(受控)</Label>
<TagGroupList>
<Tag id="news">资讯</Tag>
<Tag id="travel">旅行</Tag>
<Tag id="gaming">游戏</Tag>
<Tag id="shopping">购物</Tag>
</TagGroupList>
<Description>已选:{{ summary }}</Description>
</TagGroup>
</div>
</template>
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
IconArticle,
IconPlanet,
IconRocket,
IconShoppingBag,
} from "@tabler/icons-vue";
import { Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
const tagClass =
"group/tag gap-1.5 rounded-full border border-neutral-300/80 bg-white/80 px-2.5 py-1 text-sm font-medium text-neutral-700 shadow-sm ring-1 ring-black/5 backdrop-blur-sm transition-colors data-[selected=true]:border-neutral-500 data-[selected=true]:bg-neutral-900 data-[selected=true]:text-neutral-50 dark:border-neutral-600/80 dark:bg-neutral-900/60 dark:text-neutral-200 dark:ring-white/10 dark:data-[selected=true]:border-neutral-400 dark:data-[selected=true]:bg-neutral-100 dark:data-[selected=true]:text-neutral-900";
const iconClass =
"size-4 shrink-0 text-neutral-500 group-data-[selected=true]/tag:text-current dark:text-neutral-400";
</script>
<template>
<TagGroup aria-label="主题" selection-mode="single">
<TagGroupList class="gap-2">
<Tag id="custom-news" :class="tagClass">
<IconArticle aria-hidden="true" :class="iconClass" />
资讯
</Tag>
<Tag id="custom-travel" :class="tagClass">
<IconPlanet aria-hidden="true" :class="iconClass" />
旅行
</Tag>
<Tag id="custom-gaming" :class="tagClass">
<IconRocket aria-hidden="true" :class="iconClass" />
游戏
</Tag>
<Tag id="custom-shopping" :class="tagClass">
<IconShoppingBag aria-hidden="true" :class="iconClass" />
购物
</Tag>
</TagGroupList>
</TagGroup>
</template>
+33
View File
@@ -0,0 +1,33 @@
<script setup lang="ts">
import {
Description,
Label,
Tag,
TagGroup,
TagGroupList,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-4">
<TagGroup selection-mode="single">
<Label>已禁用的标签</Label>
<TagGroupList>
<Tag id="disabled-news" is-disabled>资讯</Tag>
<Tag id="disabled-travel">旅行</Tag>
<Tag id="disabled-gaming" is-disabled>游戏</Tag>
</TagGroupList>
<Description>部分标签已禁用</Description>
</TagGroup>
<TagGroup :disabled-keys="['travel']" selection-mode="single">
<Label>禁用的键</Label>
<TagGroupList>
<Tag id="news">资讯</Tag>
<Tag id="travel">旅行</Tag>
<Tag id="gaming">游戏</Tag>
</TagGroupList>
<Description>通过 disabledKeys 属性禁用的标签</Description>
</TagGroup>
</div>
</template>
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
IconArticle,
IconPlanet,
IconRocket,
IconShoppingBag,
} from "@tabler/icons-vue";
import { Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
</script>
<template>
<!-- Upstream 的 render 函数在此对应 as(自定义根元素标签)。 -->
<TagGroup
aria-label="标签"
as="section"
data-custom="foo"
selection-mode="single"
>
<TagGroupList>
<Tag id="render-news">
<IconArticle aria-hidden="true" />
资讯
</Tag>
<Tag id="render-travel">
<IconPlanet aria-hidden="true" />
旅行
</Tag>
<Tag id="render-gaming">
<IconRocket aria-hidden="true" />
游戏
</Tag>
<Tag id="render-shopping">
<IconShoppingBag aria-hidden="true" />
购物
</Tag>
</TagGroupList>
</TagGroup>
</template>
@@ -0,0 +1,40 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Description,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const singleSelected = ref<TagKey[]>(["news"]);
const multipleSelected = ref<TagKey[]>(["news", "travel"]);
</script>
<template>
<div class="flex flex-col gap-8">
<TagGroup v-model="singleSelected" selection-mode="single">
<Label>单选</Label>
<TagGroupList>
<Tag id="single-news">资讯</Tag>
<Tag id="single-travel">旅行</Tag>
<Tag id="single-gaming">游戏</Tag>
<Tag id="single-shopping">购物</Tag>
</TagGroupList>
<Description>选择一个分类</Description>
</TagGroup>
<TagGroup v-model="multipleSelected" selection-mode="multiple">
<Label>多选</Label>
<TagGroupList>
<Tag id="multiple-news">资讯</Tag>
<Tag id="multiple-travel">旅行</Tag>
<Tag id="multiple-gaming">游戏</Tag>
<Tag id="multiple-shopping">购物</Tag>
</TagGroupList>
<Description>选择多个分类</Description>
</TagGroup>
</div>
</template>
+32
View File
@@ -0,0 +1,32 @@
<script setup lang="ts">
import { Label, Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-6">
<TagGroup selection-mode="single" size="sm">
<Label>小</Label>
<TagGroupList>
<Tag id="sm-news">资讯</Tag>
<Tag id="sm-travel">旅行</Tag>
<Tag id="sm-gaming">游戏</Tag>
</TagGroupList>
</TagGroup>
<TagGroup selection-mode="single" size="md">
<Label>中</Label>
<TagGroupList>
<Tag id="md-news">资讯</Tag>
<Tag id="md-travel">旅行</Tag>
<Tag id="md-gaming">游戏</Tag>
</TagGroupList>
</TagGroup>
<TagGroup selection-mode="single" size="lg">
<Label>大</Label>
<TagGroupList>
<Tag id="lg-news">资讯</Tag>
<Tag id="lg-travel">旅行</Tag>
<Tag id="lg-gaming">游戏</Tag>
</TagGroupList>
</TagGroup>
</div>
</template>
+25
View File
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { Label, Tag, TagGroup, TagGroupList } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-8">
<TagGroup selection-mode="single" variant="default">
<Label>默认</Label>
<TagGroupList>
<Tag id="default-news">资讯</Tag>
<Tag id="default-travel">旅行</Tag>
<Tag id="default-gaming">游戏</Tag>
</TagGroupList>
</TagGroup>
<TagGroup selection-mode="single" variant="surface">
<Label>表面</Label>
<TagGroupList>
<Tag id="surface-news">资讯</Tag>
<Tag id="surface-travel">旅行</Tag>
<Tag id="surface-gaming">游戏</Tag>
</TagGroupList>
</TagGroup>
</div>
</template>
@@ -0,0 +1,32 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Description,
ErrorMessage,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const selected = ref<TagKey[]>([]);
const isInvalid = computed(() => selected.value.length === 0);
</script>
<template>
<TagGroup v-model="selected" selection-mode="multiple">
<Label>设施</Label>
<TagGroupList>
<Tag id="laundry">洗衣</Tag>
<Tag id="fitness">健身中心</Tag>
<Tag id="parking">停车</Tag>
<Tag id="pool">游泳池</Tag>
<Tag id="breakfast">早餐</Tag>
</TagGroupList>
<Description>
{{ isInvalid ? "请至少选择一个分类" : "已选:" + selected.join(", ") }}
</Description>
<ErrorMessage>{{ isInvalid ? "请至少选择一个分类" : "" }}</ErrorMessage>
</TagGroup>
</template>
@@ -0,0 +1,121 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Avatar,
AvatarFallback,
AvatarImage,
Description,
EmptyState,
Label,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
type User = {
id: string;
name: string;
avatar: string;
fallback: string;
};
const users = ref<User[]>([
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
fallback: "F",
id: "fred",
name: "Fred",
},
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
fallback: "M",
id: "michael",
name: "Michael",
},
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
fallback: "J",
id: "jane",
name: "Jane",
},
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
fallback: "A",
id: "alice",
name: "Alice",
},
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
fallback: "B",
id: "bob",
name: "Bob",
},
{
avatar:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/black.jpg",
fallback: "C",
id: "charlie",
name: "Charlie",
},
]);
const selectedKeys = ref<TagKey[]>(["fred", "michael"]);
function onRemove(keys: TagKey[]) {
users.value = users.value.filter((user) => !keys.includes(user.id));
}
const selectedUsers = computed(() =>
users.value.filter((user) => selectedKeys.value.includes(user.id)),
);
</script>
<template>
<div class="w-sm">
<TagGroup
v-model="selectedKeys"
selection-mode="multiple"
@remove="onRemove"
>
<Label>团队成员</Label>
<TagGroupList :items="users">
<template #default="{ item }">
<Tag :id="item.id" :key="item.id" :text-value="item.name">
<Avatar class="size-4" size="sm">
<AvatarImage :src="item.avatar" />
<AvatarFallback>{{ item.fallback }}</AvatarFallback>
</Avatar>
{{ item.name }}
</Tag>
</template>
<template #empty>
<EmptyState class="p-1">暂无团队成员</EmptyState>
</template>
</TagGroupList>
<Description>为项目选择团队成员</Description>
</TagGroup>
<div v-if="selectedUsers.length > 0" class="mt-4 flex flex-col gap-2">
<p class="text-sm font-medium text-muted">已选:</p>
<div class="flex flex-wrap gap-2">
<div
v-for="user in selectedUsers"
:key="`${user.id}-selected`"
class="flex items-center gap-2 rounded-lg bg-surface-tertiary px-2 py-1"
>
<Avatar class="size-4" size="sm">
<AvatarImage :src="user.avatar" />
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
</Avatar>
<span class="text-sm">{{ user.name }}</span>
</div>
</div>
</div>
</div>
</template>
+79
View File
@@ -0,0 +1,79 @@
<script setup lang="ts">
import {
IconArticle,
IconPlanet,
IconRocket,
IconShoppingBag,
} from "@tabler/icons-vue";
import {
Avatar,
AvatarFallback,
AvatarImage,
Description,
Label,
Tag,
TagGroup,
TagGroupList,
} from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-8">
<TagGroup selection-mode="single">
<Label>带图标</Label>
<TagGroupList>
<Tag id="icon-news">
<IconArticle aria-hidden="true" />
News
</Tag>
<Tag id="icon-travel">
<IconPlanet aria-hidden="true" />
Travel
</Tag>
<Tag id="icon-gaming">
<IconRocket aria-hidden="true" />
Gaming
</Tag>
<Tag id="icon-shopping">
<IconShoppingBag aria-hidden="true" />
Shopping
</Tag>
</TagGroupList>
<Description>带图标的标签</Description>
</TagGroup>
<TagGroup selection-mode="single">
<Label>带头像</Label>
<TagGroupList>
<Tag id="avatar-fred">
<Avatar class="size-4">
<AvatarImage
src="https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg"
/>
<AvatarFallback>F</AvatarFallback>
</Avatar>
Fred
</Tag>
<Tag id="avatar-michael">
<Avatar class="size-4">
<AvatarImage
src="https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg"
/>
<AvatarFallback>M</AvatarFallback>
</Avatar>
Michael
</Tag>
<Tag id="avatar-jane">
<Avatar class="size-4">
<AvatarImage
src="https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg"
/>
<AvatarFallback>J</AvatarFallback>
</Avatar>
Jane
</Tag>
</TagGroupList>
<Description>带头像的标签</Description>
</TagGroup>
</div>
</template>
@@ -0,0 +1,88 @@
<script setup lang="ts">
import { ref } from "vue";
import { IconCircleXFilled } from "@tabler/icons-vue";
import {
Description,
EmptyState,
Label,
Tag,
TagGroup,
TagGroupList,
TagRemoveButton,
type TagKey,
type TagRenderState,
} from "@heroui-vue/components";
type TagItem = { id: string; name: string };
const tags = ref<TagItem[]>([
{ id: "news", name: "资讯" },
{ id: "travel", name: "旅行" },
{ id: "gaming", name: "游戏" },
{ id: "shopping", name: "购物" },
]);
const frameworks = ref<TagItem[]>([
{ id: "react", name: "React" },
{ id: "vue", name: "Vue" },
{ id: "angular", name: "Angular" },
{ id: "svelte", name: "Svelte" },
]);
function onRemoveTags(keys: TagKey[]) {
tags.value = tags.value.filter((tag) => !keys.includes(tag.id));
}
function onRemoveFrameworks(keys: TagKey[]) {
frameworks.value = frameworks.value.filter(
(framework) => !keys.includes(framework.id),
);
}
function allowsRemoving(state: TagRenderState) {
return state.allowsRemoving;
}
</script>
<template>
<div class="flex flex-col gap-8">
<div class="w-sm">
<TagGroup selection-mode="single" @remove="onRemoveTags">
<Label>默认移除按钮</Label>
<TagGroupList :items="tags">
<template #default="{ item }">
<Tag :id="item.id" :key="item.id" :text-value="item.name">
{{ item.name }}
</Tag>
</template>
<template #empty>
<EmptyState class="p-1">未找到分类</EmptyState>
</template>
</TagGroupList>
<Description>点击 × 移除标签</Description>
</TagGroup>
</div>
<div class="w-md">
<TagGroup selection-mode="single" @remove="onRemoveFrameworks">
<Label>自定义移除按钮</Label>
<TagGroupList :items="frameworks">
<template #default="{ item }">
<Tag :id="item.id" :key="item.id" :text-value="item.name">
<template #default="state">
{{ item.name }}
<TagRemoveButton v-if="allowsRemoving(state)">
<IconCircleXFilled aria-hidden="true" />
</TagRemoveButton>
</template>
</Tag>
</template>
<template #empty>
<EmptyState class="p-1">未找到框架</EmptyState>
</template>
</TagGroupList>
<Description>带图标的自定义移除按钮</Description>
</TagGroup>
</div>
</div>
</template>