Files
heroui-vue/apps/docs/components/error-message.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

125 lines
4.0 KiB
Markdown

---
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`
## 相关组件
[TagGroup 标签组](./tag-group)