Files
heroui-vue/apps/docs/components/error-message.md
T
maoyingjie af6bfda3ee 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.
2026-10-25 10:45:43 +08:00

4.1 KiB

title, description, component, group
title description component group
ErrorMessage 错误消息 用于展示错误的底层错误消息组件 error-message 组件

用法

<script setup lang="ts">
import { ErrorMessage } from "@heroui-vue/components";
</script>

<template>
  <ErrorMessage>请至少选择一个分类</ErrorMessage>
</template>

ErrorMessage 是基于固定基线 Text 组件 errorMessage slot 的底层组件,用于在 非表单组件(如 TagGroup、Calendar 及其他集合类组件)中展示错误消息。

组件结构

<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,它提供表单专用校验特性与自动错误处理,遵循标准化表单校验模式

ErrorMessage 与 FieldError

组件 使用场景 表单集成 示例组件
ErrorMessage 非表单组件 否 TagGroup、Calendar
FieldError 表单字段 是 TextField、NumberField、Select

表单校验建议使用 FieldError,它遵循标准化表单校验模式并提供表单专用特性。

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 ErrorMessage 组件类。了解更多。

@layer components {
  .hv-error-message {
    @apply text-sm font-medium text-danger;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

ErrorMessage 组件使用以下 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 字段错误 · Description 描述 · TagGroup 标签组