Tree Select
A Select whose dropdown is a tree of expandable groups, where only the leaves can be chosen.
<arc-tree-select>Overview
TreeSelect has the trigger of Select and a tree in its panel. The dropdown shows groups that expand, and the leaves inside them are the choices. The trigger shows the chosen leaf after a muted path of its ancestors, so "Violin" reads as "Strings / Violin".
Only leaves can be selected. A node with children is a group header that expands and collapses but can't be chosen, so the submitted value is always a single leaf. When the panel opens, the branches containing the current value expand, so the selection is in view.
TreeSelect follows the ARIA combobox pattern with a tree popup. Enter, Space or an arrow key opens the panel. Arrow Up and Down move between rows, Arrow Right and Left expand and collapse groups, Enter picks a leaf and Escape closes. Typing jumps to the row that starts with those letters, as in Select. It takes part in native forms through ElementInternals and submits the selected leaf's value under its name.
Guidelines
Do
- Use it when the options have a real hierarchy that readers think in, such as instrument families, product categories or folder trees
- Give every node a stable value, group headers included. Group values drive `expanded-values` and appear in the `path` of `arc-change`
- Keep the tree to two or three levels. A deeper tree belongs in a picker dialog
- Use `expanded-values` to open the branches readers need most
- Set a visible `label`. It's also the accessible name
- Disable a node that's temporarily unavailable instead of removing it, so the structure stays recognizable
Don't
- Don't use it for a flat list. Use Select
- Don't use it when readers need to type to filter a large set. Use Combobox, whose text field takes the keystrokes
- Don't use it for browsing or navigation outside a form. Use TreeView, a standalone tree with no trigger or form value
- Don't expect group headers to be selectable. If a branch must be a valid choice, add a leaf such as "All strings" inside it
Features
- `expanded-values` lists the groups to show expanded at first
- Disabled nodes render, but the keyboard skips them and they can't be selected. A disabled group hides its children
- Supports `required` validation
- Neutral rails show nesting depth and carry no state
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<script type="module" src="@arclux/arc-ui"></script>
<arc-tree-select
label="Instrument"
name="instrument"
placeholder="Pick an instrument..."
></arc-tree-select>
<script>
const treeSelect = document.querySelector('arc-tree-select');
// items is a property, not an attribute: assign the tree from script.
treeSelect.items = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
{ value: 'percussion', label: 'Percussion', children: [
{ value: 'timpani', label: 'Timpani' },
] },
];
// Pre-expand a branch, or let auto-expand follow the selected value.
treeSelect.expandedValues = ['strings'];
treeSelect.addEventListener('arc-change', (e) => {
// e.detail.path holds the ancestor group values, root first.
console.log('Selected:', e.detail.value, 'in', e.detail.path.join(' / '));
});
</script>import { TreeSelect } from '@arclux/arc-ui-react';
import { useState } from 'react';
const instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
{ value: 'percussion', label: 'Percussion', children: [
{ value: 'timpani', label: 'Timpani' },
] },
];
function InstrumentPicker() {
const [instrument, setInstrument] = useState('');
return (
<TreeSelect
label="Instrument"
placeholder="Pick an instrument..."
items={instruments}
value={instrument}
onArcChange={(e) => setInstrument(e.detail.value)}
/>
);
}<script setup>
import { TreeSelect } from '@arclux/arc-ui-vue';
import { ref } from 'vue';
const instrument = ref('');
const instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
];
</script>
<template>
<TreeSelect
label="Instrument"
placeholder="Pick an instrument..."
:items="instruments"
:value="instrument"
@arc-change="instrument = $event.detail.value"
/>
</template><script>
import { TreeSelect } from '@arclux/arc-ui-svelte';
let instrument = '';
const instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
];
</script>
<TreeSelect label="Instrument" placeholder="Pick an instrument..."
items={instruments} value={instrument}
on:arc-change={(e) => instrument = e.detail.value} />import { Component } from '@angular/core';
import { TreeSelect } from '@arclux/arc-ui-angular';
@Component({
imports: [TreeSelect],
template: `
<arc-tree-select label="Instrument" placeholder="Pick an instrument..."
[items]="instruments" [value]="instrument"
(arc-change)="instrument = $event.detail.value">
</arc-tree-select>
`,
})
export class InstrumentPickerComponent {
instrument = '';
instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
];
}import { TreeSelect } from '@arclux/arc-ui-solid';
import { createSignal } from 'solid-js';
const instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
];
function InstrumentPicker() {
const [instrument, setInstrument] = createSignal('');
return (
<TreeSelect label="Instrument" placeholder="Pick an instrument..."
items={instruments} value={instrument()}
onArcChange={(e) => setInstrument(e.detail.value)} />
);
}import { TreeSelect } from '@arclux/arc-ui-preact';
import { useState } from 'preact/hooks';
const instruments = [
{ value: 'keys', label: 'Keys', children: [
{ value: 'grand-piano', label: 'Grand Piano' },
{ value: 'rhodes', label: 'Rhodes' },
] },
{ value: 'strings', label: 'Strings', children: [
{ value: 'violin', label: 'Violin' },
{ value: 'cello', label: 'Cello' },
] },
];
function InstrumentPicker() {
const [instrument, setInstrument] = useState('');
return (
<TreeSelect label="Instrument" placeholder="Pick an instrument..."
items={instruments} value={instrument}
onArcChange={(e) => setInstrument(e.detail.value)} />
);
}API
valuestring''- The selected leaf's value. Setting it programmatically updates the trigger's breadcrumb label, and the branches containing it auto-expand the next time the panel opens.
placeholderstring'Select...'- Hint text displayed inside the trigger when no leaf is selected. It disappears once a value is chosen.
labelstring''- Visible label rendered above the trigger. Also serves as the accessible name. Always provide one for accessibility compliance.
namestring''- Form field name submitted with the selected leaf value via ElementInternals.
disabledbooleanfalse- When true, the trigger becomes non-interactive: it cannot be opened, focused, or clicked, and renders with reduced opacity.
errorstring''- Error message displayed below the trigger. When set, the trigger border turns red.
itemsArray<{value: string, label: string, children?: Array<object>, disabled?: boolean}>[]- Recursive tree of nodes. A node with a non-empty
childrenarray is a group header: it expands and collapses but can never be selected. A node without children is a selectable leaf.disablednodes render but cannot be reached by keyboard or selected, and a disabled group hides its children. expandedValuesstring[][]- Values of group nodes to render initially expanded. Attribute:
expanded-values(JSON array). Branches containing the selected value auto-expand on open regardless of this list. size'sm' | 'md' | 'lg''md'- Controls the trigger size.
openbooleanfalse- Controls whether the tree panel is visible. Automatically set to false when a leaf is selected, Escape is pressed, or the user clicks outside. Held at
falsewhiledisabled. formAssociatedbooleantruepropertiesobject{ // flag(), unlike `disabled`. The exclusion in props.js is specifically // about form-associated *platform* semantics: a `disabled` content // attribute that is merely present makes the element actually disabled // per the HTML spec, and formDisabledCallback assigns the property back, // so no converter can win. Neither of these is platform-mapped: // `required` is enforced by _computeValidity() below and `readonly` by // each component's own interaction handlers, so the stock converter buys // nothing here and costs the usual bug: `required="false"` read as true, // blocking submission of a form the author meant to leave optional. // Finding #48's shape, across all 26 form controls at once. required: flag(false), readonly: flag(false), }- Lit merges static properties up the prototype chain, so every consumer
gets these without declaring them.
requiredparticipates in constraint validation below;readonlyreflects for styling and is enforced by each component's interaction handlers (the mixin can't know which gestures mutate state). autoValidatesbooleantrue- Components that run their own constraint-validation logic (pattern checks, range checks) opt out of the automatic required sync by overriding this to false, and own the whole validity flag set instead.
formvalidityvalidationMessagerequiredbooleanfalsereadonlybooleanfalse
Methods
checkValidity()boolean- Whether the control currently satisfies its constraints, per the native
constraint-validation API. Fires
invalidon the element when it does not, and reports nothing to the user. reportValidity()boolean- As checkValidity(), but also shows the browser's validation message against the control when it fails.
Events
arc-change- Fired when a leaf is selected.
detail.valueis the leaf value,detail.labelits label, anddetail.paththe array of ancestor group values from root to parent.
See also
- SelectA dropdown for choosing one option from a list in a form.
- ComboboxA text input that filters a list of options as you type, for picking one value from a long list.
- Tree ViewShows nested items as a tree whose branches open and close, with one selected item at a time.
- Multi SelectA dropdown for picking several options, shown as removable chips.