Switch
Similar to Checkboxes, you can use Switches to indicate or control boolean values.
To add the Switch
component to your app:
import {Switch} from '@stripe/ui-extension-sdk/ui';
A common use of Switches is for settings that you save immediately—in other words, Switch
is rarely part of a larger form that needs to be submitted separately.
Here’s a simple example of a Switch
.
<Switch label="This is a Switch." onChange={(e) => { console.log(e.target.checked); }} />
Switch props
Property | Type |
---|---|
| Optional
If |
| Optional
Controls whether the input is selected. When you pass this prop, you must also pass an |
| Optional
Specifies the initial value that a user can change. |
| Optional
Descriptive text that will be rendered adjacent to the control’s label. |
| Optional
Sets whether or not the element should be disabled. Prevents selection. |
| Optional
Error text that will be rendered below the control. |
| Optional
Specifies the |
| Optional
Visually hides the specified elements. The hidden elements will still be present and visible to screen readers. |
| Optional
Sets whether or not the element is in an invalid state. This is a display-only prop, and will not prevent form submission. |
| Optional
Text that describes the control. Will be both visible and clickable. |
| Optional
Specifies the name for this input that’s submitted with the form. |
| Optional
Required for controlled inputs. Fires immediately when the input’s value is changed by the user (for example, it fires on every keystroke). Behaves like the browser input event. |
| Optional
If |
| Optional
If |
| Optional
Overrides the default Tab button behavior. Avoid using values other than |
| Optional
Controls the input’s text. When you pass this prop, you must also pass an |
State management
Use the Switch
component as an uncontrolled input:
<Switch onChange={(e) => { console.log(e.target.checked); }} defaultChecked label="This Switch is uncontrolled." />
Disabled
You can disable a Switch
component, which prevents changes.
<Switch label="This Switch is disabled." defaultChecked disabled />