import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxBasicDemo() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
label="Accept terms and conditions"
checked={checked}
onCheckedChange={setChecked}
/>
);
}安装
批量导入
import { Checkbox } from "@cloudflare/kumo";按需导入
import { Checkbox } from "@cloudflare/kumo/components/checkbox";用法
import { Checkbox } from "@cloudflare/kumo";
export default function Example() {
return <Checkbox label="Accept terms" />;
}示例
默认
带内置标签的 Checkbox。标签会自动以水平布局显示(Checkbox 在标签之前)。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxDefaultDemo() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
label="Enable notifications"
checked={checked}
onCheckedChange={setChecked}
/>
);
}已选中
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxCheckedDemo() {
const [checked, setChecked] = useState(true);
return (
<Checkbox label="I agree" checked={checked} onCheckedChange={setChecked} />
);
}半选
用于”全选”模式中仅部分项目被选中的场景。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxIndeterminateDemo() {
const [indeterminate, setIndeterminate] = useState(true);
return (
<Checkbox
label="Select all"
indeterminate={indeterminate}
onCheckedChange={setIndeterminate}
/>
);
}标签在前的布局
使用 controlFirst={false} 将标签放在 Checkbox 之前。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxLabelFirstDemo() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
label="Remember me"
controlFirst={false}
checked={checked}
onCheckedChange={setChecked}
/>
);
}禁用
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxDisabledDemo() {
return <Checkbox label="Disabled option" disabled />;
}错误
错误变体提供视觉样式(红色描边)。若需显示错误提示文本,请使用 Checkbox.Group。
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxErrorDemo() {
return <Checkbox label="Invalid option" variant="error" />;
}Checkbox 分组
使用图例(legend)、描述和共享的错误提示消息将多个 Checkbox 归组。使用 Checkbox.Group 和 Checkbox.Item。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxGroupDemo() {
const [preferences, setPreferences] = useState<string[]>(["email"]);
return (
<Checkbox.Group
legend="Email preferences"
description="Choose how you'd like to receive updates"
value={preferences}
onValueChange={setPreferences}
>
<Checkbox.Item value="email" label="Email notifications" />
<Checkbox.Item value="sms" label="SMS notifications" />
<Checkbox.Item value="push" label="Push notifications" />
</Checkbox.Group>
);
}带错误的 Checkbox 分组
在分组级别显示校验错误。存在错误时,错误消息会替换描述。
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxGroupErrorDemo() {
return (
<Checkbox.Group
legend="Required preferences"
error="Please select at least one notification method"
value={[]}
onValueChange={() => {}}
>
<Checkbox.Item value="email" label="Email" variant="error" />
<Checkbox.Item value="sms" label="SMS" variant="error" />
</Checkbox.Group>
);
}卡片式 Checkbox
在组上使用 appearance="card",可将每个选项显示为单个卡片中可通过分隔线
分隔选择的行。可结合每个项目的 description 属性获得更丰富的内容。
卡片项目默认将 Checkbox 放在标签之后,除非设置了 controlFirst。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxCardDemo() {
const [products, setProducts] = useState<string[]>(["gateway"]);
return (
<Checkbox.Group
legend="Where should this profile scan?"
appearance="card"
value={products}
onValueChange={setProducts}
>
<Checkbox.Item
value="gateway"
label="Web traffic"
description="Uploads and downloads through Gateway HTTP policies."
/>
<Checkbox.Item
value="ai"
label="AI prompts"
description="Prompts sent to AI apps, and the responses they return."
/>
<Checkbox.Item
value="casb"
label="SaaS and cloud storage"
description="Files stored in connected apps and cloud accounts."
/>
<Checkbox.Item
value="email"
label="Outbound email"
description="Email leaving your organization."
/>
</Checkbox.Group>
);
}卡片式 Checkbox(水平)
将 appearance="card" 与 orientation="horizontal" 结合,可将选项在同一个
卡片内排列成两列。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
export function CheckboxCardHorizontalDemo() {
const [products, setProducts] = useState<string[]>(["gateway"]);
return (
<Checkbox.Group
legend="Where should this profile scan?"
appearance="card"
orientation="horizontal"
value={products}
onValueChange={setProducts}
>
<Checkbox.Item
value="gateway"
label="Web traffic"
description="Uploads and downloads through Gateway HTTP policies."
/>
<Checkbox.Item
value="ai"
label="AI prompts"
description="Prompts sent to AI apps, and the responses they return."
/>
<Checkbox.Item
value="casb"
label="SaaS and cloud storage"
description="Files stored in connected apps and cloud accounts."
/>
<Checkbox.Item
value="email"
label="Outbound email"
description="Email leaving your organization."
/>
</Checkbox.Group>
);
}视觉隐藏的图例
使用 Checkbox.Legend 并搭配 className="sr-only",可使图例保持对屏幕阅读器可用,同时在视觉上隐藏。当分组已由其父级 Field 或标题提供标签、显示图例会形成重复标签时,这一做法很有用。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
/** Shows Checkbox.Legend with sr-only to visually hide the legend while keeping it accessible, useful when a parent Field already provides a visible label */
export function CheckboxLegendSrOnlyDemo() {
const [preferences, setPreferences] = useState<string[]>(["email"]);
return (
<Checkbox.Group value={preferences} onValueChange={setPreferences}>
<Checkbox.Legend className="sr-only">
Notification preferences
</Checkbox.Legend>
<Checkbox.Item value="email" label="Email notifications" />
<Checkbox.Item value="sms" label="SMS notifications" />
<Checkbox.Item value="push" label="Push notifications" />
</Checkbox.Group>
);
}自定义图例样式
Checkbox.Legend 接受 className,可完全控制图例的呈现效果。当需要自定义排版、颜色或布局时,请用它替代 legend 字符串属性。
import { useState } from "react";
import { Checkbox } from "@cloudflare/kumo";
/** Shows Checkbox.Legend with custom styling for full control over legend presentation */
export function CheckboxLegendCustomDemo() {
const [preferences, setPreferences] = useState<string[]>(["email"]);
return (
<Checkbox.Group value={preferences} onValueChange={setPreferences}>
<Checkbox.Legend className="text-sm font-normal text-kumo-subtle">
Notification preferences
</Checkbox.Legend>
<Checkbox.Item value="email" label="Email notifications" />
<Checkbox.Item value="sms" label="SMS notifications" />
<Checkbox.Item value="push" label="Push notifications" />
</Checkbox.Group>
);
}API 参考
Checkbox
带有内置标签和水平布局的单个 Checkbox 组件。
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "error" | "default" | 视觉变体:"default" 或 "error" 用于校验失败(仅视觉,无错误文本) |
| label | ReactNode | - | 复选框的标签内容(启用内置 Field 包装器)- 可以是字符串或任何 React 节点 |
| labelTooltip | ReactNode | - | 通过信息图标显示在标签旁边的 Tooltip 内容。 |
| controlFirst | boolean | - | 当 true(默认)时,复选框显示在标签前。当 false 时,标签显示在复选框前。 |
| checked | boolean | - | 复选框是否被选中(受控) |
| indeterminate | boolean | - | 复选框是否处于不确定状态 |
| disabled | boolean | - | 复选框是否被禁用 |
| name | string | - | 表单提交时的名称 |
| required | boolean | - | 该字段是否为必填项 |
| className | string | - | 额外的类名 |
Checkbox.Group
用于多个 Checkbox 的包装器,支持图例、描述和错误提示。
| Prop | Type | Default | Description |
|---|---|---|---|
| legend | string | - | 分组的图例文本。若需更精细地控制图例样式,请省略此属性,改用 |
| children* | ReactNode | - | 子 |
| error | string | - | 组的错误消息(仅出现在组中,不适用于单个复选框) |
| description | ReactNode | - | 组的帮助文本 |
| value | string[] | - | 应被选中的复选框的值(受控) |
| allValues | string[] | - | 所有可能的复选框值(父复选框模式必填) |
| disabled | boolean | - | 是否禁用组中的所有复选框 |
| orientation | "vertical" | "horizontal" | - | 校验框项目的布局方向。 |
| appearance | KumoCheckboxAppearance | - | 应用于所有
|
| controlFirst | boolean | - | 为 true 时,校验框出现在标签之前;为 false 时,标签出现在校验框之前。默认外观下默认为 true,卡片外观下默认为 false。 |
| className | string | - | 额外的 CSS 类 |
Checkbox.Legend
Checkbox.Group 的可组合图例子组件。接受 className 以完全控制样式(例如使用 className="sr-only" 实现视觉隐藏)。当需要自定义图例样式时,用它替代 legend 字符串属性。
| Prop | Type | Default | Description |
|---|---|---|---|
| children* | ReactNode | - | 图例内容 |
| className | string | - | 额外的 CSS 类(例如用 "sr-only" 视觉隐藏图例) |
Checkbox.Item
Checkbox.Group 中的单个 Checkbox。
| Prop | Type | Default |
|---|
No component-specific props. Accepts standard HTML attributes.