Select
A trigger and a panel of options that haus draws, so an option can carry a hint.
Examples
Playground
States
Every state side by side. Hover and focus are forced, so they can be reviewed without a pointer. Open is the Motion story.
Content
Options with a second line, which a native select cannot draw; plain options with one disabled, which the keyboard skips; and a hint under the field.
Install
npx shadcn add @hipuku/selectUsage
import { Select } from '@/components/haus/components/Select'
<Select
label="Role"
value={role}
onChange={setRole}
options={[
{ label: 'Author', value: 'author', hint: 'Can propose and revise' },
{ label: 'Viewer', value: 'viewer' },
]}
/>API
Select also takes its element's own attributes and forwards its ref. className lands on the root
and controlClassName on the trigger.
| Prop | Type | Description |
|---|---|---|
optionsrequired | SelectOption[] | |
valuerequired | string | |
onChangerequired | (value: string) => void | |
placeholder | string | Shown when nothing is selected. |
disabled | boolean | |
name | string | Renders a hidden input so the control still submits inside a plain
<form action>. Omit it for a purely controlled use. |
controlClassName | string | Lands on the trigger rather than the root. Ruling B5. |
hint | string | A line under the control that describes it, read out with it. Stays when
there is an error, which sits above it. Same as Input's. |
error | string | What is wrong with the current choice. Marks the trigger aria-invalid,
rings it in the error colour, and is announced when it appears. |
portal | boolean | Render the panel into document.body, for a Select inside something that
clips: a Modal body is overflow-y: auto, and an absolute panel is cut
off at its edge. Passed straight to Popover. haus#68. |
className | string | |
label | string |
Options and forms
Options come from options. Each has a label, a value, and an optional hint shown under the
label, which a native <select> cannot do, and disabled. Select is controlled: pass value and
onChange. Set name and it submits inside a plain form.
Inside something that scrolls or clips, such as a Modal's body, set portal so the panel is not
cut off.
Accessibility
- A name is required, as
labeloraria-labelledby. - Focus stays on the trigger. The arrow keys move the highlight, Enter or Space chooses, and typing a letter jumps to the next option starting with it.
- Escape or a click outside closes it. The selected option carries a check, not only a colour.