Components

复选框

¥Checkbox

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

import * as React from "react";
import { Checkbox } from "radix-ui";
import { CheckIcon } from "@radix-ui/react-icons";
import "./styles.css";
const CheckboxDemo = () => (
<form>
<div style={{ display: "flex", alignItems: "center" }}>
<Checkbox.Root className="CheckboxRoot" defaultChecked id="c1">
<Checkbox.Indicator className="CheckboxIndicator">
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Root>
<label className="Label" htmlFor="c1">
Accept terms and conditions.
</label>
</div>
</form>
);
export default CheckboxDemo;

Features

    Supports indeterminate state.

    Full keyboard navigation.

    Can be controlled or uncontrolled.

安装

¥Installation

从命令行安装组件。

¥Install the component from your command line.

npm install @radix-ui/react-checkbox

结构

¥Anatomy

导入所有部分并将它们组合在一起。

¥Import all parts and piece them together.

import { Checkbox } from "radix-ui";
export default () => (
<Checkbox.Root>
<Checkbox.Indicator />
</Checkbox.Root>
);

API 参考

¥API Reference

¥Root

包含复选框的所有部分。inputform 中使用时也会渲染,以确保事件正确传播。

¥Contains all the parts of a checkbox. An input will also render when used within a form to ensure events propagate correctly.

PropTypeDefault
asChild
boolean
false
defaultChecked
boolean | 'indeterminate'
No default value
checked
boolean | 'indeterminate'
No default value
onCheckedChange
function
No default value
disabled
boolean
No default value
required
boolean
No default value
name
string
No default value
value
string
on
Data attributeValues
[data-state]"checked" | "unchecked" | "indeterminate"
[data-disabled]

Present when disabled

指示符

¥Indicator

当复选框处于选中或不确定状态时渲染。你可以直接设置此元素的样式,也可以将其用作封装器来放置图标,或者两者兼而有之。

¥Renders when the checkbox is in a checked or indeterminate state. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.

PropTypeDefault
asChild
boolean
false
forceMount
boolean
No default value
Data attributeValues
[data-state]"checked" | "unchecked" | "indeterminate"
[data-disabled]

Present when disabled

示例

¥Examples

不确定

¥Indeterminate

你可以通过控制复选框的状态将其设置为 indeterminate

¥You can set the checkbox to indeterminate by taking control of its state.

import { DividerHorizontalIcon, CheckIcon } from "@radix-ui/react-icons";
import { Checkbox } from "radix-ui";
export default () => {
const [checked, setChecked] = React.useState("indeterminate");
return (
<>
<StyledCheckbox checked={checked} onCheckedChange={setChecked}>
<Checkbox.Indicator>
{checked === "indeterminate" && <DividerHorizontalIcon />}
{checked === true && <CheckIcon />}
</Checkbox.Indicator>
</StyledCheckbox>
<button type="button" onClick={() => setChecked((prevIsChecked) => prevIsChecked === "indeterminate" ? false : "indeterminate", ) } >
Toggle indeterminate
</button>
</>
);
};

可访问性

¥Accessibility

遵循 三态复选框 WAI-ARIA 设计模式

¥Adheres to the tri-state Checkbox WAI-ARIA design pattern.

键盘交互

¥Keyboard Interactions

KeyDescription
Space
Checks/unchecks the checkbox.
Previous头像