Pick several from a known list, shown as chips, such as assignees.
- 6.2 kB JS 4.5 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- 6 keys checked
- Right-to-left
- 9 tests
Assignees
Ada Lovelace[email protected]
Ben Okafor[email protected]
Mira Patel[email protected]
Sam Rivera[email protected]
Kenji Watanabe[email protected]
Lena Fischer[email protected]
Omar Haddad[email protected]
This example also uses Avatar. Install it too:
npx shadcn@latest add checkfu/uicontext/avatarimport { 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 emailexport 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 }) }, }), ),})When to use
Pick several from a known list, shown as chips, such as assignees. Use Tag Input instead when people can type values the list doesn't hold.
Usage
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
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.
Regions
us-east-1Ashburn, Virginia
us-west-2Portland, Oregon
eu-central-1Frankfurt
eu-west-1Dublin
ap-southeast-1Singapore · degraded, deploys paused
ap-northeast-1Tokyo
sa-east-1São Paulo
Your plan deploys to three regions.
2 of 3import { 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 downexport 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 }) }, }), ),})API reference
Props
| Name | Type | Description |
|---|---|---|
labelrequired | string | What's picked: "Assignees", "Regions". |
optionsrequired | MultiSelectOption[] | — |
selectedrequired | 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 | Does |
|---|---|
| a letter | Filter the options |
| ↓Down arrow ↑Up arrow | Open the list, then move through it |
| EnterEnter | Pick the highlighted option, or unpick it |
| BackspaceBackspace | In an empty field, remove the last pick |
| ←Left arrow →Right arrow | Move between picks and back to the field |
| EscEscape | Close the list |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/multi-select