Command palette

@yasinsensoy/web

Web · Components

TreeSelect

Select box from a tree: for nested options like categories, departments, folders, country > city.

5 exports

Live example

Preview

Code

import { TreeSelect } from "@yasinsensoy/web";

<TreeSelect name="unit" items={units} showPath clearable />

Events

  1. Events appear here when you interact with the preview.

In a form

Import

import { TreeSelect } from "@yasinsensoy/web";

TreeSelect

componentClient component

Select box from a tree: for nested options like categories, departments, folders, country > city. Clicking the box opens a TreeView below it.

  • Single selection: clicking a node selects it and closes; with leavesOnly only leaves are selectable. The box shows the name (the full path with showPath).
  • Multiple selection: checkboxes with multiple; a parent is linked to its children (tri-state). The value holds leaves (checkOutput="leaves"), fully selected branches ("top") or all ("all").
  • Search: a search box appears with more than 15 nodes; non-matching branches are hidden, matches highlighted.
  • Large tree: rows are virtualized; with loadChildren branches load as they are expanded.
  • Mobile: the panel is a bottom sheet; it closes by pulling down and with the back button.
  • Form: name is submitted via a hidden native <select>, required works, and it returns to the initial value on form reset.
TreeSelect(props: TreeSelectProps<T>): JSX.Element

Examples

<TreeSelect name="category" leavesOnly showPath items={categories} />

<TreeSelect multiple name="departments" checkOutput="top" clearable items={departments}
  onValueChange={(ids) => setDepartments(ids)} />

Classes that can be passed to the inner parts of TreeSelect.

NameTypeDescription
trigger?stringOpen button (the visible box of the field).
content?stringThe opened panel.
tree?stringThe tree in the panel.

Single-select TreeSelect.

NameTypeDefaultDescription
multiple?falsefalseIf true, several nodes are selected with checkboxes.
value?stringId of the selected node in controlled use; "" for none.
defaultValue?stringInitial value in uncontrolled use.
onValueChange?(value: string, node: TreeNode<T> | undefined) => voidCalled with the id and node when the selection changes ("" when cleared).
leavesOnly?booleanfalseWhether only leaves (nodes without children) can be selected. If true, clicking a parent node expands/collapses it.
itemsreadonly TreeNode<T>[]Nodes. See TreeNode; sub-branches can be loaded later with loadChildren.
loadChildren?(node: TreeNode<T>, context: { signal: AbortSignal; }) => Promise<readonly TreeNode<T>[] | undefined | void>hasChildren nodes fetch their children when expanded. See TreeView loadChildren.
search?boolean | "auto""auto"Search box in the panel. "auto": shows with more than 15 nodes. Insensitive to locale-specific letters and accents; non-matching branches are hidden.
placeholder?ReactNode"Seçiniz"Text shown while nothing is selected.
defaultExpanded?readonly string[] | "all"Initially expanded nodes; "all" expands all. If omitted, the path of selected nodes is expanded.
icon?IconPropIcon at the start of the box.
clearable?booleanfalse× button clearing the selection.
showPath?booleanfalseWhether the path of the selection is written in the box too. true: "Türkiye / İstanbul / Kadıköy"; false: only "Kadıköy".
pathSeparator?string" / "showPath separator.
variant?FieldVariant | CustomVariant<"field">Look. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
name?stringForm field name; the selected id(s) are submitted under this name.
form?stringId of the form the field belongs to.
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the panel is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
tooltip?TooltipPropTooltip when hovering the box. See TooltipProp.
className?stringClasses added to the container.
classNames?TreeSelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box.
aria-invalid?boolean | "true" | "false"Show as invalid.

Multi-select TreeSelect.

NameTypeDefaultDescription
multipletrueSeveral nodes are selected with checkboxes; the panel doesn't close on selection.
value?readonly string[]Checked ids in controlled use (see checkOutput).
defaultValue?readonly string[]Initial values in uncontrolled use.
onValueChange?(value: string[], nodes: TreeNode<T>[]) => voidCalled with the ids and nodes when the selection changes.
checkOutput?CheckOutput"leaves"Which nodes go into the value? "leaves": only leaves, "top": only the fully selected branch itself (if "Europe" is selected, not its countries), "all": all of them. See CheckOutput.
checkMode?"cascade" | "independent""cascade""cascade": a parent is linked to its children (tri-state). "independent": each node on its own.
maxDisplay?number2At most how many names are written in the box; the rest become "+3".
itemsreadonly TreeNode<T>[]Nodes. See TreeNode; sub-branches can be loaded later with loadChildren.
loadChildren?(node: TreeNode<T>, context: { signal: AbortSignal; }) => Promise<readonly TreeNode<T>[] | undefined | void>hasChildren nodes fetch their children when expanded. See TreeView loadChildren.
search?boolean | "auto""auto"Search box in the panel. "auto": shows with more than 15 nodes. Insensitive to locale-specific letters and accents; non-matching branches are hidden.
placeholder?ReactNode"Seçiniz"Text shown while nothing is selected.
defaultExpanded?readonly string[] | "all"Initially expanded nodes; "all" expands all. If omitted, the path of selected nodes is expanded.
icon?IconPropIcon at the start of the box.
clearable?booleanfalse× button clearing the selection.
showPath?booleanfalseWhether the path of the selection is written in the box too. true: "Türkiye / İstanbul / Kadıköy"; false: only "Kadıköy".
pathSeparator?string" / "showPath separator.
variant?FieldVariant | CustomVariant<"field">Look. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
name?stringForm field name; the selected id(s) are submitted under this name.
form?stringId of the form the field belongs to.
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the panel is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
tooltip?TooltipPropTooltip when hovering the box. See TooltipProp.
className?stringClasses added to the container.
classNames?TreeSelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box.
aria-invalid?boolean | "true" | "false"Show as invalid.

Props of the TreeSelect component.

type TreeSelectProps = SingleTreeSelectProps<T> | MultiTreeSelectProps<T>
NameTypeDefaultDescription
multiple?false | truefalseIf true, several nodes are selected with checkboxes.
value?string | readonly string[]Id of the selected node in controlled use; "" for none.
defaultValue?string | readonly string[]Initial value in uncontrolled use.
onValueChange?(value: string, node: TreeNode<T> | undefined) => void | (value: string[], nodes: TreeNode<T>[]) => voidCalled with the id and node when the selection changes ("" when cleared).
leavesOnly?booleanfalseWhether only leaves (nodes without children) can be selected. If true, clicking a parent node expands/collapses it.(SingleTreeSelectProps<T>)
itemsreadonly TreeNode<T>[]Nodes. See TreeNode; sub-branches can be loaded later with loadChildren.
loadChildren?(node: TreeNode<T>, context: { signal: AbortSignal; }) => Promise<readonly TreeNode<T>[] | undefined | void>hasChildren nodes fetch their children when expanded. See TreeView loadChildren.
search?boolean | "auto""auto"Search box in the panel. "auto": shows with more than 15 nodes. Insensitive to locale-specific letters and accents; non-matching branches are hidden.
placeholder?ReactNode"Seçiniz"Text shown while nothing is selected.
defaultExpanded?readonly string[] | "all"Initially expanded nodes; "all" expands all. If omitted, the path of selected nodes is expanded.
icon?IconPropIcon at the start of the box.
clearable?booleanfalse× button clearing the selection.
showPath?booleanfalseWhether the path of the selection is written in the box too. true: "Türkiye / İstanbul / Kadıköy"; false: only "Kadıköy".
pathSeparator?string" / "showPath separator.
variant?FieldVariant | CustomVariant<"field">Look. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
name?stringForm field name; the selected id(s) are submitted under this name.
form?stringId of the form the field belongs to.
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the panel is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
tooltip?TooltipPropTooltip when hovering the box. See TooltipProp.
className?stringClasses added to the container.
classNames?TreeSelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box.
aria-invalid?boolean | "true" | "false"Show as invalid.
checkOutput?CheckOutput"leaves"Which nodes go into the value? "leaves": only leaves, "top": only the fully selected branch itself (if "Europe" is selected, not its countries), "all": all of them. See CheckOutput.(MultiTreeSelectProps<T>)
checkMode?"cascade" | "independent""cascade""cascade": a parent is linked to its children (tri-state). "independent": each node on its own.(MultiTreeSelectProps<T>)
maxDisplay?number2At most how many names are written in the box; the rest become "+3".(MultiTreeSelectProps<T>)