Back to Ant Design

Checkbox

components/checkbox/index.zh-CN.md

6.3.73.7 KB
Original Source

何时使用 {#when-to-use}

  • 在一组可选项中进行多项选择时;
  • 单独使用可以表示两种状态之间的切换,和 switch 类似。区别在于切换 switch 会直接触发状态改变,而 checkbox 一般用于状态标记,需要和提交操作配合。

代码演示 {#examples}

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">基本用法</code> <code src="./demo/disabled.tsx">不可用</code> <code src="./demo/controller.tsx">受控的 Checkbox</code> <code src="./demo/group.tsx">Checkbox 组</code> <code src="./demo/check-all.tsx">全选</code> <code src="./demo/layout.tsx">布局</code> <code src="./demo/style-class.tsx" version="6.0.0">自定义语义结构的样式和类</code> <code src="./demo/debug-line.tsx" debug>同行布局</code> <code src="./demo/debug-disable-popover.tsx" debug>禁用下的 Tooltip</code> <code src="./demo/debug-group-width.tsx" debug>Group 内勾选框等宽</code> <code src="./demo/custom-line-width.tsx" debug>自定义 lineWidth</code>

API

通用属性参考:通用属性

Checkbox

参数说明类型默认值版本
checked指定当前是否选中booleanfalse
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
defaultChecked初始是否选中booleanfalse
disabled失效状态booleanfalse
indeterminate设置 indeterminate 状态,只负责样式控制booleanfalse
onChange变化时的回调函数(e: CheckboxChangeEvent) => void-
onBlur失去焦点时的回调function()-
onFocus获得焦点时的回调function()-
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-

Checkbox.Group

参数说明类型默认值版本
defaultValue默认选中的选项(string | number)[][]
disabled整组失效booleanfalse
nameCheckboxGroup 下所有 input[type="checkbox"]name 属性string-
options指定可选项string[] | number[] | Option[][]
value指定选中的选项(string | number | boolean)[][]
title选项的 titlestring-
className选项的类名string-5.25.0
style选项的样式React.CSSProperties-
onChange变化时的回调函数(checkedValue: T[]) => void-
Option
typescript
interface Option {
  label: string;
  value: string;
  disabled?: boolean;
}

方法 {#methods}

Checkbox

名称描述版本
blur()移除焦点
focus()获取焦点
nativeElement返回 Checkbox 的 DOM 节点5.17.3

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

主题变量(Design Token){#design-token}

<ComponentTokenTable component="Checkbox"></ComponentTokenTable>

FAQ

为什么在 Form.Item 下不能绑定数据? {#faq-form-item-limitations}

Form.Item 默认绑定值属性到 value 上,而 Checkbox 的值属性为 checked。你可以通过 valuePropName 来修改绑定的值属性。

tsx
<Form.Item name="fieldA" valuePropName="checked">
  <Checkbox />
</Form.Item>