Switch 开关
===
[](https://jaywcjlove.github.io/#/sponsor)
[](https://t.me/s/AwesomeMacApps)
[](https://x.com/jaywcjlove)
[](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-switch/file/README.md)
[](https://www.npmjs.com/package/@uiw/react-switch)
[](https://npmjs.com/@uiw/react-switch)
表示两种相互对立的状态间的切换,多用于触发「开/关」。选中时的内容支持响应式。
```jsx
import { Switch } from 'uiw';
// or
import Switch from '@uiw/react-switch';
```
### 基本用法
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';
export default function Demo() {
return (
电源
);
}
```
### Form 中使用 Switch
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Form, Switch, Row, Col, Button, Notify } from 'uiw';
export default function Demo() {
return (
);
}
```
### 设置文字
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';
export default function Demo() {
return (
{
console.log('e', e.target.checked);
}}
style={{ marginRight: 10 }}
/>
);
}
```
### 禁用状态
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';
export default function Demo() {
return (
电源
);
}
```
### 尺寸
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';
export default function Demo() {
return (
电源
);
}
```
### 控制组件
通过 `checked` 属性改变 `Switch` 组件状态。
```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch, Button } from 'uiw';
class Demo extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: true
}
}
onChange(e) {
console.log('~~~:::');
this.setState({ checked: e.target.checked });
}
render() {
return (
)
}
}
export default Demo;
```
## Switch
| 参数 | 说明 | 类型 | 默认值 |
|------ |-------- |---------- |-------- |
| value | 根据 value 进行比较,判断是否选中 | String/Number/Boolean | - |
| name | 用于表单对应的名称 | String | - |
| checked | 指定当前是否选中 | boolean | false |
| disabled | 是否禁用 | boolean | false |
| onChange | 变化时回调函数 | Function(e:Event) | - |
| data-checked | 选中时的内容 | string/ReactNode | - |
| data-unchecked | 非选中时的内容 | string/ReactNode | - |
| size | 开关大小,可选值:`large` `default` `small` | string | default |