- 手风琴
- 警报
- 警报对话框
- 纵横比
- 头像
- 徽章
- 面包屑
- 按钮
- 按钮组
- 日历
- 卡片
- 轮播
- 图表
- 复选框
- 可折叠
- 组合框
- 命令
- 上下文菜单
- 数据表格
- 日期选择器
- 对话框
- 方向
- 抽屉
- 下拉菜单
- 空
- 字段
- 悬停卡
- 输入
- 输入组
- 输入 OTP
- 条目
- 键盘
- 标签
- 菜单栏
- 原生选择
- 导航菜单
- 分页
- 弹出窗口
- 进度
- 单选按钮组
- 可调整大小
- 滚动区域
- 选择
- 分隔符
- 工作表
- 侧边栏
- 骨架
- 滑块
- Sonner
- 旋转器
- 切换
- 表格
- 标签
- 文本区域
- Toast
- 切换
- 切换组
- 工具提示
- 排版
- 手风琴
- 警报
- 警报对话框
- 纵横比
- 头像
- 徽章
- 面包屑
- 按钮
- 按钮组
- 日历
- 卡片
- 轮播
- 图表
- 复选框
- 可折叠
- 组合框
- 命令
- 上下文菜单
- 数据表格
- 日期选择器
- 对话框
- 方向
- 抽屉
- 下拉菜单
- 空
- 字段
- 悬停卡
- 输入
- 输入组
- 输入 OTP
- 条目
- 键盘
- 标签
- 菜单栏
- 原生选择
- 导航菜单
- 分页
- 弹出窗口
- 进度
- 单选按钮组
- 可调整大小
- 滚动区域
- 选择
- 分隔符
- 工作表
- 侧边栏
- 骨架
- 滑块
- Sonner
- 旋转器
- 切换
- 表格
- 标签
- 文本区域
- Toast
- 切换
- 切换组
- 工具提示
- 排版
By clicking this checkbox, you agree to the terms.
"use client"
import { Checkbox } from "@/components/ui/checkbox"安装
🌐 Installation
pnpm dlx shadcn@latest add checkbox
用法
🌐 Usage
import { Checkbox } from "@/components/ui/checkbox"<Checkbox />已选状态
🌐 Checked State
对于不受控的复选框使用 defaultChecked,或者使用 checked 和 onCheckedChange 来控制状态。
🌐 Use defaultChecked for uncontrolled checkboxes, or checked and
onCheckedChange to control the state.
import * as React from "react"
export function Example() {
const [checked, setChecked] = React.useState(false)
return <Checkbox checked={checked} onCheckedChange={setChecked} />
}无效状态
🌐 Invalid State
在复选框上设置 aria-invalid,在字段封装器上设置 data-invalid 以显示无效样式。
🌐 Set aria-invalid on the checkbox and data-invalid on the field wrapper to
show the invalid styles.
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
示例
🌐 Examples
基础
🌐 Basic
将复选框与 Field 和 FieldLabel 配对,以实现正确的布局和标注。
🌐 Pair the checkbox with Field and FieldLabel for proper layout and labeling.
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
描述
🌐 Description
使用 FieldContent 和 FieldDescription 作为辅助文本。
🌐 Use FieldContent and FieldDescription for helper text.
By clicking this checkbox, you agree to the terms and conditions.
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,已禁用
🌐 Disabled
使用 disabled 属性来防止交互,并在 <Field> 组件上添加 data-disabled 属性以应用禁用样式。
🌐 Use the disabled prop to prevent interaction and add the data-disabled attribute to the <Field> component for disabled styles.
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
群组
🌐 Group
使用多个字段来创建复选框列表。
🌐 Use multiple fields to create a checkbox list.
import { Checkbox } from "@/components/ui/checkbox"
import {
Field,表格
🌐 Table
| Name | Role | ||
|---|---|---|---|
| Sarah Chen | sarah.chen@example.com | Admin | |
| Marcus Rodriguez | marcus.rodriguez@example.com | User | |
| Priya Patel | priya.patel@example.com | User | |
| David Kim | david.kim@example.com | Editor |
"use client"
import * as React from "react"从右到左
🌐 RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
🌐 To enable RTL support in shadcn/ui, see the RTL configuration guide.
بالنقر على هذا المربع، فإنك توافق على الشروط.
"use client"
import * as React from "react"API参考
🌐 API Reference
有关更多信息,请参见 Base UI 文档。
🌐 See the Base UI documentation for more information.