Switches allow the user to toggle an option on or off.
Switches allow the user to toggle an option on or off.
Use the checked attribute to activate the switch.
Use the disabled attribute to disable the switch.
Add descriptive help text to a switch with the help-text attribute. For help texts that contain HTML, use the *help-text slot instead.
You can listen for the ods-change event to determine when a switch is updated. You can see other switch events here . You can also see how a switch is used in forms by checking out the form section .
import '@ods/components/web.switch';Note: You only need to import a web component once in your code, as it registers itself globally when defined, allowing you to use it anywhere within your application without needing to re-import it each time you want to use the component.
This component is still in development and cannot be imported at this time.
| Name | Description |
|---|---|
| (default) | The switch's label. |
| help-text | Text that describes how to use the switch. Alternatively, you can use the `help-text` attribute. |
Please note, both DOM properties and HTML attributes are shown in the table below. Unless otherwise specified, the property and attribute names are identical.
| Property/Attribute | Reflects | Type | Default |
|---|---|---|---|
title | string | '' | |
name | string | '' | |
value | string | ||
disabled | boolean | false | |
checked | boolean | false | |
defaultChecked | boolean | false | |
form | string | '' | |
required | boolean | false | |
helpText | string | '' | |
validity | |||
validationMessage |
| React Event | Description | Event Detail |
|---|---|---|
| ods-blur | Emitted when the control loses focus. | -- |
| ods-change | Emitted when the control's checked state changes. | -- |
| ods-input | Emitted when the control receives input. | -- |
| ods-focus | Emitted when the control gains focus. | -- |
| ods-invalid | Emitted when the form control has been checked for validity and its constraints aren't satisfied. | -- |
| Name | Description | Arguments |
|---|---|---|
| click() | Simulates a click on the switch. | -- |
| focus() | Sets focus on the switch. | -- |
| blur() | Removes focus from the switch. | -- |
| checkValidity() | Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. | -- |
| getForm() | Gets the associated form, if one exists. | -- |
| reportValidity() | Checks for validity and shows the browser's validation message if the control is invalid. | -- |
| setCustomValidity() | Sets a custom validation message. Pass an empty string to restore validity. | -- |
| Name | Description |
|---|---|
| base | The component's base wrapper. |
| control | The control that houses the switch's thumb. |
| thumb | The switch's thumb. |
| label | The switch's label. |
| form-control-help-text | The help text's wrapper. |
| form-control-error-text | The error text's wrapper. |