Switch
Capture boolean input from user
Source
LLM docs
Docs
PackageIcon
Usage
Controlled
Uncontrolled
Switch can be used with uncontrolled forms the same way as a native input[type="checkbox"].
Set the name attribute to include switch value in FormData object on form submission.
To control the initial checked state in uncontrolled forms, use defaultChecked prop.
Example usage of uncontrolled Switch with FormData:
States
Inner Labels
Icon labels
Thumb icon
With tooltip
Set refProp="rootRef" on Tooltip and other similar components to make them work with Switch:
Pointer cursor
By default, the switch input and label have cursor: default (same as native input[type="checkbox"]).
To change the cursor to pointer, set cursorType on the theme:
Add props to the root element
All props passed to the component are forwarded to the input element. If you need to add props to the root element, use wrapperProps. In the following example:
data-testid="wrapper"is added to the root elementdata-testid="input"is added to the input element
Switch.Group
This is anonymous
Switch.Group with uncontrolled forms
Switch.Group can be used with uncontrolled forms, it renders a hidden input
which joins all checked values into a single string using hiddenInputValuesSeparator prop.
Props for usage with uncontrolled forms:
name– name attribute passed to the hidden inputhiddenInputValuesSeparator– string used to join checked values into a single string,','by defaulthiddenInputProps– additional props passed to the hidden input
maxSelectedValues
Use maxSelectedValues prop to limit the number of selected values in Switch.Group.
When the limit is reached, the remaining switches are disabled and cannot be selected.
Switch.Group disabled
This is anonymous
Controlled Switch.Group
Change styles based on checked state
Styles API
Switch supports the Styles API; you can add styles to any inner element of the component with the classNames prop. Follow the Styles API documentation to learn more.
Component Styles API
Hover over selectors to highlight corresponding elements
Get input ref
Accessibility
Switch is a regular input[type="checkbox"]. Set aria-label if the Switch is used without the label prop: