Label

用于显示表单控件的名称,并让标签文字成为可点击区域。

预览

输入与设置标签

点击邮箱标签以聚焦输入,或点击周报标签切换关联的开关。

点击标签会将焦点移到关联的输入控件。

安装

首次使用此 Registry?请先完成 基础配置与共享样式安装 。

npx shadcn@latest add @neumorphism-ui/label

先安装一次共享 base,然后使用 CLI 添加组件及其依赖。

输入与设置标签

npx shadcn@latest add @neumorphism-ui/input @neumorphism-ui/label @neumorphism-ui/switch
Registry JSON

用法

将 Label 的 htmlFor 与控件 id 保持一致。标签始终可见,示例或验证信息使用独立的辅助文字。

"use client";

import { useId } from "react";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

export default function Example() {
  const id = useId();
  return (
    <div className="grid gap-2">
      <Label htmlFor={id}>Project name</Label>
      <Input id={id} name="projectName" />
    </div>
  );
}

API 参考

对原生 label 的轻量封装,透传所有标准属性,并提供与 peer-disabled 状态一致的文字颜色和 cursor。

组件属性类型默认值说明
LabelhtmlForstring—要关联的 form control id。
Labelchildren必填React.ReactNode—说明 control 用途的文本。
LabelclassNamestring—扩展布局和状态样式。

无障碍

  • 01

    htmlFor 必须与目标 control 的 id 完全一致。

  • 02

    为 RadioGroup 等多个 control 命名时,应使用 fieldset/legend 或 aria-labelledby,而不是单独的 Label。

  • 03

    必填状态和辅助说明不仅要有视觉标识,还应通过 required、aria-describedby 传达。