Skip to content
EspañolEnglish
All components (283)

Tree select

Fields · TreeSelect

What it is and when to use it

A dropdown with a tree inside: choosing a category, a department or a folder from a hierarchy without having the whole tree always in view. The field is that of Select; the panel is the Tree.

When to use it

  • A hierarchy from which ONE thing (or several) is chosen and which does not fit as a flat list.

When not to

  • Data in levels where a leaf is chosen by following a path (country > province > city): Cascader.
  • The tree has to be visible at all times, as navigation or as a filter: the standalone Tree.

One or several: with selection="single" (the default) choosing a node closes the panel; with "multiple" it stays open and what is chosen appears in the field, in the order of the tree. Any node can be chosen, branch or leaf, except the disabled ones. The branches leading to what is chosen open on their own.

Keyboard: Down Arrow or Up Arrow open the field; inside, those of the tree (arrow keys, Right Arrow opens a branch, Left Arrow closes it or goes up, Home and End, typing to jump, Enter or Space chooses) and Escape closes it, returning the focus to the field.

Accessibility: the panel is a treegrid with the name of the field; the focus enters the chosen row or the first one.

Form: with FormField (see "Form"): value, onChange and onBlur are passed to it as they are.

Fine-tuning: --amset-field-* variables of the field and --amset-tree-select-height (maximum height of the panel). No search inside the panel yet.

What it looks like

The one in the product catalog
Clothing
Shirts
Pants
Coats
Feathers
Wool
Home

<TreeSelect
  label="Category"
  placeholder="Choose a category"
  branches={[
    { key: 'clothing', label: 'Clothing', children: [
      { key: 'abrigos', label: 'Coats', children: [{ key: 'lana', label: 'Wool' }] },
    ] },
    { key: 'hogar', label: 'Home' },
  ]}
  defaultValue="lana"
/>

Props

Import it with import { TreeSelect } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
branchesrequiredreadonly TreeNode[]The tree of options. Any node can be chosen, branch or leaf, except disabled ones.
labelrequiredstring—
cascadebooleanChecking a branch checks everything inside it, and a branch with only part of it checked shows as “partial”. Without this, each node is checked on its own (branch and leaves independent).
classNamestringMerged with the library classes; it does not replace them.
defaultValuestring | readonly string[] | null—
disabledboolean—
displayRender(labels: readonly string[]) => ReactNodeHow the chosen items are shown in the closed field. Receives the names. By default, separated by commas.
errorstring—
filter(node: TreeNode, text: string) => booleanWhich nodes remain when searching. By default, those whose name contains the typed text, ignoring case and accents. With this you can also search by code, by synonyms or in another field of the node. Whatever hangs from a matching node stays, and the path to it opens.
hiddenLabelbooleanThe label for the screen reader only.
hintstring—
idstring—
initialOpenKeysreadonly string[]Branches open at the start. Those leading to the chosen one open by themselves.
invalidboolean—
loadData(key: string) => Promise<readonly TreeNode[]>Requests a branch's children the first time it opens (lazy loading). The branch has `isLeaf: false` and no `children`. What arrives is remembered, so the closed field knows the name of what was chosen inside it.
loadErrorTextstringWhat is announced when loading fails. By default, the notice of the language.
loadingTextstringWhat is announced while the children arrive. By default, the "Loading…" of the language.
namestringSo that what is chosen travels in a native form: the key (or, with several, one field for each).
offsetnumber—
onBlur() => voidOn leaving the field. The form uses it to mark that it has been touched.
onChange((key: string | null) => void) | ((keys: readonly string[]) => void)—
onLoadError(key: string, error: unknown) => voidReports that loading a branch failed; the branch can be opened again to retry.
placeholderstring—
placementPlacement—
readOnlyboolean—
requiredboolean—
searchablebooleanPuts a search box above the tree: what matches stays, with the path to it open. For trees of hundreds of nodes.
selection"multiple" | "single"—
size"sm" | "md" | "lg"—
valuestring | readonly string[] | null—
variantFieldVariant—

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: