# Multi-Select

> Pick several from a known list, shown as chips, such as assignees.

Page: https://uicontext.com/docs/components/multi-select

Group: Overlays.

## When to use

Pick several from a known list, shown as chips, such as assignees. Use [Tag Input](https://uicontext.com/docs/components/tag-input.md) instead when people can type values the list doesn't hold.

## Usage

```ts
import { MultiSelect } from "./ui/uicontext/components/MultiSelect.ts"

const Message = UiSchema.TaggedEnum({ ChangedTeams: { selected: Schema.Array(Schema.String) } })

const teams = [
	{ value: "design", label: "Design" },
	{ value: "platform", label: "Platform" },
	{ value: "growth", label: "Growth" },
]

export const Teams = (h: Html.H<typeof Message.Type>, selected: ReadonlyArray<string>) =>
	MultiSelect(h, {
		props: { label: "Teams", options: teams, selected },
		onEmit: { RequestedSelected: ({ selected }) => Message.ChangedTeams({ selected }) },
	})
```

## Examples

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

### Assignees

The example the page opens with: Multi-Select in “Assignees”.

This example also uses Avatar. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/avatar
```

```ts title="src/demo.ts" source="www/src/examples/multi-select/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { MultiSelect, type MultiSelectOption } from "./ui/uicontext/components/MultiSelect.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { reviewers } from "./people.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 28rem)" } })

const people: ReadonlyArray<MultiSelectOption> = reviewers.map((p) => ({
	value: p.id,
	label: p.name,
	glyph: { _tag: "Person", id: p.id, name: p.name, agent: false, ...(p.presence === undefined ? {} : { presence: p.presence }) },
	description: `${p.id}@northwind.example`,
}))

const Message = UiSchema.TaggedEnum({ ChangedAssignees: { selected: Schema.Array(Schema.String) } })

// an issue's assignees: pick from the team, by name or email
export const IssueAssignees = Html.component({
	name: "IssueAssignees",
	model: Schema.Struct({ assignees: Schema.Array(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ assignees: ["ada", "mira"] }),
	update: { ChangedAssignees: ($, { selected }) => $.evolve({ assignees: selected }) },
	view: (h, { assignees }) =>
		h.div.attrs(sx(styles.wrap)).children(
			MultiSelect(h, {
				props: { label: "Assignees", options: people, selected: assignees, placeholder: "Assign people", name: "assignees" },
				onEmit: { RequestedSelected: ({ selected }) => Message.ChangedAssignees({ selected }) },
			}),
		),
})
```

### A limit, and an option that's down

`max` counts the picks and refuses one more. A disabled option says why it can't be picked.

```ts title="src/regions.ts" source="www/src/examples/multi-select/regions.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { MultiSelect, type MultiSelectOption } from "./ui/uicontext/components/MultiSelect.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 28rem)" } })

const up = { _tag: "Swatch" as const, color: color.success }

const regions: ReadonlyArray<MultiSelectOption> = [
	{ value: "iad", label: "us-east-1", description: "Ashburn, Virginia", glyph: up },
	{ value: "pdx", label: "us-west-2", description: "Portland, Oregon", glyph: up },
	{ value: "fra", label: "eu-central-1", description: "Frankfurt", glyph: up },
	{ value: "dub", label: "eu-west-1", description: "Dublin", glyph: up },
	{ value: "sin", label: "ap-southeast-1", description: "Singapore · degraded, deploys paused", glyph: { _tag: "Swatch", color: color.warning }, disabled: true },
	{ value: "nrt", label: "ap-northeast-1", description: "Tokyo", glyph: up },
	{ value: "gru", label: "sa-east-1", description: "São Paulo", glyph: up },
]

const Message = UiSchema.TaggedEnum({ ChangedRegions: { selected: Schema.Array(Schema.String) } })

// where a release goes: at most three regions on the starter plan, and one is down
export const DeployRegions = Html.component({
	name: "DeployRegions",
	model: Schema.Struct({ regions: Schema.Array(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ regions: ["iad", "fra"] }),
	update: { ChangedRegions: ($, { selected }) => $.evolve({ regions: selected }) },
	view: (h, { regions: chosen }) =>
		h.div.attrs(sx(styles.wrap)).children(
			MultiSelect(h, {
				props: { label: "Regions", options: regions, selected: chosen, max: 3, description: "Your plan deploys to three regions." },
				onEmit: { RequestedSelected: ({ selected }) => Message.ChangedRegions({ selected }) },
			}),
		),
})
```

### Sample data

The examples above import these files.

```ts title="src/people.ts" source="www/src/examples/_shared/people.ts"
import type { AvatarPerson } from "./ui/uicontext/components/Avatar.ts"

// People for avatars, pickers and mentions. Pictures are drawn inline, a head and shoulders on a
// soft backdrop, so the examples need no image files.

const portrait = (backdrop: string, skin: string, shirt: string, hair: string) =>
	`data:image/svg+xml,${encodeURIComponent(
		`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="${backdrop}"/><path d="M10 64c2-13 11-19 22-19s20 6 22 19z" fill="${shirt}"/><circle cx="32" cy="27" r="12" fill="${skin}"/><path d="M19 26c0-9 6-14 13-14s13 5 13 14c-3-5-8-7-13-7s-10 2-13 7z" fill="${hair}"/></svg>`,
	)}`

export const ada: AvatarPerson = { id: "ada", name: "Ada Lovelace", image: portrait("#dbe7f7", "#f1c7a5", "#3b5b8c", "#4a2f22"), presence: "active" }
export const ben: AvatarPerson = { id: "ben", name: "Ben Okafor", image: portrait("#f3e3d3", "#8d5a3b", "#2f6b4f", "#1d1410"), presence: "away" }
export const mira: AvatarPerson = { id: "mira", name: "Mira Patel", presence: "dnd" }
export const sam: AvatarPerson = { id: "sam", name: "Sam Rivera", image: portrait("#e6e1f5", "#d9a57e", "#7a3e8c", "#2b1c14"), presence: "offline" }
export const kenji: AvatarPerson = { id: "kenji", name: "Kenji Watanabe" }
export const lena: AvatarPerson = { id: "lena", name: "Lena Fischer", image: portrait("#e3f1e8", "#f4d2b8", "#b0413e", "#c8a165") }
export const omar: AvatarPerson = { id: "omar", name: "Omar Haddad" }
export const priya: AvatarPerson = { id: "priya", name: "Priya Raman", presence: "active" }
export const scout: AvatarPerson = { id: "scout", name: "Scout", agent: true }

export const reviewers: ReadonlyArray<AvatarPerson> = [ada, ben, mira, sam, kenji, lena, omar]

/** Names that break layouts, and a picture that never loads. */
export const worstPeople: ReadonlyArray<AvatarPerson> = [
	{ id: "aleksandra", name: "Aleksandra Wiśniewska-Kowalczyk", presence: "active" },
	{ id: "christopher", name: "Christopher Alexander Montgomery III" },
	{ id: "j", name: "J" },
	{ id: "fox", name: "🦊 Fox" },
	{ id: "xiuying", name: "王秀英", presence: "away" },
	{ id: "ngoc-han", name: "Đặng Thị Ngọc Hân" },
	{ id: "broken", name: "Dana Whitfield", image: "/missing/dana.jpg" },
	{ id: "relay", name: "Relay Customer Escalations Assistant", agent: true },
]

/** A big workspace: 1,284 members. */
export const workspace: ReadonlyArray<AvatarPerson> = Array.from({ length: 1284 }, (_, i) => {
	const first = ["Ada", "Ben", "Mira", "Sam", "Kenji", "Lena", "Omar", "Priya", "Jo", "Rafael", "Ines", "Tomas"][i % 12]!
	const last = ["Lovelace", "Okafor", "Patel", "Rivera", "Watanabe", "Fischer", "Haddad", "Raman", "Kim", "Silva", "Novak", "Berg"][Math.floor(i / 12) % 12]!
	return { id: `m${i}`, name: `${first} ${last}` }
})
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | What's picked: "Assignees", "Regions". |
| `options` (required) | `MultiSelectOption[]` | — |
| `selected` (required) | `string[]` | The picked values, in the order picked. The caller keeps them and stores what `RequestedSelected` asks for. |
| `placeholder` | `string` | — |
| `description` | `string` | A line under the field. |
| `max` | `number` | The most that can be picked; the count shows under the field. |
| `disabled` | `boolean` | — |
| `name` | `string` | Submits each value under this form field name. |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedSelected` | `{ selected: string[] }` | The values asked for, all of them: store them and pass them back as `selected`. |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| A–Z | Filter the options |
| Down arrow, Up arrow | Open the list, then move through it |
| Enter | Pick the highlighted option, or unpick it |
| Backspace | In an empty field, remove the last pick |
| Left arrow, Right arrow | Move between picks and back to the field |
| Escape | Close the list |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/multi-select
```

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

It writes these files:

- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/Motion.ts`
- `src/ui/uicontext/components/MultiSelect.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.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/MultiSelect.ts](https://github.com/checkfu/uicontext/blob/main/src/MultiSelect.ts)

## Related

- [Dialog](https://uicontext.com/docs/components/dialog.md) (`dialog`): A modal window with a title, a description and actions.
- [Menu](https://uicontext.com/docs/components/menu.md) (`menu`): A dropdown menu of actions, checkbox and radio items, shortcuts and submenus.
- [Context Menu](https://uicontext.com/docs/components/context-menu.md) (`context-menu`): The menu a right click or long press opens over rows, files and cards.
- [Sheet](https://uicontext.com/docs/components/sheet.md) (`sheet`): A panel that slides over the page from a side or the bottom.
- [Toast](https://uicontext.com/docs/components/toast.md) (`toast`): Short notifications with an optional action like Undo.
- [Popover](https://uicontext.com/docs/components/popover.md) (`popover`): Content that opens beside its trigger and closes when you click away.
