Checkbox
在透明的原生 input 上叠加拟物表面,同时保留表单提交和浏览器无障碍能力。支持受控、非受控、indeterminate 状态以及 shadcn 风格的 onCheckedChange。
@neumorphism-ui/checkbox预览
安装
安装: checkbox
npx shadcn@latest add @neumorphism-ui/checkboxCLI 会依次处理 npm 依赖、base token、utils 和组件源码。
首次使用此 Registry?请先完成 namespace 配置 。
用法
用法: 安装
npx shadcn@latest add @neumorphism-ui/checkbox @neumorphism-ui/labelImport
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 参考
| 组件 | 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|---|
Checkbox | checked | boolean | "indeterminate" | — | 受控的选中或混合状态。 |
Checkbox | defaultChecked | boolean | false | 非受控模式下的初始选中状态。 |
Checkbox | onCheckedChange | (checked: boolean | "indeterminate") => void | — | 选中状态变化时调用。 |
Checkbox | disabled | boolean | false | 禁用输入和指针操作。 |
Checkbox | name / value / required | native input props | — | 用于表单提交的原生 checkbox 属性。 |
无障碍
- 01
使用真实的 input[type=checkbox],因此会保留表单提交和 Space 键操作。
- 02
将 Label 的 htmlFor 与 Checkbox 的 id 关联,或将 Checkbox 放在 label 内。
- 03
在 indeterminate 状态下,会同时设置 aria-checked=mixed 和 DOM indeterminate 属性。