Multi select
Type to filter a list and pick several options, shown as removable tags.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- ARIA 1.2 combobox with a listbox that has aria-multiselectable; options carry aria-selected.
- ArrowDown/ArrowUp move, Enter toggles the active option, Escape closes, Tab leaves.
- Backspace in an empty field removes the last tag; each tag has a button named "Remove <label>".
- Styles in one file:
03-form-fields.css
Import
import '@monoline/css';
import {MultiSelect} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add multi-select
Usage
import {Field, MultiSelect} from '@monoline/react';
const options = ['Ada Lovelace', 'Grace Hopper', 'Alan Turing', 'Katherine Johnson', 'Linus Torvalds'].map((n) => ({value: n.toLowerCase().replace(/\W+/g, '-'), label: n}));
<div style={{maxWidth: '360px', minHeight: '280px'}}>
<Field label="People">
<MultiSelect
options={options}
placeholder="Type a name"
defaultValue={['ada-lovelace']}
/>
</Field>
</div>
Examples
Pick several
Props
MultiSelectProps
| Prop | Type | Default | Description |
|---|---|---|---|
options required | MultiSelectOption[] | – | The choices, each with a `value`, a `label` and an optional `disabled`. The user filters them by typing part of the label. |
value | string[] | – | The chosen option values when controlled. Pair with `onValueChange`. |
defaultValue | string[] | [] | The initially chosen option values when uncontrolled. Default empty. |
onValueChange | ((value: string[]) => void) | – | Called with the full list of chosen values. |
emptyText | string | 'No matches' | Shown when nothing matches. |
size | "sm" | "md" | "lg" | 'md' | `md` (default), `sm` for dense layouts or `lg` for prominent forms. Heights match Button, Input and Select at each size. |
invalid | boolean | – | Marks the field as having an error: sets `aria-invalid` and shows the error border. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[aria-checked="true"] | 03-form-fields.css |
[aria-invalid="true"] | 03-form-fields.css |
[aria-pressed="true"] | 03-form-fields.css |
[aria-disabled="true"] | multi-select.css |
[aria-selected="true"] | multi-select.css |
[data-active] | multi-select.css |
[data-disabled] | multi-select.css |
[data-invalid] | multi-select.css |
[data-size="lg"] | multi-select.css |
[data-size="sm"] | multi-select.css |
CSS classes
| Class | Stylesheet |
|---|---|
.field | 03-form-fields.css |
.field-hint | 03-form-fields.css |
.input | 03-form-fields.css |
.input-bare | 03-form-fields.css |
.input-end | 03-form-fields.css |
.input-lg | 03-form-fields.css |
.input-sm | 03-form-fields.css |
.input-wrap | 03-form-fields.css |
.muted | 03-form-fields.css |
.seg | 03-form-fields.css |
.seg-opt | 03-form-fields.css |
.select | 03-form-fields.css |
.setting | 03-form-fields.css |
.switch | 03-form-fields.css |
.textarea | 03-form-fields.css |
.avatar | 19-avatar.css |
.avatar-group | 19-avatar.css |
.avatar-lg | 19-avatar.css |
.avatar-line | 19-avatar.css |
.avatar-sm | 19-avatar.css |
.stat | 19-avatar.css |
.stat-note | 19-avatar.css |
.stat-value | 19-avatar.css |
.tag | 19-avatar.css |
.multi-select | multi-select.css |
.multi-select-box | multi-select.css |
.multi-select-empty | multi-select.css |
.multi-select-input | multi-select.css |
.multi-select-list | multi-select.css |
.multi-select-option | multi-select.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.field {
border-color: var(--accent);
}
.field[aria-checked="true"] {
background: var(--wash);
}
Accessibility
- ARIA 1.2 combobox with a listbox that has aria-multiselectable; options carry aria-selected.
- ArrowDown/ArrowUp move, Enter toggles the active option, Escape closes, Tab leaves.
- Backspace in an empty field removes the last tag; each tag has a button named "Remove <label>".
Keyboard
| Keys | Action |
|---|---|
| ArrowDown / ArrowUp | Move |
| Enter | Toggles the active option |
| Escape | Closes |
| Tab | Leaves |
| Backspace | In an empty field removes the last tag |