Select19
Pick one option from a list. It looks like a field, opens like a menu and
submits like a native <select>.
<Select label="Plan" placeholder="Choose a plan">
<SelectItem id="free">Free</SelectItem>
<SelectItem id="pro">Pro</SelectItem>
<SelectItem id="team">Team</SelectItem>
</Select>Usage
import { Select, SelectItem } from "plonk-ui";
export function Plan() {
return (
<Select label="Plan" name="plan" defaultValue="free">
<SelectItem id="free">Free</SelectItem>
<SelectItem id="pro">Pro</SelectItem>
</Select>
);
}Each SelectItem has an id; that's the value you get back and the value the
form submits under name. For a handful of options that should all be visible,
a Radio Group is quicker to scan.
Descriptions
Options can have a description and an icon. The closed field shows only the
option's name.
import { Select, SelectItem } from "plonk-ui";
export default function Descriptions() {
return (
<Select
className="w-full max-w-xs"
label="Plan"
defaultValue="pro"
description="You can change plans at any time."
>
<SelectItem id="free" description="1 project, 3 members">
Free
</SelectItem>
<SelectItem id="pro" description="Unlimited projects, 10 members">
Pro
</SelectItem>
<SelectItem id="team" description="Everything unlimited">
Team
</SelectItem>
<SelectItem id="enterprise" description="Talk to sales" isDisabled>
Enterprise
</SelectItem>
</Select>
);
}Sections
Group long lists with SelectSection.
import { Select, SelectItem, SelectSection } from "plonk-ui";
export default function Sections() {
return (
<Select
className="w-full max-w-xs"
label="Time zone"
placeholder="Pick a city"
>
<SelectSection title="Europe">
<SelectItem id="Europe/Istanbul">Istanbul</SelectItem>
<SelectItem id="Europe/Berlin">Berlin</SelectItem>
<SelectItem id="Europe/Lisbon">Lisbon</SelectItem>
</SelectSection>
<SelectSection title="Americas">
<SelectItem id="America/New_York">New York</SelectItem>
<SelectItem id="America/Mexico_City">Mexico City</SelectItem>
</SelectSection>
<SelectSection title="Asia">
<SelectItem id="Asia/Tokyo">Tokyo</SelectItem>
<SelectItem id="Asia/Singapore">Singapore</SelectItem>
</SelectSection>
</Select>
);
}Controlled, in a form
Control the value with value and onChange. Validation works like
Input: you decide, then pass the
message to error.
import { useState, type FormEvent } from "react";
import { Button, Select, SelectItem } from "plonk-ui";
export default function Form() {
const [role, setRole] = useState<string | null>(null);
const [submitted, setSubmitted] = useState(false);
const [sent, setSent] = useState<string>();
function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setSubmitted(true);
if (!role) return;
setSent(String(new FormData(event.currentTarget).get("role")));
}
return (
<form
onSubmit={handleSubmit}
noValidate
className="flex w-full max-w-xs flex-col gap-4"
>
<Select
label="Role"
name="role"
placeholder="Choose a role"
isRequired
value={role}
onChange={(key) => setRole(key == null ? null : String(key))}
error={
submitted && !role ? "Pick a role for the new member." : undefined
}
>
<SelectItem id="viewer">Viewer</SelectItem>
<SelectItem id="editor">Editor</SelectItem>
<SelectItem id="admin">Admin</SelectItem>
</Select>
<Button type="submit" className="self-start">
Invite
</Button>
<p role="status" className="font-bold empty:hidden">
{sent ? `Invited as ${sent}.` : ""}
</p>
</form>
);
}Options from data
Pass items and a render function to build the list from an array. Each item
needs an id:
<Select label="Owner" items={members}>
{(member) => <SelectItem>{member.name}</SelectItem>}
</Select>;Personalities
The field lifts on focus like Input, and the list opens like a Menu: from the edge facing the field, without tilting, in each personality's own style. The selected option has a tick (a star in Arcade). The highlight hooks work here too.
Accessibility
- A button with a listbox:
Enter,Spaceor the arrow keys open it, and typing a letter selects the matching option even while closed. - The label, description and error are linked to the button.
- A hidden native
<select>carries the value for forms and autofill. - The focus shadow shows for keyboard users only; clicking doesn't leave it behind.
API
Select
| Prop | Type | Default | Description |
|---|---|---|---|
items | Iterable<T> | — | Options as data, with a render function as children. Each item needs an id. |
className | string | — | Extra classes for the outer wrapper. |
label | ReactNode | — | Visible label, linked to the field. |
description | ReactNode | — | Help text under the field. |
error | ReactNode | — | Error message. Marks the field invalid and shakes it once. Pass true to mark it without a message. |
value | Key | null | — | Controlled value: the selected option's id, or null. |
defaultValue | Key | null | — | Selected option on first render, when uncontrolled. |
onChange | (value: Key | null) => void | — | Called with the new option's id. |
placeholder | string | "Select an item" | Shown while nothing is selected. The default is translated for the provider's locale. |
name | string | — | Field name: the selected id is submitted with the form. |
isRequired | boolean | — | Marks the field required, with an asterisk in the label. |
isDisabled | boolean | — | Disables the field. |
disabledKeys | Iterable<Key> | — | Ids of options that can't be selected. |
SelectItem
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the row. |
icon | ReactNode | — | Icon before the label. Hidden from screen readers. |
description | ReactNode | — | Smaller text under the label, read with it. |
id | Key | — | Unique key, used for selection and in callbacks. |
isDisabled | boolean | — | Can't be focused or chosen. |
textValue | string | — | Plain text for type-to-search. Set it when the label isn't a string. |
SelectSection
| Prop | Type | Default | Description |
|---|---|---|---|
title | ReactNode | — | Group heading, read by screen readers. |