# @uiw/react-switch

> Switch component

Latest version **4.22.3** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @uiw/react-switch
pnpm add @uiw/react-switch
yarn add @uiw/react-switch
bun add @uiw/react-switch
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.22.3 |
| Published | 2023-11-29 |
| First published | 2020-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 740 |
| Author | Kenny Wong |
| Maintainers | uiwjs, wcjiang |
| Keywords | switch, react-switch, design, uiw, uiw-react, react.js, react, react-component, component, components, ui, css, uikit, react-ui, framework, front-end, frontend |

## Links

- npm: https://www.npmjs.com/package/@uiw/react-switch
- Repository: https://github.com/uiwjs/uiw
- Homepage: https://uiwjs.github.io/#/components/switch
- Issues: https://github.com/uiwjs/uiw/issues
- Funding: https://jaywcjlove.github.io/#/sponsor
- npm.io page: https://npm.io/package/@uiw/react-switch

## Dependencies (1)

- [@uiw/react-radio](https://npm.io/package/@uiw/react-radio.md) ^4.22.3

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@progress/kendo-vue-listbox](https://npm.io/package/@progress/kendo-vue-listbox.md) — 932 weekly downloads

## Recent versions

- 4.22.3 (latest) — 2023-11-29
- 5.0.0-bate.2.1 (bate) — 2023-03-10
- 4.22.2 — 2023-11-09
- 4.22.1 — 2023-11-08
- 4.22.0 — 2023-09-19
- 4.21.28 — 2023-05-05
- 4.21.27 — 2023-04-21
- 4.21.26 — 2023-04-20
- 4.21.25 — 2023-03-14
- 4.21.24 — 2023-03-08
- 5.0.0-bate.2.0 — 2023-03-02
- 4.21.23 — 2022-12-21
- 4.21.22 — 2022-12-19
- 4.21.21 — 2022-12-19
- 5.0.0-bate-19 — 2022-12-12
- … 157 more at https://npm.io/package/@uiw/react-switch/versions

## README

Switch 开关
===

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-switch/file/README.md)
[![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-switch.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-switch)
[![npm version](https://img.shields.io/npm/v/@uiw/react-switch.svg?label=@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 (
    <div>
      <Switch checked style={{ marginRight: 10 }} />
      <Switch style={{ marginRight: 10 }} />
      <Switch data-checked="开" data-unchecked="关">电源</Switch>
    </div>
  );
}
```


### 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 (
    <Form
      onChange={({ initial, current }) => { }}
      resetOnSubmit={false}
      onSubmitError={(error) => error && error.filed ? { ...error.filed } : null}
      onSubmit={({initial, current}) => {
        console.log('switch::', initial, current)
        const ErrObj = {};
        if(Object.keys(ErrObj).length > 0) {
          const err = new Error();
          err.filed = ErrObj;
          throw err;
        }
      }}
      fields={{
        switch: {
          checked: true,
          initialValue: true,
          label: '请输入内容',
          help: '必选选项！',
          validator: value => !value ? '必填选项！' : null,
          children: <Switch style={{ maxWidth: 200 }} />,
        },
      }}
    >
      {({ fields, state, canSubmit }) => {
        return (
          <div>
            <Row>
              <Col>{fields.switch}</Col>
            </Row>
            <Row>
              <Col>
                <Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
              </Col>
            </Row>
            <Row>
              <Col>
                <pre style={{ padding: 10, marginTop: 10 }}>
                  {JSON.stringify(state.current, null, 2)}
                </pre>
              </Col>
            </Row>
          </div>
        );
      }}
    </Form>
  );
}
```

### 设置文字

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Switch
        data-checked="开"
        data-unchecked="关"
        onChange={(e) => {
          console.log('e', e.target.checked);
        }}
        style={{ marginRight: 10 }}
      />
    </div>
  );
}
```

### 禁用状态

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Switch disabled checked style={{ marginRight: 10 }} />
      <Switch disabled style={{ marginRight: 10 }} />
      <Switch disabled data-checked="开" data-unchecked="关">电源</Switch>
    </div>
  );
}
```

### 尺寸

```jsx mdx:preview&bg=#fff
import React from 'react';
import { Switch } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Switch size="large" style={{ marginRight: 10 }} data-checked="开" data-unchecked="关" />
      <Switch size="large" checked style={{ marginRight: 10 }} />
      <Switch style={{ marginRight: 10 }} />
      <Switch style={{ marginRight: 10 }} data-checked="开" data-unchecked="关" />
      <Switch size="small" data-checked="开" data-unchecked="关">电源</Switch>
    </div>
  );
}
```

### 控制组件

通过 `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 (
      <div style={{ backgroundColor: '#fff', margin: -15, padding: 15, borderRadius: '5px 5px 0 0' }}>
        <Switch onChange={this.onChange.bind(this)} checked={this.state.checked} style={{ marginRight: 10 }} />
        <Button
          size="small"
          type="primary"
          onClick={() => {
            this.setState({ checked: !this.state.checked });
          }}
        >
          点击按钮改变 Switch 状态
        </Button>
      </div>
    )
  }
}
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 |

---
_Source: https://npm.io/package/@uiw/react-switch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
