# Checkbox Group

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

Page: https://uicontext.com/docs/components/checkbox-group

Group: Primitives.

## 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](https://uicontext.com/docs/components/multi-select.md) instead when the choices are too many to show at once.

## Usage

```ts
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

Each example is a complete file. Its imports point where the CLI installs the source.

### Email settings

The example the page opens with: Checkbox Group in “Email settings”.

```ts title="src/demo.ts" source="www/src/examples/checkbox-group/demo.ts"
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 on
export 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 }),
			}),
		),
})
```

### 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:

```sh
npx shadcn@latest add checkfu/uicontext/button-family
```

```ts title="src/regions.ts" source="www/src/examples/checkbox-group/regions.ts"
import { 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 room
export 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

### CheckboxGroup (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `id` (required) | `string` | A prefix for the ids of the group's parts; unique on the page. |
| `legend` (required) | `string` | The question: "Email me about". |
| `description` | `string` | — |
| `options` (required) | `CheckboxChoice[]` | — |
| `value` (required) | `string[]` | The values on. The caller keeps them. |
| `onChange` (required) | `(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` | — |

### Checkbox (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `key` | `string` | — | Names the checkbox among others in the same view; needed for more than one |
| `checked` (required) | `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) |
| `onToggle` (required) | `(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

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

With npm: `npx shadcn@latest add checkfu/uicontext/checkbox-group`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/checkbox-group`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/checkbox-group`.

It writes these files:

- `src/ui/uicontext/components/Checkbox.ts`
- `src/ui/uicontext/components/CheckboxGroup.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/CheckboxGroup.ts](https://github.com/checkfu/uicontext/blob/main/src/CheckboxGroup.ts)
- [src/Checkbox.ts](https://github.com/checkfu/uicontext/blob/main/src/Checkbox.ts)

## Used in

- [CRM](https://uicontext.com/blocks/crm.md)
- [Email](https://uicontext.com/blocks/email.md)
- [Scheduling](https://uicontext.com/blocks/scheduling.md)
- [Storefront](https://uicontext.com/blocks/storefront.md)
- [Bots](https://uicontext.com/blocks/bot-console.md)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
