Playground

Tree select

A select whose options are a nested tree, with search and single or multiple selection.

  • 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

Single

Type to search. ArrowRight and ArrowLeft open and close branches.

Multiple

Props

TreeSelectProps

PropTypeDefaultDescription
items requiredTreeNode[]–The tree: top-level nodes, each with an `id`, a `label` and optional `children`. Same shape as `Tree`.
valuestring[]–Selected node ids when controlled. Always an array, also in single mode (zero or one id). Pair with `onValueChange`.
defaultValuestring[][]Ids selected at first when uncontrolled. Default none.
onValueChange((ids: string[]) => void)–Called with the full list of selected ids after each change.
multiplebooleanfalseAllows several selections: choosing a node toggles it and the list stays open. Default false (choosing closes the list).
emptyTextstring'No matches'Shown when the search matches nothing. Default "No matches".
invalidboolean–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.

AttributeStylesheet
[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

ClassStylesheet
.field03-form-fields.css
.field-hint03-form-fields.css
.input03-form-fields.css
.input-bare03-form-fields.css
.input-end03-form-fields.css
.input-lg03-form-fields.css
.input-sm03-form-fields.css
.input-wrap03-form-fields.css
.muted03-form-fields.css
.seg03-form-fields.css
.seg-opt03-form-fields.css
.select03-form-fields.css
.setting03-form-fields.css
.switch03-form-fields.css
.textarea03-form-fields.css
.tree-selecttree-select.css
.tree-select-emptytree-select.css
.tree-select-itemtree-select.css
.tree-select-listtree-select.css
.tree-select-twistytree-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

KeysAction
ArrowDown / ArrowUpOpen the list and move
ArrowRightOpens a branch then enters it
ArrowLeftCloses it then goes to the parent (swapped in RTL)
EnterSelects or toggles
EscapeCloses