Tree select
A select whose options are a nested tree, with search and single or multiple selection.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- An input with role=combobox, aria-haspopup=tree and aria-activedescendant; focus never leaves the input.
- The popup is role=tree: each treeitem has aria-level, aria-setsize, aria-posinset, aria-selected and (for branches) aria-expanded; aria-multiselectable in multiple mode.
- ArrowDown/ArrowUp open the list and move; ArrowRight opens a branch then enters it; ArrowLeft closes it then goes to the parent (swapped in RTL); Enter selects or toggles; Escape closes.
- Styles in one file:
03-form-fields.css
Import
import '@monoline/css';
import {TreeSelect} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add tree-select
Usage
import {Field, TreeSelect, type TreeNode} from '@monoline/react';
const items: TreeNode[] = [
{id: 'eng', label: 'Engineering', children: [
{id: 'web', label: 'Web'},
{id: 'platform', label: 'Platform', children: [{id: 'infra', label: 'Infrastructure'}, {id: 'data', label: 'Data'}]},
]},
{id: 'design', label: 'Design', children: [{id: 'brand', label: 'Brand'}, {id: 'product', label: 'Product design'}]},
{id: 'ops', label: 'Operations'},
];
<div style={{maxWidth: '320px', minHeight: '300px'}}>
<Field label="Team">
<TreeSelect items={items} placeholder="Choose a team" />
</Field>
</div>
Examples
Multiple
Props
TreeSelectProps
| Prop | Type | Default | Description |
|---|---|---|---|
items required | TreeNode[] | – | The tree: top-level nodes, each with an `id`, a `label` and optional `children`. Same shape as `Tree`. |
value | string[] | – | Selected node ids when controlled. Always an array, also in single mode (zero or one id). Pair with `onValueChange`. |
defaultValue | string[] | [] | Ids selected at first when uncontrolled. Default none. |
onValueChange | ((ids: string[]) => void) | – | Called with the full list of selected ids after each change. |
multiple | boolean | false | Allows several selections: choosing a node toggles it and the list stays open. Default false (choosing closes the list). |
emptyText | string | 'No matches' | Shown when the search matches nothing. Default "No matches". |
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"] | tree-select.css |
[aria-selected="true"] | tree-select.css |
[data-active] | tree-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 |
.tree-select | tree-select.css |
.tree-select-empty | tree-select.css |
.tree-select-item | tree-select.css |
.tree-select-list | tree-select.css |
.tree-select-twisty | tree-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
- An input with role=combobox, aria-haspopup=tree and aria-activedescendant; focus never leaves the input.
- The popup is role=tree: each treeitem has aria-level, aria-setsize, aria-posinset, aria-selected and (for branches) aria-expanded; aria-multiselectable in multiple mode.
- ArrowDown/ArrowUp open the list and move; ArrowRight opens a branch then enters it; ArrowLeft closes it then goes to the parent (swapped in RTL); Enter selects or toggles; Escape closes.
- Typing filters the tree to matching nodes and their parents, opened automatically; an empty result shows text.
- Selection is shown in the input as text ('A, B' or '3 selected') and a check mark, not colour.
Keyboard
| Keys | Action |
|---|---|
| ArrowDown / ArrowUp | Open the list and move |
| ArrowRight | Opens a branch then enters it |
| ArrowLeft | Closes it then goes to the parent (swapped in RTL) |
| Enter | Selects or toggles |
| Escape | Closes |