Several choices under one question, with All, at least and at most, and two columns when there's room.
- 2.5 kB JS 3.6 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 25 tests
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { CheckboxGroup } from "./ui/uicontext/components/CheckboxGroup.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 28rem)" } })const Message = UiSchema.TaggedEnum({ Changed: { topics: Schema.Array(Schema.String) } })const topics = [ { value: "mentions", label: "Mentions", description: "Someone @mentions you or your team" }, { value: "assigned", label: "Assigned to me", description: "An issue or review is given to you" }, { value: "comments", label: "Comments on my issues" }, { value: "weekly", label: "Weekly summary", description: "Every Monday at 9:00" }, { value: "security", label: "Security alerts", description: "Always on for admins", disabled: true },]// a profile's email settings: an "All" checkbox that shows a dash while some are onexport const EmailSettings = Html.component({ name: "EmailSettings", model: Schema.Struct({ topics: Schema.Array(Schema.String) }), message: Message,}).make({ init: () => ({ topics: ["mentions", "assigned", "security"] }), update: { Changed: ($, { topics }) => $.evolve({ topics }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( CheckboxGroup(h, { id: "email", legend: "Email me about", options: topics, value: model.topics, all: "Everything", name: "email", onChange: (next) => Message.Changed({ topics: next }), }), ),})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.
This example also uses Domain. Install it too:
npx shadcn@latest add checkfu/uicontext/button-familyimport { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { CheckboxGroup } from "./ui/uicontext/components/CheckboxGroup.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" }, submit: { justifySelf: "start" },})const Message = UiSchema.TaggedEnum({ Changed: { regions: Schema.Array(Schema.String) }, Submitted: {} })const regions = ["us-east-1", "us-west-2", "ca-central-1", "eu-west-1", "eu-central-1", "eu-north-1", "ap-southeast-1", "ap-northeast-1"].map((value) => ({ value, label: value }))// where a webhook fires: at least one region, at most three, in two columns once there's roomexport const WebhookRegions = Html.component({ name: "WebhookRegions", model: Schema.Struct({ regions: Schema.Array(Schema.String), tried: Schema.Boolean, saved: Schema.Boolean }), message: Message,}).make({ init: () => ({ regions: [], tried: false, saved: false }), update: { Changed: ($, { regions }) => $.evolve({ regions, saved: false }), Submitted: ($) => $.evolve({ tried: true, saved: $.model.regions.length > 0 }), }, view: (h, model) => h.form.attrs(sx(styles.form)).on("submit", Message.Submitted(), { preventDefault: true }).children( CheckboxGroup(h, { id: "regions", legend: "Fire in these regions", description: "Events from other regions are dropped.", options: regions, value: model.regions, min: 1, max: 3, showErrors: model.tried, columns: true, onChange: (next) => Message.Changed({ regions: next }), }), Button(h, { type: "submit", tone: "primary", style: styles.submit }).children(model.saved ? "Saved" : "Save regions"), ),})API reference
CheckboxGroupView function
Options
| Name | Type | Description |
|---|---|---|
idrequired | string | A prefix for the ids of the group's parts; unique on the page. |
legendrequired | string | The question: "Email me about". |
description | string | — |
optionsrequired | CheckboxChoice[] | — |
valuerequired | string[] | The values on. The caller keeps them. |
onChangerequired | (value: string[]) => Message | — |
all | string | A checkbox at the top that ticks or clears every choice: "All regions". |
min | number | At least this many; the error shows when fewer are on and showErrors is set. |
max | number | At most this many: once reached, the others can't be turned on. |
showErrors | boolean | Shows the min error; set it once someone has tried to go on. |
columns | boolean | Two columns once the group is 480px wide, for a long list of short labels. |
name | string | Submits each value on under this name. |
disabled | boolean | — |
style | Style | — |
CheckboxView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the checkbox among others in the same view; needed for more than one |
checkedrequired | boolean | — | Whether it's ticked. The caller keeps it and passes back what onToggle asks for. |
indeterminate | boolean | — | Some of what it stands for is ticked: shown as a dash, announced as mixed |
disabled | boolean | — | Can't be pressed or focused, and is skipped when its form submits. |
readOnly | boolean | — | Shows the value and stays focusable, but a press doesn't change it: a setting only an admin can change. |
required | boolean | — | Must be ticked before its form submits. |
name | string | — | The form field's name: a ticked checkbox submits its value under it |
value | string | on | What a ticked box submits under name; on by default. |
label | string | — | The visible label beside the box; leave it out inside a FormField |
ariaLabel | string | — | 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 |
field | FieldControlAttrs | — | A FormField's attributes: its label names the box, its description and error describe it |
style | Style | — | StyleX styles applied after the checkbox’s own. |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/checkbox-group