2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
2016-09-21 11:28:38 +08:00
|
|
|
|
subtitle: 开关
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 数据录入
|
2016-09-21 11:28:38 +08:00
|
|
|
|
title: Switch
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*rtArRpBNDZcAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*al07RK8SGf4AAAAAAAAAAAAADrJ8AQ/original
|
2022-11-09 12:28:04 +08:00
|
|
|
|
demo:
|
|
|
|
|
cols: 2
|
2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
2015-06-25 19:55:15 +08:00
|
|
|
|
|
2015-08-10 15:33:46 +08:00
|
|
|
|
开关选择器。
|
2015-06-25 19:55:15 +08:00
|
|
|
|
|
2015-06-26 14:42:30 +08:00
|
|
|
|
## 何时使用
|
2015-06-25 19:55:15 +08:00
|
|
|
|
|
2015-06-26 14:42:30 +08:00
|
|
|
|
- 需要表示开关状态/两种状态之间的切换时;
|
2020-11-08 15:04:02 +08:00
|
|
|
|
- 和 `checkbox` 的区别是,切换 `switch` 会直接触发状态改变,而 `checkbox` 一般用于状态标记,需要和提交操作配合。
|
2015-07-08 19:47:45 +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/text.tsx">文字和图标</code>
|
|
|
|
|
<code src="./demo/size.tsx">两种大小</code>
|
|
|
|
|
<code src="./demo/loading.tsx">加载中</code>
|
2023-08-16 19:17:12 +08:00
|
|
|
|
<code src="./demo/component-token.tsx" debug>自定义组件 Token</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
2015-07-08 19:47:45 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 |
|
|
|
|
|
| --- | --- | --- | --- |
|
|
|
|
|
| autoFocus | 组件自动获取焦点 | boolean | false |
|
|
|
|
|
| checked | 指定当前是否选中 | boolean | false |
|
|
|
|
|
| checkedChildren | 选中时的内容 | ReactNode | - |
|
|
|
|
|
| className | Switch 器类名 | string | - |
|
|
|
|
|
| defaultChecked | 初始是否选中 | boolean | false |
|
|
|
|
|
| disabled | 是否禁用 | boolean | false |
|
|
|
|
|
| loading | 加载中的开关 | boolean | false |
|
|
|
|
|
| size | 开关大小,可选值:`default` `small` | string | `default` |
|
|
|
|
|
| unCheckedChildren | 非选中时的内容 | ReactNode | - |
|
|
|
|
|
| onChange | 变化时的回调函数 | function(checked: boolean, event: Event) | - |
|
|
|
|
|
| onClick | 点击时的回调函数 | function(checked: boolean, event: Event) | - |
|
2017-12-01 19:28:41 +08:00
|
|
|
|
|
|
|
|
|
## 方法
|
|
|
|
|
|
2022-11-13 14:07:22 +08:00
|
|
|
|
| 名称 | 描述 |
|
|
|
|
|
| ------- | -------- |
|
|
|
|
|
| blur() | 移除焦点 |
|
2019-11-22 14:37:39 +08:00
|
|
|
|
| focus() | 获取焦点 |
|
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="Switch"></ComponentTokenTable>
|
2023-09-04 14:10:39 +08:00
|
|
|
|
|
|
|
|
|
## FAQ
|
|
|
|
|
|
|
|
|
|
### 为什么在 Form.Item 下不能绑定数据?
|
|
|
|
|
|
|
|
|
|
Form.Item 默认绑定值属性到 `value` 上,而 Switch 的值属性为 `checked`。你可以通过 `valuePropName` 来修改绑定的值属性。
|
|
|
|
|
|
|
|
|
|
```tsx | pure
|
|
|
|
|
<Form.Item name="fieldA" valuePropName="checked">
|
|
|
|
|
<Switch />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
```
|