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
+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>