feat: complete field documentation and TextArea examples

This commit is contained in:
maoyingjie
2026-10-21 23:41:57 +08:00
parent 37e11e6330
commit 116056832c
40 changed files with 2071 additions and 15 deletions
+212
View File
@@ -0,0 +1,212 @@
---
title: FieldError 字段错误
description: 展示表单字段校验错误消息
component: field-error
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { FieldError, TextField, Label, Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="field-error-basic" />
FieldError 组件展示表单字段的校验错误消息。当父字段标记为无效时自动显示,并提供平滑的透明度过渡。
## 示例
### 基础校验
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const value = ref("");
const isInvalid = computed(
() => value.value.length > 0 && value.value.length < 3,
);
</script>
<template>
<TextField v-model="value" class="w-64" :is-invalid="isInvalid">
<Label for="username">用户名</Label>
<Input id="username" placeholder="输入用户名" />
<FieldError>用户名至少需要 3 个字符</FieldError>
</TextField>
</template>
```
### 动态错误信息
使用 **FieldError** 作用域插槽读取当前字段错误。`is-invalid` 只负责展示;阻止提交请使用 `validate` 或 Form 的 `validation-errors`。下面示例用 `validate` + `validation-behavior="aria"`,输入时即可看到动态消息:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
return value.length < 8 ? "密码至少需要 8 个字符" : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="至少 8 个字符" />
<FieldError>
<template #default="{ validationErrors }">
{{ validationErrors.join(",") }}
</template>
</FieldError>
</TextField>
</template>
```
上游 `FieldError` 的 render 函数 `(validation) => …` 在 Vue 中为 **FieldError** 的作用域插槽 `#default="{ validationErrors, isInvalid }"`。当前未提供完整 React Aria `validationDetails` 对象。
### 自定义校验逻辑
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const email = ref("");
const isInvalid = computed(
() => email.value.length > 0 && !email.value.includes("@"),
);
</script>
<template>
<TextField v-model="email" :is-invalid="isInvalid">
<Label>邮箱</Label>
<Input type="email" />
<FieldError>邮箱必须包含 @ 符号</FieldError>
</TextField>
</template>
```
也可以用 `validate` 做同步提交拦截:
```vue
<TextField
v-model="email"
:validate="(v) => (v.includes('@') ? true : '邮箱必须包含 @ 符号')"
name="email"
>
<Label>邮箱</Label>
<Input type="email" />
<FieldError />
</TextField>
```
### 多条错误信息
默认插槽展示合并后的错误文本;需要逐条渲染时使用 FieldError 作用域插槽。下面示例的 `validate` 会返回多条错误:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
const errors: string[] = [];
if (value.length < 8) errors.push("密码至少需要 8 个字符");
if (!/[A-Z]/.test(value)) errors.push("需要包含一个大写字母");
return errors.length ? errors : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="试试 abc" />
<FieldError>
<template #default="{ validationErrors }">
<div v-for="(error, i) in validationErrors" :key="i">{{ error }}</div>
</template>
</FieldError>
</TextField>
</template>
```
## 自定义样式
### Tailwind CSS
<ComponentPreview name="field-error-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 FieldError 组件类。
```css
@layer components {
.hv-field-error {
@apply font-medium text-danger;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
FieldError 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/field-error.css)):
#### 基础类 [!toc]
- `.hv-field-error` - 基础错误样式,danger 色
- 仅在存在 `data-visible` 属性时显示
- 长消息自动换行(`wrap-break-word`)
## API 参考
### FieldError
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ---------------------------------------------------- |
| `id` | `string` | 自动 | 显式 id;未指定时生成并关联到控件 `aria-describedby` |
| 默认插槽 | `Component` | - | 错误消息;作用域提供 `validationErrors`、`isInvalid` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
字段无效时渲染;默认展示错误数组(空格连接)。在 Switch 等 toggle 字段内也可使用。
## 无障碍
FieldError 组件通过以下方式确保无障碍性:
- 使用 `role="alert"` 播报错误
- 语义化 HTML 支持屏幕阅读器
- 提供视觉与程序化错误指示(`aria-describedby` 关联)
- 根据校验状态自动管理可见性
当前不提供完整 React Aria `validationDetails` 对象;第三方契约请勿据此宣称完全兼容。
## 相关组件
[TextField 文本输入框](./text-field) · [Label 标签](./label) · [Description 描述](./description)