API: components
CheckboxGroup
Checkboxes sharing one array value, with an optional parent checkbox that checks or clears them all, in each framework.
A group of checkboxes sharing one array value: the values of the checked
ones. A parent checkbox (parent) checks every value of allValues, clears them all, and shows
the mixed state in between (the APG checkbox pattern,
mixed). CheckboxGroup is one component, not a namespace; its checkboxes come from
/checkbox.
Behaviour
- Children: a checkbox's state is whether the group's value holds its
value; toggling it adds or removes it. The value keeps the order ofallValues, else the order they were checked. - Parent: checked when every value of
allValuesis, unchecked when none is, mixed otherwise. Toggling it from unchecked or mixed checks all; from checked, clears all. It submits nothing. - Forms: each child submits its own
name=value; a form reset returns an uncontrolled group todefaultValue. - Name: the group is a
role="group"; name it witharia-labelledby(a heading) oraria-label, or put it in a Field, whose Label names it. - In a Field: the group is the field's control. The Field's Label names
it (
aria-labelledby), its Description and Errors describe it (aria-describedby),aria-invalidand the field's state attributes are on the group, andvalidatereceives the array of checked values. Its checkboxes take the field'sname(each still submits its ownvalue; a parent submits nothing) anddisabled, and do not register as the field's control themselves. The browser validates no element for it: a Form blocks a submission with an invalid group, and focuses its first enabled checkbox.
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| CheckboxGroup | div | checkbox-group-root | the group; holds the array value |
Usage
import { Checkbox } from "@fragiola/primitives-react/checkbox";
import { CheckboxGroup } from "@fragiola/primitives-react/checkbox-group";
<CheckboxGroup aria-labelledby="toppings" defaultValue={["cheese"]} allValues={["cheese", "ham"]}>
<h2 id="toppings">Toppings</h2>
<label>
<Checkbox.Root parent />
All toppings
</label>
<label>
<Checkbox.Root name="toppings" value="cheese" />
Cheese
</label>
<label>
<Checkbox.Root name="toppings" value="ham" />
Ham
</label>
</CheckboxGroup>;In a Field
<Form>
<Field.Root name="toppings" validate={(value) => ((value as string[]).length === 0 ? "Choose a topping." : null)}>
<Field.Label>Toppings</Field.Label>
<CheckboxGroup>
<label><Checkbox.Root value="cheese" /> Cheese</label>
<label><Checkbox.Root value="ham" /> Ham</label>
</CheckboxGroup>
<Field.Description>Pick at least one.</Field.Description>
<Field.Error />
</Field.Root>
<button type="submit">Order</button>
</Form>The checkboxes have no name of their own: the field gives it, and the form submits
toppings=ham. The Field's Label is a label with no labelable control here; it names the group
through aria-labelledby.
Props
It also takes what the primitive contract gives: render
(Vue: as and the render slot), a class and a style, a ref, and any attribute, which it
forwards. Vue binds them as v-model:value and @value-change; Angular as [(value)] and
(valueChangeDetails).
CheckboxGroup
| prop | type | default | description |
|---|---|---|---|
value | string[] | the checked values, controlled | |
defaultValue | string[] | [] | the initial values, uncontrolled |
onValueChange | (value: string[], details: { reason }) => void | every change (reason none) | |
allValues | string[] | [] | every child value: what the parent checks |
disabled | boolean | false | disables every child |
State and part attributes
| part | attributes |
|---|---|
| CheckboxGroup | role="group", data-disabled; in a Field, aria-labelledby (the Field's Label), aria-describedby, aria-invalid and the field's states |
The parent checkbox gets aria-controls (its children's ids) and aria-checked="mixed" in
between.
Keyboard
Each checkbox is a tab stop; Space toggles the focused one.
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/checkbox-group | ChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupProps, CheckboxGroupState, RenderProp |
@fragiola/primitives-vue/checkbox-group | ChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupEmits, CheckboxGroupProps, CheckboxGroupState, RenderSlotProps |
@fragiola/primitives-angular/checkbox-group | ChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupState, ValueChangeEvent |