Components
Checkbox Group
Provides shared state to a series of checkboxes.
Usage guidelines#
- Form controls must have an accessible name. It can be created using
<label>elements, or the Field and Fieldset components. See Labeling a checkbox group.
Anatomy#
Checkbox Group is composed together with Checkbox. Import the components and place them together:
import { Checkbox } from '@egoist/solid-base/checkbox';
import { CheckboxGroup } from '@egoist/solid-base/checkbox-group';
<CheckboxGroup>
<Checkbox.Root />
</CheckboxGroup>;Examples#
Labeling a checkbox group#
Label the group with aria-labelledby and a sibling label element:
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">{/* ... */}</CheckboxGroup>An enclosing <label> is the simplest labeling pattern for each checkbox:
<label>
<Checkbox.Root value="http" />
HTTP
</label>Rendering as a native button#
By default, <Checkbox.Root> renders a <span> element to support enclosing labels. Prefer
rendering each checkbox as a native button when using sibling labels (for/id).
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
<div>
<label for="protocol-http">HTTP</label>
<Checkbox.Root id="protocol-http" value="http" nativeButton render="button">
<Checkbox.Indicator />
</Checkbox.Root>
</div>
</CheckboxGroup>Native buttons with wrapping labels are supported by using the render callback to avoid invalid
HTML, so the hidden input is placed outside the label:
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
<Checkbox.Root
value="http"
nativeButton
render={(buttonProps) => (
<label>
<button {...buttonProps} />
HTTP
</label>
)}
/>
</CheckboxGroup>Parent checkbox#
A checkbox that controls other checkboxes within a <CheckboxGroup> can be created:
- Make
<CheckboxGroup>a controlled component - Pass an array of all the child checkbox values to the
allValuesprop on the<CheckboxGroup>component - Add the
parentboolean prop to the parent<Checkbox.Root>
The group controls the parent checkbox's indeterminate state when some, but not all, child checkboxes are checked.
Nested parent checkbox#
Groups can be nested. Keep both groups controlled and sync them in their onValueChange
handlers; the outer parent checkbox receives an explicit indeterminate prop when only some of
the nested permissions are checked.
API reference#
<CheckboxGroup>
Provides a shared state to a series of checkboxes.
idstring—The id of the group element.
valuestring[]—Names of the checkboxes in the group that should be ticked.
To render an uncontrolled checkbox group, use the defaultValue prop instead.
defaultValuestring[]—Names of the checkboxes in the group that should be initially ticked.
To render a controlled checkbox group, use the value prop instead.
onValueChange| ((value: string[], eventDetails: CheckboxGroupChangeEventDetails) => void)—Event handler called when a checkbox in the group is ticked or unticked. Provides the new value as an argument.
allValuesstring[]—Names of all checkboxes in the group. Use this when creating a parent checkbox.
disabledbooleanfalseWhether the component should ignore user interaction.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the checkbox group is disabled.