表单与选择@neumorphism-ui/checkbox

Checkbox

在透明的原生 input 上叠加拟物表面,同时保留表单提交和浏览器无障碍能力。支持受控、非受控、indeterminate 状态以及 shadcn 风格的 onCheckedChange。

@neumorphism-ui/checkbox
Registry JSON

预览

安装

安装: checkboxnpx shadcn@latest add @neumorphism-ui/checkbox

CLI 会依次处理 npm 依赖、base token、utils 和组件源码。

首次使用此 Registry?请先完成 namespace 配置 。

用法

用法: 安装npx shadcn@latest add @neumorphism-ui/checkbox @neumorphism-ui/label

Import

Checkbox import 代码
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"

Example

Checkbox 用法代码
<div className="flex items-center gap-2">
  <Checkbox id="updates" defaultChecked />
  <Label htmlFor="updates">接收产品更新</Label>
</div>

API 参考

组件属性类型默认值说明
Checkboxcheckedboolean | "indeterminate"—受控的选中或混合状态。
CheckboxdefaultCheckedbooleanfalse非受控模式下的初始选中状态。
CheckboxonCheckedChange(checked: boolean | "indeterminate") => void—选中状态变化时调用。
Checkboxdisabledbooleanfalse禁用输入和指针操作。
Checkboxname / value / requirednative input props—用于表单提交的原生 checkbox 属性。

无障碍

  • 01

    使用真实的 input[type=checkbox],因此会保留表单提交和 Space 键操作。

  • 02

    将 Label 的 htmlFor 与 Checkbox 的 id 关联,或将 Checkbox 放在 label 内。

  • 03

    在 indeterminate 状态下,会同时设置 aria-checked=mixed 和 DOM indeterminate 属性。