Label(标签)
渲染与控件关联的无障碍标签。
loading
对于表单字段,请使用 Field 组件, 它内置标签、描述和错误处理。
安装
bash
npx shadcn@latest add label1
1. 安装以下依赖:
bash
npm install @base-ui/react1
2. 将以下代码复制并粘贴到项目中。
tsx
"use client"
import * as React from "react"
import { cn } from "cn"
function Label({ className, ...props }: React.ComponentProps<"label">) {
return (
<label
data-slot="label"
className={cn(
"flex items-center gap-2 type-body font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
className
)}
{...props}
/>
)
}
export { Label }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
3. 根据项目配置调整导入路径。
用法
tsx
import { Label } from "@/components/ui/label"1
tsx
<Label htmlFor="email">Your email address</Label>1
表单字段中的标签
对于表单字段,请使用 Field 组件, 它内置 FieldLabel、FieldDescription 和 FieldError 组件。
tsx
<Field>
<FieldLabel htmlFor="email">Your email address</FieldLabel>
<Input id="email" />
</Field>1
2
3
4
2
3
4
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
详情请参阅 Base UI Label 文档。