Switch开关

开关选择器。

何时使用#

  • 需要表示开关状态/两种状态之间的切换时;

  • checkbox 的区别是,切换 switch 会直接触发状态改变,而 checkbox 一般用于状态标记,需要和提交操作配合。

代码演示




Form

最简单的用法。

expand codeexpand code
import {
  Form,
  Checkbox,
  Switch,
} from 'antd';
import React from 'react';

const onChange = (checked: boolean) => {
  console.log(`switch to ${checked}`);
};

const App: React.FC = () => {
  const [checked, setChecked] = React.useState(false)
  const [form] = Form.useForm()
  const userName = Form.useWatch('username', form);
  const fav = Form.useWatch('fav', form);
  console.log(form.getFieldsValue())
  const initialValues = {
    username: true,
    fav: ['A', "C"]
  }
  return (
    <div>
      <Switch defaultChecked onChange={onChange} />
      <Switch
        size="small"
        checked={checked}
        onChange={() => setChecked(!checked)}
      >
        文字说明
      </Switch>
      <br />
      <hr />
      <br />
      <h1>Form</h1>
      <Form
        layout="horizontal"
        form={form}
        initialValues={initialValues}
        onValuesChange={console.log}
      >
        <Form.Item label="Checkbox" valuePropName="checked" name="username">
          <Checkbox>Checkbox</Checkbox>
        </Form.Item>
        <Form.Item label="Switch" valuePropName="checked" name="username">
          <Switch>Switch</Switch>
        </Form.Item>
        <Form.Item label="Checkbox Group" name="fav">
          <Checkbox.Group>
            <Checkbox value="A">A</Checkbox>
            <Checkbox value="B">B</Checkbox>
            <Checkbox value="C">C</Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="Switch" name="fav">
          <Switch.Group>
            <Switch value="A">A</Switch>
            <Switch value="B">B</Switch>
            <Switch value="C">C</Switch>
          </Switch.Group>
        </Form.Item>
        <Form.Item label="Switch" name="fav">
          <Switch.Group layout="vertical">
            <Switch value="A">A</Switch>
            <Switch value="B">B</Switch>
            <Switch value="C">C</Switch>
          </Switch.Group>
        </Form.Item>
      </Form>
    </div>
  )
}

export default App;


带有文字和图标。

expand codeexpand code
import { CheckOutlined, CloseOutlined } from '@ant-design/icons';
import { Switch } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Switch checkedChildren="开启" unCheckedChildren="关闭" defaultChecked />
    <br />
    <Switch checkedChildren="1" unCheckedChildren="0" />
    <br />
    <Switch
      checkedChildren={<CheckOutlined />}
      unCheckedChildren={<CloseOutlined />}
      defaultChecked
    />
  </>
);

export default App;

标识开关操作仍在执行中。

expand codeexpand code
import { Switch } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Switch loading defaultChecked />
    <br />
    <Switch size="small" loading />
  </>
);

export default App;

Switch 失效状态。

expand codeexpand code
import { Button, Switch } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [disabled, setDisabled] = useState(true);

  const toggle = () => {
    setDisabled(!disabled);
  };

  return (
    <>
      <Switch disabled={disabled} defaultChecked />
      <br />
      <Button type="primary" onClick={toggle}>
        Toggle disabled
      </Button>
    </>
  );
};

export default App;

size="small" 表示小号开关。

expand codeexpand code
import { Switch } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Switch defaultChecked />
    <br />
    <Switch size="small" defaultChecked />
  </>
);

export default App;

API#

参数说明类型默认值
autoFocus组件自动获取焦点booleanfalse
checked指定当前是否选中booleanfalse
checkedChildren选中时的内容ReactNode-
classNameSwitch 器类名string-
defaultChecked初始是否选中booleanfalse
disabled是否禁用booleanfalse
loading加载中的开关booleanfalse
size开关大小,可选值:default smallstringdefault
unCheckedChildren非选中时的内容ReactNode-
onChange变化时的回调函数function(checked: boolean, event: Event)-
onClick点击时的回调函数function(checked: boolean, event: Event)-

方法#

名称描述
blur()移除焦点
focus()获取焦点
Slider滑动输入条TimePicker时间选择框