🌐 Nodejs.cn

复选框

允许用户在选中和未选中之间切换的控件。

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,或者使用 checkedonCheckedChange 来控制状态。

🌐 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

将复选框与 FieldFieldLabel 配对,以实现正确的布局和标注。

🌐 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

使用 FieldContentFieldDescription 作为辅助文本。

🌐 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.

Show these items on the desktop:

Select the items you want to show on the desktop.

import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,

表格

🌐 Table

NameEmailRole
Sarah Chensarah.chen@example.comAdmin
Marcus Rodriguezmarcus.rodriguez@example.comUser
Priya Patelpriya.patel@example.comUser
David Kimdavid.kim@example.comEditor
"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

有关更多信息,请参阅 Radix UI 文档。

🌐 See the Radix UI documentation for more information.