checklist
Three condiments and a select-all that goes mixed.
import { checklist } from '@markless/ui';
export default function Condiments() @{
<checklist.root values={['lettuce', 'tomato']}>
<checklist.label>Sandwich condiments</checklist.label>
<checklist.selectall>
<checklist.selectallindicator>Checked</checklist.selectallindicator>
</checklist.selectall>
<checklist.item value="lettuce">
<checklist.itemtrigger>
<checklist.itemindicator>Checked</checklist.itemindicator>
</checklist.itemtrigger>
<checklist.itemlabel>Lettuce</checklist.itemlabel>
</checklist.item>
<checklist.item value="tomato">
<checklist.itemtrigger>
<checklist.itemindicator>Checked</checklist.itemindicator>
</checklist.itemtrigger>
<checklist.itemlabel>Tomato</checklist.itemlabel>
</checklist.item>
</checklist.root>
}A checklist is a group of checkboxes with one select-all box on top. Use it when a person picks any number of options from one short list. For a single box, use checkbox.
You give the root two lists. values is every option the list offers. value is the options that
are ticked. Each checklist.item names its option with its own value.
How the select-all works
You never set the select-all's state. The family works it out from your two lists:
- Nothing ticked: the select-all is off.
- Every option in
valuesticked: the select-all is on. - Anything in between: the select-all is mixed.
The test is a count. If value holds as many entries as values, the select-all is on. So keep
values in step with the items you render.
A click on the select-all from mixed or off ticks everything. A click from on clears everything.
Either way onChange gets the whole new list of ticked values.
Anatomy
The group and the select-all's own checkbox root are one element: role="group" plus the checkbox root's own behavior.
The root and the select-all share one checkbox. The root is that checkbox's root, with
role="group", and checklist.selectall is its trigger. checklist.label names the group by
labelling that trigger.
Each checklist.item is a checkbox of its own, with its own trigger, label, description and
indicator. Items carry the checkbox keyboard rules: each box is a Tab
stop, and Space toggles it.
Forms and errors
Put a checklist.field inside an item to send that item with a form. The field submits the item's
name and value. An item that is ticked and disabled sends nothing.
checklist.error placed directly in the root marks the select-all invalid. Inside an item, it marks
only that item.
Styling
Every box is a checkbox, so the family writes the checkbox attributes:
| Attribute | On | When |
|---|---|---|
ui-checked |
root, selectall, item, itemtrigger |
That box is on. |
ui-mixed |
root, selectall |
Some items are ticked, not all. |
ui-disabled |
root, selectall, item, itemtrigger |
That box is disabled. |
Props on checklist.root
| Prop | Type | What it does |
|---|---|---|
value |
readonly string[] |
The ticked options. The default is an empty list. |
values |
readonly string[] |
Every option the list offers. The select-all compares against it. |
disabled |
boolean |
Nobody can change any box, the select-all included. |
onChange |
(value: readonly string[]) => void |
Called with the whole new ticked list. |
Every other prop goes on the root <div>. checklist.item takes value, which it needs,
plus disabled and name.