2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 数据录入
|
2016-09-21 11:28:38 +08:00
|
|
|
|
title: Checkbox
|
2024-03-22 14:22:42 +08:00
|
|
|
|
subtitle: 多选框
|
|
|
|
|
description: 收集用户的多项选择。
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*DzgiRbW3khIAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*G3MjTYXL6AIAAAAAAAAAAAAADrJ8AQ/original
|
2022-11-09 12:28:04 +08:00
|
|
|
|
demo:
|
|
|
|
|
cols: 2
|
2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
2015-07-15 15:19:24 +08:00
|
|
|
|
|
|
|
|
|
## 何时使用
|
|
|
|
|
|
2016-01-13 21:56:51 +08:00
|
|
|
|
- 在一组可选项中进行多项选择时;
|
|
|
|
|
- 单独使用可以表示两种状态之间的切换,和 `switch` 类似。区别在于切换 `switch` 会直接触发状态改变,而 `checkbox` 一般用于状态标记,需要和提交操作配合。
|
2015-07-15 15:19:24 +08:00
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<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/debug-line.tsx" debug>同行布局</code>
|
2022-12-26 23:35:18 +08:00
|
|
|
|
<code src="./demo/debug-disable-popover.tsx" debug>禁用下的 Tooltip</code>
|
2023-12-13 21:46:25 +08:00
|
|
|
|
<code src="./demo/custom-line-width.tsx" debug>自定义 lineWidth</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
2015-07-15 15:19:24 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2018-06-03 16:28:10 +08:00
|
|
|
|
#### Checkbox
|
2015-07-15 15:19:24 +08:00
|
|
|
|
|
2022-12-06 21:09:59 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| autoFocus | 自动获取焦点 | boolean | false | |
|
|
|
|
|
| checked | 指定当前是否选中 | boolean | false | |
|
|
|
|
|
| defaultChecked | 初始是否选中 | boolean | false | |
|
|
|
|
|
| disabled | 失效状态 | boolean | false | |
|
|
|
|
|
| indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | |
|
2023-02-05 11:13:38 +08:00
|
|
|
|
| onChange | 变化时的回调函数 | (e: CheckboxChangeEvent) => void | - | |
|
2024-09-14 14:39:53 +08:00
|
|
|
|
| onBlur | 失去焦点时的回调 | function() | - | |
|
|
|
|
|
| onFocus | 获得焦点时的回调 | function() | - | |
|
2015-12-24 15:03:06 +08:00
|
|
|
|
|
2018-06-03 16:28:10 +08:00
|
|
|
|
#### Checkbox Group
|
2015-12-24 15:03:06 +08:00
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
2022-12-06 21:09:59 +08:00
|
|
|
|
| defaultValue | 默认选中的选项 | (string \| number)\[] | \[] | |
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| disabled | 整组失效 | boolean | false | |
|
|
|
|
|
| name | CheckboxGroup 下所有 `input[type="checkbox"]` 的 `name` 属性 | string | - | |
|
|
|
|
|
| options | 指定可选项 | string\[] \| number\[] \| Option\[] | \[] | |
|
2023-03-12 17:37:32 +08:00
|
|
|
|
| value | 指定选中的选项 | (string \| number \| boolean)\[] | \[] | |
|
2024-05-27 15:06:38 +08:00
|
|
|
|
| onChange | 变化时的回调函数 | (checkedValue: T[]) => void | - | |
|
2017-12-01 19:28:41 +08:00
|
|
|
|
|
2019-12-01 15:46:17 +08:00
|
|
|
|
##### Option
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
interface Option {
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-06-03 16:28:10 +08:00
|
|
|
|
### 方法
|
2017-12-01 19:28:41 +08:00
|
|
|
|
|
2018-06-03 16:28:10 +08:00
|
|
|
|
#### Checkbox
|
2017-12-01 19:28:41 +08:00
|
|
|
|
|
2024-05-18 15:07:26 +08:00
|
|
|
|
| 名称 | 描述 | 版本 |
|
|
|
|
|
| ------------- | ------------------------- | ------ |
|
|
|
|
|
| blur() | 移除焦点 | |
|
|
|
|
|
| focus() | 获取焦点 | |
|
|
|
|
|
| nativeElement | 返回 Checkbox 的 DOM 节点 | 5.17.3 |
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
2023-08-30 23:02:35 +08:00
|
|
|
|
## 主题变量(Design Token)
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
|
|
|
|
<ComponentTokenTable component="Checkbox"></ComponentTokenTable>
|
2023-09-04 14:10:39 +08:00
|
|
|
|
|
|
|
|
|
## FAQ
|
|
|
|
|
|
|
|
|
|
### 为什么在 Form.Item 下不能绑定数据?
|
|
|
|
|
|
|
|
|
|
Form.Item 默认绑定值属性到 `value` 上,而 Checkbox 的值属性为 `checked`。你可以通过 `valuePropName` 来修改绑定的值属性。
|
|
|
|
|
|
|
|
|
|
```tsx | pure
|
|
|
|
|
<Form.Item name="fieldA" valuePropName="checked">
|
|
|
|
|
<Checkbox />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
```
|