CheckboxGroup unstable
Shares one list of ticked values among the checkboxes inside it, and lets a
parent checkbox tick or untick all of them. Pass the ticked values as
value and store the list Updated reports.
CheckboxGroup is a headless component: the checkboxes in its view
read the value from it and report their toggles to it, and it remembers
the selection a parent checkbox brings back.
Anatomy
| Part | Element |
|---|---|
root |
any element, usually a <div> |
The root takes the consumer's builder and returns it, so the consumer goes
on chaining and adds the checkboxes with .children(...):
checkboxGroup.root(h.div).children(...). It adds no markup.
A Checkbox with a value prop inside the group is a member: it is
ticked while the group's value holds its value, and a click adds or
removes it. A Checkbox with parent: true is the group's parent, given
allValues: it is ticked while every value is, indeterminate while some
are, and points at the members with aria-controls.
Name the group with aria-labelledby on the root, or draw it as a field's
group: checkboxGroup.root(field.group(h.div)) with field.legend as the name and
field.member on each checkbox's root.
Data attributes
On root, present with an empty value or absent:
data-disabled
Keyboard
CheckboxGroup adds no key handler. Each checkbox keeps its own: Space
toggles it and Tab moves between them.
Parent checkbox
A click on the parent cycles through the selections. From a mix of ticked and unticked members it ticks them all, from all it unticks them all, and from none it brings back the last mix, or ticks them all when there was none. A disabled member keeps its state throughout: a ticked one stays ticked, and an unticked one stays unticked.
Forms
Each ticked member submits its value under its own name, or the
group's name when it has none. The parent submits nothing, and an
unticked member submits nothing.
Example Fruit with a parent checkbox
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Checkbox, CheckboxGroup } from "effect/ui/kit"import { Effect, Schema } from "effect"
const fruits = ["apple", "pear", "plum"]
const BasketMessage = UiSchema.TaggedEnum({ UpdatedFruits: { value: Schema.Array(Schema.String) } })
const Basket = Html.component({ name: "Basket", model: Schema.Struct({ fruits: Schema.Array(Schema.String) }), message: BasketMessage}).make({ init: () => ({ fruits: ["apple"] }), update: { UpdatedFruits: ($, { value }) => $.evolve({ fruits: value }) }, view: (h, model) => CheckboxGroup(h, { props: { value: model.fruits, allValues: fruits, name: "fruit" }, onEmit: { Updated: ({ value }) => BasketMessage.UpdatedFruits({ value }) }, view: (checkboxGroup) => checkboxGroup.root(h.div.class("fruits").attr("aria-label", "Fruit")).children( h.label.children( Checkbox(h, { key: "all", props: { parent: true }, view: (checkbox) => checkbox.root(h.button) }), "All fruit" ), fruits.map((fruit) => h.label.children( Checkbox(h, { key: fruit, props: { value: fruit }, view: (checkbox) => checkbox.root(h.button) }), fruit ) ) ) })})
// The root is a styled group, and the parent is mixed and points at the apple it controls.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Basket, {}))if (!/<div class="fruits"[^>]* role="group"/.test(html)) throw new Error("the root isn't a styled group")const controls = /aria-checked="mixed"[^>]* aria-controls="([^"]+)"/.exec(html)?.[1].split(" ") ?? []const apple = /<button [^>]* id="([^"]+)"[^>]*><input type="hidden" name="fruit" value="apple">/.exec(html)?.[1]if (apple === undefined || !controls.includes(apple)) throw new Error("the mixed parent doesn't control the apple")Components
CheckboxGroup
unstableA headless checkbox group. Call it with the consumer's h, the props,
and a view closure that draws the root with checkboxGroup.root(h.div) and the
checkboxes inside it.
Details
The parts also return value and disabled, for markup that depends on
them.
The root writes role="group" and data-disabled over what the consumer
set on the builder before the part, including a data-disabled the state
leaves absent. An attribute the consumer chains after the part is the
consumer's to answer for.
A click on a disabled group's checkboxes emits nothing.
Signature
declare const CheckboxGroup: Headless<{ readonly allValues?: readonly Array<string>; readonly disabled?: boolean; readonly name?: string; readonly value: readonly Array<string>;}, { readonly _tag: "Updated"; readonly value: readonly Array<string>;}, { disabled: boolean; root: Part<string, [], never>; value: readonly Array<string>;}, never, { readonly _tag: "ToggledItem"; readonly value: string;} | { readonly _tag: "ToggledParent";}, Shared<"CheckboxGroup">, { readonly disabled: boolean; readonly name: string | null; readonly parent: { readonly checked: boolean; readonly controls: string; readonly indeterminate: boolean; } | null; readonly value: readonly Array<string>;}, { readonly disabled: boolean; readonly id: string; readonly value: string;} | null, SharedKey<"CheckboxGroup", { readonly disabled: boolean; readonly name: string | null; readonly parent: { readonly checked: boolean; readonly controls: string; readonly indeterminate: boolean; } | null; readonly value: readonly Array<string>;}, { readonly _tag: "ToggledItem"; readonly value: string;} | { readonly _tag: "ToggledParent";}, { readonly disabled: boolean; readonly id: string; readonly value: string;} | null, Shared<"CheckboxGroup">>, { readonly remembered: readonly Array<string>;}, { readonly _tag: "ToggledItem"; readonly value: string;} | { readonly _tag: "ToggledParent";}, QueryEntries<{}, {}>>Models
Schemas
CheckboxGroupEvents
unstableUpdated asks for the ticked values in value, after a member or the
parent was clicked. Store them and pass them back as value.
Signature
declare const CheckboxGroupEvents: TaggedEnum<{ Updated: TaggedStruct<"Updated", { readonly value: $Array<String>; }>;}>CheckboxGroupProps
unstableProps for the value of a CheckboxGroup and the checkboxes it holds.
Details
value: the values of the ticked members. Required: the consumer owns it.allValues: the values of every member, in order. Needed for a parent checkbox: without it aparentcheckbox follows its ownchecked.disabled: disables every checkbox in the group and setsdata-disabledon the root.name: the form name of each member that has nonameof its own.
Signature
declare const CheckboxGroupProps: Struct<{ readonly allValues: optional<$Array<String>>; readonly disabled: optional<Boolean>; readonly name: optional<String>; readonly value: $Array<String>;}>