Checkbox Group

View as Markdown

Several choices under one question, with All, at least and at most, and two columns when there's room.

Email me about

When to use

Several choices under one question, with All, at least and at most, and two columns when there's room. Use Multi-Select instead when the choices are too many to show at once.

Usage

import { CheckboxGroup } from "./ui/uicontext/components/CheckboxGroup.ts"
const Message = UiSchema.TaggedEnum({ ChangedFields: { fields: Schema.Array(Schema.String) } })export const ExportFields = (h: Html.H<typeof Message.Type>, fields: ReadonlyArray<string>) =>	CheckboxGroup(h, {		id: "export",		legend: "Include in the export",		options: [{ value: "email", label: "Email" }, { value: "plan", label: "Plan" }, { value: "spend", label: "Spend this year" }],		value: fields,		all: "All fields",		onChange: (next) => Message.ChangedFields({ fields: next }),	})

Examples

At least one, at most three

min shows its error once showErrors is set, after a try. At max, the rest turn off until one is cleared.

Fire in these regions

Events from other regions are dropped.

Up to 3.

API reference

CheckboxGroupView function

Options

CheckboxGroup options
NameTypeDescription
idrequiredstringA prefix for the ids of the group's parts; unique on the page.
legendrequiredstringThe question: "Email me about".
descriptionstring—
optionsrequiredCheckboxChoice[]—
valuerequiredstring[]The values on. The caller keeps them.
onChangerequired(value: string[]) => Message—
allstringA checkbox at the top that ticks or clears every choice: "All regions".
minnumberAt least this many; the error shows when fewer are on and showErrors is set.
maxnumberAt most this many: once reached, the others can't be turned on.
showErrorsbooleanShows the min error; set it once someone has tried to go on.
columnsbooleanTwo columns once the group is 480px wide, for a long list of short labels.
namestringSubmits each value on under this name.
disabledboolean—
styleStyle—

CheckboxView function

Options

Checkbox options
NameTypeDefaultDescription
keystring—Names the checkbox among others in the same view; needed for more than one
checkedrequiredboolean—Whether it's ticked. The caller keeps it and passes back what onToggle asks for.
indeterminateboolean—Some of what it stands for is ticked: shown as a dash, announced as mixed
disabledboolean—Can't be pressed or focused, and is skipped when its form submits.
readOnlyboolean—Shows the value and stays focusable, but a press doesn't change it: a setting only an admin can change.
requiredboolean—Must be ticked before its form submits.
namestring—The form field's name: a ticked checkbox submits its value under it
valuestringonWhat a ticked box submits under name; on by default.
labelstring—The visible label beside the box; leave it out inside a FormField
ariaLabelstring—The accessible name when there's no visible label (a row's selection box)
onTogglerequired(checked: boolean) => Message—What a press asks for: the value to store and pass back as checked
fieldFieldControlAttrs—A FormField's attributes: its label names the box, its description and error describe it
styleStyle—StyleX styles applied after the checkbox’s own.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/checkbox-group

Source