Nested Checkboxes
UI Coding
Medium
30 min
tree
derived-state
forms
accessibility
Build a CheckboxTree for choosing permissions in an admin panel. Permissions are grouped into nested categories, and every item, group or leaf, has a checkbox.
The starter renders the nested data as plain, independent checkboxes. Make them behave as a tree.
Requirements
- Downward: checking or unchecking a group applies the same value to all of its descendants.
- Upward: a group's checkbox is derived from its children:
- checked if every descendant leaf is checked,
- unchecked if none are,
- indeterminate (the "–" state) if only some are.
- Clicking an indeterminate group checks everything below it.
- Show a summary below the tree: "N of M permissions selected" (counting leaves only).
- Use native
<input type="checkbox">elements with proper<label>s. The indeterminate state can only be set from JavaScript (input.indeterminate = true); there is no HTML attribute for it. - The data can be any depth.
Follow-up
- Add a "Select all" master checkbox. Do you need any new state?
- Some permissions are disabled (locked by the organisation). How does that change the group logic?
- The tree has 10,000 leaves. Where is the cost, and how do you avoid it?
Spotted a mistake or a rights concern? Report an issue