Switch
Switch
component allows users to create a switch to represent boolean states.Basic usage
Switch
using its component state.tsxCopied1<Switch id="switch" label="Click me!" />2<Text>Checked: {String(switchState.checked)}</Text>
Disabling
Switch
can be disabled using the defaultDisabled
prop.tsxCopied1<Switch label="Disabled switch" defaultDisabled defaultChecked />
Inner labels
Switch
can be added through the onLabel
and offLabel
props.tsxCopied1<Switch label="Switch with inner labels" onLabel="ON" offLabel="OFF" size="lg" />
Setting values
setChecked
and setDisabled
functions on the state.tsxCopied1<Button onClick={() => switchState.setChecked(!switchState.checked)}>Toggle checked</Button>2<Button onClick={() => switchState.setDisabled(!switchState.disabled)}>3Toggle disabled4</Button>5<Switch id="switch" label="You can control this switch using the above buttons" />
Component API
Whether the switch is checked when using as a controlled component. Prefer using defaultChecked and the global component state.
Color of the switch.
Whether the switch is initially checked.
Whether the switch is initially disabled.
Description displayed below the switch. Can be a string or a React component.
Displays error message after the switch input. Can be a string or a React component.
The ID referenced by the global component state.
Switch label. Can be a string or a React component.
Inner label when the switch is unchecked. Can be a string or a React component.
Callback when checked state changes when using as a controlled component. Prefer using the global component state.
Inner label when the switch is checked. Can be a string or a React component.
Adds red asterisk on the right side of label and sets required on input element
Size of the switch.
A single function or an array of functions that validate the input value.
State API
Whether the switch is checked
Whether the switch is disabled
Sets the value of the switch
Sets the disabled value of the switch. If the disabled
value is not provided, the switch will be disabled