Checkbox Group
<sl-checkbox-group> | SlCheckboxGroup
Checkbox groups wrap multiple checkboxes so they behave as a single form control.
<sl-checkbox-group label="Select toppings" help-text="Pick as many as you like"> <sl-checkbox value="olives">Olives</sl-checkbox> <sl-checkbox value="mushrooms">Mushrooms</sl-checkbox> <sl-checkbox value="onions">Onions</sl-checkbox> </sl-checkbox-group>
import SlCheckboxGroup from '@yoummday/ymmd-shoelace/dist/react/checkbox-group'; import SlCheckbox from '@yoummday/ymmd-shoelace/dist/react/checkbox'; const App = () => ( <SlCheckboxGroup label="Select toppings" help-text="Pick as many as you like"> <SlCheckbox value="olives">Olives</SlCheckbox> <SlCheckbox value="mushrooms">Mushrooms</SlCheckbox> <SlCheckbox value="onions">Onions</SlCheckbox> </SlCheckboxGroup> );
Checkbox groups work with native <form> elements. Visit
form controls for details about submission and validation.
Examples
Required
Set required to ensure the user selects at least one option.
<sl-checkbox-group label="Notification channels" required> <sl-checkbox value="email">Email</sl-checkbox> <sl-checkbox value="sms">SMS</sl-checkbox> <sl-checkbox value="push">Push</sl-checkbox> </sl-checkbox-group>
import SlCheckboxGroup from '@yoummday/ymmd-shoelace/dist/react/checkbox-group'; import SlCheckbox from '@yoummday/ymmd-shoelace/dist/react/checkbox'; const App = () => ( <SlCheckboxGroup label="Notification channels" required> <SlCheckbox value="email">Email</SlCheckbox> <SlCheckbox value="sms">SMS</SlCheckbox> <SlCheckbox value="push">Push</SlCheckbox> </SlCheckboxGroup> );
Default Selection
Add checked to individual checkboxes or assign the group’s value property
programmatically to set the initial state.
<sl-checkbox-group label="Favorite pets"> <sl-checkbox value="dogs" checked>Dogs</sl-checkbox> <sl-checkbox value="cats" checked>Cats</sl-checkbox> <sl-checkbox value="birds">Birds</sl-checkbox> </sl-checkbox-group>
import SlCheckboxGroup from '@yoummday/ymmd-shoelace/dist/react/checkbox-group'; import SlCheckbox from '@yoummday/ymmd-shoelace/dist/react/checkbox'; const App = () => ( <SlCheckboxGroup label="Favorite pets" value={['dogs', 'cats']}> <SlCheckbox value="dogs">Dogs</SlCheckbox> <SlCheckbox value="cats">Cats</SlCheckbox> <SlCheckbox value="birds">Birds</SlCheckbox> </SlCheckboxGroup> );
Sizes
Change size on the group to update every checkbox.
<sl-checkbox-group size="small" label="Breakfast"> <sl-checkbox value="eggs">Eggs</sl-checkbox> <sl-checkbox value="toast">Toast</sl-checkbox> </sl-checkbox-group> <sl-checkbox-group size="large" label="Dinner"> <sl-checkbox value="pasta">Pasta</sl-checkbox> <sl-checkbox value="salad">Salad</sl-checkbox> </sl-checkbox-group>
Alignment
Use the alignment attribute to lay out checkboxes horizontally.
<sl-checkbox-group alignment="horizontal" label="Payment methods"> <sl-checkbox value="visa">Visa</sl-checkbox> <sl-checkbox value="mastercard">Mastercard</sl-checkbox> <sl-checkbox value="amex">Amex</sl-checkbox> </sl-checkbox-group>
Custom Validity
Use setCustomValidity() to show a custom error.
<form class="checkbox-group-validity"> <sl-checkbox-group label="Agree to policies"> <sl-checkbox value="terms">Terms of Service</sl-checkbox> <sl-checkbox value="privacy">Privacy Policy</sl-checkbox> </sl-checkbox-group> <sl-button type="submit" variant="primary" style="margin-top: 1rem;">Submit</sl-button> </form> <script> const form = document.querySelector('.checkbox-group-validity'); const group = form.querySelector('sl-checkbox-group'); const message = 'Please agree to both policies.'; customElements.whenDefined('sl-checkbox-group').then(async () => { await group.updateComplete; group.setCustomValidity(message); }); group.addEventListener('sl-change', () => { group.setCustomValidity(group.value.length === 2 ? '' : message); }); form.addEventListener('submit', event => { event.preventDefault(); alert('Thanks!'); }); </script>
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://v2-54-0.yoummday-theme.pages.dev/components/checkbox-group/checkbox-group.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://v2-54-0.yoummday-theme.pages.dev/components/checkbox-group/checkbox-group.js';
To import this component using a bundler:
import '@yoummday/ymmd-shoelace/dist/components/checkbox-group/checkbox-group.js';
To import this component as a React component:
import SlCheckboxGroup from '@yoummday/ymmd-shoelace/dist/react/checkbox-group';
Slots
| Name | Description |
|---|---|
| (default) | One or more <sl-checkbox> elements. |
label
|
The checkbox group’s label. |
help-text
|
Help text for the group. |
Learn more about using slots.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
label
|
The checkbox group’s label. |
string
|
''
|
|
helpText
help-text
|
Help text shown below the control. |
string
|
''
|
|
name
|
Name used when submitting form data. |
string
|
'selection'
|
|
value
|
Selected values. |
string[]
|
[]
|
|
size
|
The checkbox group’s size. |
|
'small' | 'medium' | 'large'
|
'medium'
|
form
|
Associates the control with a form by id. |
|
string
|
''
|
alignment
|
Lays out checkboxes vertically (default) or horizontally. |
|
'vertical' | 'horizontal'
|
'vertical'
|
required
|
Makes selection required. |
|
boolean
|
false
|
disabled
|
Disables the group. |
|
boolean
|
false
|
validity
|
Gets the validity state. | - | - | |
validationMessage
|
Gets the validation message. | - | - | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
| Name | React Event | Description | Event Detail |
|---|---|---|---|
sl-change |
onSlChange |
Emitted when the selection changes. | - |
sl-input |
onSlInput |
Emitted when the group receives user input. | - |
sl-invalid |
onSlInvalid |
Emitted when the control fails validation. | - |
Learn more about events.
Methods
| Name | Description | Arguments |
|---|---|---|
checkValidity() |
Checks for validity without showing a message. | - |
getForm() |
Gets the associated form, if any. | - |
reportValidity() |
Shows the browser’s validation message when invalid. | - |
setCustomValidity() |
Sets a custom validation message. |
message:
|
focus() |
Focuses the first checked or enabled checkbox. |
options: FocusOptions
|
Learn more about methods.
Parts
| Name | Description |
|---|---|
form-control |
The form control that wraps the label, input, and help text. |
form-control-label |
The label’s wrapper. |
form-control-input |
The input’s wrapper. |
form-control-help-text |
The help text’s wrapper. |
items |
The wrapper that arranges the checkboxes. |
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<sl-checkbox><sl-icon>