145a208687
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
213 lines
6.0 KiB
Markdown
213 lines
6.0 KiB
Markdown
---
|
||
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) · [Input 输入框](./input) · [TextArea 多行文本框](./textarea)
|