shadcn.io is not affiliated with official shadcn/ui
Checkbox examples · Standard
Nested List Checkbox for React
A select-all checkbox over three indented feature checkboxes, checked, cleared or indeterminate to match them.
A select-all needs to show what it's selecting. Here Select all features sits over three indented features: checking it checks all three, clearing it clears them, and when only some are checked it shows the indeterminate state. The parent isn't stored: it's computed from the three children on every render, so the two can never disagree. The children sit in their own FieldGroup with ms-6, and the parent label is font-semibold. Use it for feature bundles, permission groups and other options with a parent.
Preview
Installation
FAQ
Should the parent checkbox show indeterminate state?
Yes. When some but not all children are checked, the parent is indeterminate rather than checked or empty, so people see a partial selection without counting.
The parent gets checked when all children are on and indeterminate when some are.
What should happen when clicking the parent checkbox?
Every child takes the parent's new value: checking it selects all, clearing it clears all. Clicking an indeterminate parent checks it, which completes the partial selection. The parent's change handler sets the three children, and the parent follows from them.
Should clicking children affect parent state?
Yes, but not by setting it. The parent's state is derived from the children each render: all checked, none, or some. Ticking a child changes the children, and the parent updates with them.
Can I nest checkboxes more than two levels deep?
You can repeat the pattern, with each parent derived from its own children, but past two or three levels it gets hard to follow. For deep hierarchies, use a Tree View with checkboxes and collapsible branches, or flatten the list into headed groups.
How do I visually indicate nesting levels?
Indent each level: the children here are a FieldGroup with ms-6, a logical margin that flips in right-to-left pages. A heavier parent label helps, and border-s ps-4 on the child group adds a guide line.
Are nested checkbox lists accessible?
Every checkbox has its own label, Tab moves from the parent through the children, and Space toggles. Screen readers announce the indeterminate parent as mixed or partially checked. The indent is only visual, so a parent label that says what it controls (Select all features) carries the relationship.
Related
- Indeterminate CheckboxA checkbox that starts indeterminate, the
- Vertical List CheckboxA stacked list of programming language
- Todo Style CheckboxA to-do item whose label is struck
- Basic Data Table with CheckboxesA table of products with a checkbox on
- Nested FieldsA shipping address fieldset with a nested
- With Description CheckboxA checkbox with a label and a line of