- 手风琴
- 提示
- 警告对话框
- 宽高比
- 头像
- 徽章
- 面包屑导航
- 按钮
- 按钮组
- 日历 Calendar
- 卡片
- Carousel
- 图表 Chart
- 复选框
- 折叠面板
- 组合框
- 命令
- 上下文菜单
- 数据表格 Data Table
- 日期选择器 Date Picker
- 对话框 Dialog
- 抽屉
- 下拉菜单
- Empty
- 字段
- 悬停卡片
- 输入
- 输入组
- 输入 OTP
- 项目
- Kbd
- 标签
- 菜单栏
- 原生选择框
- 导航菜单 Navigation Menu
- 分页
- 弹出框
- 进度 Progress
- 单选框组
- 可调整大小
- 滚动区域 Scroll Area
- 选择框
- 分隔符 Separator
- 侧边栏 Sheet
- 侧边栏 Sidebar
- 骨架屏
- 滑块
- Sonner
- 加载指示器 Spinner
- 开关
- 表格
- 标签页 Tabs
- 文本域
- 吐司
- 切换按钮 Toggle
- 切换组
- 提示 Tooltip
- 排版
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {安装
pnpm dlx shadcn@latest add field
用法
import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
FieldTitle,
} from "@/components/ui/field"<FieldSet>
<FieldLegend>个人信息</FieldLegend>
<FieldDescription>这会显示在发票和邮件中。</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel htmlFor="name">全名</FieldLabel>
<Input id="name" autoComplete="off" placeholder="邪恶兔子" />
<FieldDescription>这会显示在发票和邮件中。</FieldDescription>
</Field>
<Field>
<FieldLabel htmlFor="username">用户名</FieldLabel>
<Input id="username" autoComplete="off" aria-invalid />
<FieldError>请选择其他用户名。</FieldError>
</Field>
<Field orientation="horizontal">
<Switch id="newsletter" />
<FieldLabel htmlFor="newsletter">订阅新闻邮件</FieldLabel>
</Field>
</FieldGroup>
</FieldSet>结构
Field 组件族专为构建可访问表单设计。一个典型字段结构如下:
<Field>
<FieldLabel htmlFor="input-id">标签</FieldLabel>
{/* 输入框、下拉选择、开关等 */}
<FieldDescription>可选的帮助文本。</FieldDescription>
<FieldError>验证信息。</FieldError>
</Field>Field是单个字段的核心容器。FieldContent是一个弹性列布局,用于包裹标签和描述。如果没有描述,则非必需。- 使用
FieldGroup包裹相关字段,并搭配FieldSet和FieldLegend进行语义分组。
表单
参见 Form 文档,了解如何结合 Field 组件与 React Hook Form 或 Tanstack Form 构建表单。
示例
输入框
import {
Field,
FieldDescription,多行文本框
import {
Field,
FieldDescription,选择框
Select your department or area of work.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import {
Select,滑块
Set your budget range ($200 - 800).
"use client"
import * as React from "react"字段集
import {
Field,
FieldDescription,复选框
Your Desktop & Documents folders are being synced with iCloud Drive. You can access them from other devices.
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,单选框
import {
Field,
FieldDescription,开关
import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
选择卡片
将多个 Field 组件包裹在 FieldLabel 内,形成可选择的字段组。适用于 RadioItem、Checkbox 和 Switch 组件。
import {
Field,
FieldContent,字段组
使用 FieldGroup 垂直堆叠多个 Field,通过添加 FieldSeparator 分隔它们。
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,响应式布局
- 垂直字段: 默认堆叠标签、控件和帮助文本,适合移动优先布局。
- 水平字段: 在
Field上设置orientation="horizontal"来让标签和控件横向排列。结合使用FieldContent保持描述对齐。 - 响应式字段: 设置
orientation="responsive",实现容器感知的自动列布局。在FieldGroup上应用@container/field-group类,在特定断点切换方向。
import { Button } from "@/components/ui/button"
import {
Field,验证与错误
- 给
Field添加data-invalid属性,将整个块切换为错误状态。 - 在输入控件本身添加
aria-invalid,支持辅助技术识别。 - 在控件后或
FieldContent内渲染FieldError,以保持错误信息与字段对齐。
<Field data-invalid>
<FieldLabel htmlFor="email">电子邮箱</FieldLabel>
<Input id="email" type="email" aria-invalid />
<FieldError>请输入有效的电子邮件地址。</FieldError>
</Field>可访问性
FieldSet和FieldLegend用于对相关控件进行分组,方便键盘和辅助技术用户操作。Field会输出role="group",使嵌套控件能继承来自FieldLabel和FieldLegend的标签信息。- 谨慎使用
FieldSeparator,保证屏幕阅读器能识别清晰的区域边界。
API 参考
FieldSet
语义化的 fieldset 容器,自带间距预设。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<FieldSet>
<FieldLegend>配送信息</FieldLegend>
<FieldGroup>{/* 字段们 */}</FieldGroup>
</FieldSet>FieldLegend
FieldSet 的标题元素。切换为 label 变体以匹配标签大小。
| 属性 | 类型 | 默认值 |
|---|---|---|
variant | "legend" | "label" | "legend" |
className | string |
<FieldLegend variant="label">通知偏好</FieldLegend>FieldLegend 有两个变体:legend 和 label。label 变体应用标签大小和对齐,适合嵌套 FieldSet 时使用。
FieldGroup
布局包装器,垂直堆叠多个 Field,并启用容器查询实现响应式方向切换。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<FieldGroup className="@container/field-group flex flex-col gap-6">
<Field>{/* ... */}</Field>
<Field>{/* ... */}</Field>
</FieldGroup>Field
单个字段的核心包装器。提供方向控制、无效状态样式和间距设置。
| 属性 | 类型 | 默认值 |
|---|---|---|
orientation | "vertical" | "horizontal" | "responsive" | "vertical" |
className | string | |
data-invalid | boolean |
<Field orientation="horizontal">
<FieldLabel htmlFor="remember">记住我</FieldLabel>
<Switch id="remember" />
</Field>FieldContent
弹性列布局,包裹控件和描述,适用于标签与控件并列的情况。如果无描述,可省略。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<Field>
<Checkbox id="notifications" />
<FieldContent>
<FieldLabel htmlFor="notifications">通知</FieldLabel>
<FieldDescription>电子邮件、短信和推送选项。</FieldDescription>
</FieldContent>
</Field>FieldLabel
用于直接输入控件和嵌套 Field 子元素的标签样式。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | |
asChild | boolean | false |
<FieldLabel htmlFor="email">电子邮箱</FieldLabel>FieldTitle
在 FieldContent 内渲染具有标签样式的标题。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<FieldContent>
<FieldTitle>启用 Touch ID</FieldTitle>
<FieldDescription>更快解锁设备。</FieldDescription>
</FieldContent>FieldDescription
帮助文本区域,能在水平布局中自动平衡长行文本。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<FieldDescription>我们绝不会泄露您的邮箱给任何人。</FieldDescription>FieldSeparator
视觉分隔符,用于在 FieldGroup 内分割区域。支持可选的内嵌内容。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
<FieldSeparator>或继续使用</FieldSeparator>FieldError
辅助访问的错误容器,支持接受子节点或 errors 数组(例如来自 react-hook-form)。
| 属性 | 类型 | 默认值 |
|---|---|---|
errors | Array<{ message?: string } | undefined> | |
className | string |
<FieldError errors={errors.username} />当 errors 数组包含多条错误消息时,组件会自动渲染错误列表。
FieldError 也支持任何实现了 Standard Schema 的验证器产生的问题数据,包括 Zod、Valibot 和 ArkType。直接传递模式结果中的 issues 数组,即可跨库统一渲染错误列表。