<wa-checkbox>
Checkboxes allow the user to toggle an option on or off.
<wa-checkbox>Checkbox</wa-checkbox>
This component works with standard <form>
elements. Please refer to the section on
form controls to learn more about form submission and client-side
validation.
Use the checked
attribute to activate the checkbox.
<wa-checkbox checked>Checked</wa-checkbox>
Use the indeterminate
attribute to make the checkbox indeterminate.
<wa-checkbox indeterminate>Indeterminate</wa-checkbox>
Use the disabled
attribute to disable the checkbox.
<wa-checkbox disabled>Disabled</wa-checkbox>
Use the size
attribute to change a checkbox's size.
<wa-checkbox size="small">Small</wa-checkbox> <br /> <wa-checkbox size="medium">Medium</wa-checkbox> <br /> <wa-checkbox size="large">Large</wa-checkbox>
Add descriptive help text to a switch with the help-text
attribute. For help texts that contain
HTML, use the help-text
slot instead.
<wa-checkbox help-text="What should the user know about the checkbox?">Label</wa-checkbox>
Use the setCustomValidity()
method to set a custom validation message. This will prevent the form
from submitting and make the browser display the error message you provide. To clear the error, call this
function with an empty string.
<form class="custom-validity"> <wa-checkbox>Check me</wa-checkbox> <br /> <wa-button type="submit" variant="brand" style="margin-top: 1rem;">Submit</wa-button> </form> <script> const form = document.querySelector('.custom-validity'); const checkbox = form.querySelector('wa-checkbox'); const errorMessage = `Don't forget to check me!`; // Set initial validity as soon as the element is defined customElements.whenDefined('wa-checkbox').then(async () => { await checkbox.updateComplete; checkbox.setCustomValidity(errorMessage); }); // Update validity on change checkbox.addEventListener('wa-change', () => { checkbox.setCustomValidity(checkbox.checked ? '' : errorMessage); }); // Handle submit customElements.whenDefined('wa-checkbox').then(() => { form.addEventListener('submit', event => { event.preventDefault(); alert('All fields are valid!'); }); }); </script>
Name | Description |
---|---|
(default) | The checkbox's label. |
help-text
|
Text that describes how to use the checkbox. Alternatively, you can use the
help-text attribute.
|
Name | Description | Reflects | |
---|---|---|---|
name name
|
The name of the checkbox, submitted as a name/value pair with form data.
Type
string
Default
''
|
|
|
value value
|
The value of the checkbox, submitted as a name/value pair with form data.
|
|
|
size size
|
The checkbox's size.
Type
'small' | 'medium' | 'large'
Default
'medium'
|
|
|
disabled disabled
|
Disables the checkbox.
Type
boolean
Default
false
|
||
indeterminate indeterminate
|
Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents
a "select all/none" behavior when associated checkboxes have a mix of checked and
unchecked states.
Type
boolean
Default
false
|
|
|
checked |
Draws the checkbox in a checked state.
|
||
defaultChecked checked
|
The default value of the form control. Primarily used for resetting the form control.
|
|
|
form form
|
By default, form controls are associated with the nearest containing
<form> element. This attribute allows you to place the form control outside of a
form and associate it with the form that has this id . The form must be in the same
document or shadow root for this to work.
Type
null
Default
null
|
|
|
required required
|
Makes the checkbox a required field.
Type
boolean
Default
false
|
|
|
helpText help-text
|
The checkbox's help text. If you need to display HTML, use the
help-text slot instead.
Type
string
Default
''
|
Name | Description | Arguments |
---|---|---|
click() |
Simulates a click on the checkbox. | |
focus() |
Sets focus on the checkbox. |
options: FocusOptions
|
blur() |
Removes focus from the checkbox. |
Name | Description |
---|---|
wa-blur |
Emitted when the checkbox loses focus. |
wa-change |
Emitted when the checked state changes. |
wa-focus |
Emitted when the checkbox gains focus. |
wa-input |
Emitted when the checkbox receives input. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
Name | Description |
---|---|
--background-color |
The checkbox's background color.
|
--background-color-checked |
The checkbox's background color when checked.
|
--border-color |
The color of the checkbox's borders.
|
--border-color-checked |
The color of the checkbox's borders when checked.
|
--border-radius |
The radius of the checkbox's corners.
|
--border-style |
The style of the checkbox's borders.
|
--border-width |
The width of the checkbox's borders. Expects a single value.
|
--box-shadow |
The shadow effects around the edges of the checkbox.
|
--toggle-size |
The size of the checkbox.
|
Name | Description |
---|---|
base |
The component's base wrapper. |
control |
The square container that wraps the checkbox's checked state. |
control--checked |
Matches the control part when the checkbox is checked. |
control--indeterminate |
Matches the control part when the checkbox is indeterminate. |
checked-icon |
The checked icon, a <wa-icon> element. |
indeterminate-icon |
The indeterminate icon, a <wa-icon> element. |
label |
The container that wraps the checkbox's label. |
form-control-help-text |
The help text's wrapper. |
This component automatically imports the following elements. Subdependencies, if any exist, will also be included in this list.
The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
To manually import this component from the CDN, use the following code.
import 'https://early.webawesome.com/webawesome@3.0.0-alpha.4/dist/components/checkbox/checkbox.js';