Back to Ant Design

Switch

components/switch/index.en-US.md

6.3.72.8 KB
Original Source

When To Use

  • If you need to represent the switching between two states or on-off state.
  • The difference between Switch and Checkbox is that Switch will trigger a state change directly when you toggle it, while Checkbox is generally used for state marking, which should work in conjunction with submit operation.

Examples

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">Basic</code> <code src="./demo/disabled.tsx">Disabled</code> <code src="./demo/text.tsx">Text & icon</code> <code src="./demo/size.tsx">Two sizes</code> <code src="./demo/loading.tsx">Loading</code> <code src="./demo/component-token.tsx" debug>Custom component token</code> <code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code>

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersion
checkedDetermine whether the Switch is checkedbooleanfalse
checkedChildrenThe content to be shown when the state is checkedReactNode-
classNameThe additional class to Switchstring-
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
defaultCheckedWhether to set the initial statebooleanfalse
defaultValueAlias for defaultCheckedboolean-5.12.0
disabledDisable switchbooleanfalse
loadingLoading state of switchbooleanfalse
sizeThe size of the Switch, options: medium smallstringmedium
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
unCheckedChildrenThe content to be shown when the state is uncheckedReactNode-
valueAlias for checkedboolean-5.12.0
onChangeTrigger when the checked state is changingfunction(checked: boolean, event: Event)-
onClickTrigger when clickedfunction(checked: boolean, event: Event)-

Methods

NameDescription
blur()Remove focus
focus()Get focus

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Switch"></ComponentTokenTable>

FAQ

Why not work in Form.Item? {#faq-binding-data}

Form.Item default bind value to value property, but Switch value property is checked. You can use valuePropName to change bind property.

tsx
<Form.Item name="fieldA" valuePropName="checked">
  <Switch />
</Form.Item>