dc859fb8ae
HD-05 scope, against heroui ce90ea3 styles sources: - add labelVariants/descriptionVariants/fieldErrorVariants/ textFieldVariants/switchGroupVariants (+ VariantProps types) to @heroui-vue/styles and re-export them from @heroui-vue/components; variant keys, defaultVariants and slot sets match the pinned *.styles.ts files key for key (hv- namespace only) - Label/Description/FieldError/useTextField/SwitchGroup now consume those mappings instead of inline class strings, so the state classes have a single source; rendered class output is unchanged and pinned by tests/shared-field-styles.test.ts (mapping output <-> component output) - restore the global border-color fallback (*, ::after, ::before, ::backdrop, ::file-selector-button -> border-color: var(--border, currentColor)) in a new base/base.css, imported layer(base) right after preflight like the pinned entry: a low-priority default that components, utilities and consumer CSS override without !important - view-transition globals from the pinned base.css stay unported: no View Transitions API usage exists in the library or docs site - shared-styles package contract: packed runtime exports from both entries, variant-output assertions, CSS tokens incl. the fallback, strict nodenext+bundler type contract, and a real-browser consumer fixture (light/dark --border, explicit colors, consumer overrides, currentColor fallback) wired into check-package.mjs - docs: styling layers/border fallback/exports notes, five component API pages, compatibility matrix and upstream source records Full suite 111 files / 1634 tests, typecheck and build:docs pass; shared-styles + api-root-aliases + handbook-styles verified against the final tarballs.
215 lines
6.2 KiB
Markdown
215 lines
6.2 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)):
|
||
|
||
> 变体映射 `fieldErrorVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `FieldErrorVariants` 类型);组件渲染的类名由该映射生成。
|
||
|
||
#### 基础类 [!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)
|