Examples
Single nested choice
Choose a node while preserving its complete parent hierarchy.
Single team selection
import { TreeSelect } from "@phuctech/ui/components/tree-select";
const nodes = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] }, { id: "design", label: "Design", children: [{ id: "product-design", label: "Product design" }] }];
export function Example() {
return <TreeSelect label="Team" nodes={nodes} defaultValue="frontend" />;
}
Multiple choices
Select several nodes without closing the popup after each choice.
Multiple team selection
import { TreeSelect } from "@phuctech/ui/components/tree-select";
const nodes = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] }, { id: "design", label: "Design" }];
export function Example() {
return <TreeSelect label="Teams to notify" nodes={nodes} multiple defaultValue={["frontend"]} />;
}
Callback search
The feature receives a debounced query and supplies the matching tree and any request states.
TreeSelect remote callback search
"use client";
import { useState } from "react";
import { TreeSelect } from "@phuctech/ui/components/tree-select";
import type { TreeNode } from "@phuctech/ui/components/tree";
const allNodes: readonly TreeNode[] = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] }, { id: "design", label: "Design", children: [{ id: "product-design", label: "Product design" }] }];
function filterNodes(nodes: readonly TreeNode[], query: string): readonly TreeNode[] {
if (!query) return allNodes;
return nodes.flatMap((node) => {
const children = node.children ? filterNodes(node.children, query) : [];
const matches = (node.textValue ?? (typeof node.label === "string" ? node.label : "")).toLowerCase().includes(query.toLowerCase());
return matches || children.length ? [{ ...node, children: children.length ? children : undefined }] : [];
});
}
export function Example() {
const [query, setQuery] = useState("");
const [nodes, setNodes] = useState(allNodes);
return <div className="grid gap-2">
<TreeSelect label="Team" nodes={nodes} query={query} onQueryChange={setQuery} onSearch={(nextQuery) => setNodes(filterNodes(allNodes, nextQuery.trim()))} defaultValue="frontend" />
<p className="text-xs text-muted-foreground">The feature supplies the filtered tree after the debounced callback.</p>
</div>;
}
Read-only and disabled
Keep the current value visible while distinguishing non-editable and unavailable states.
Selection is read-only.
TreeSelect read-only and disabled states
import { TreeSelect } from "@phuctech/ui/components/tree-select";
const nodes = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }] }, { id: "design", label: "Design" }];
export function Example() {
return <div className="grid gap-5">
<TreeSelect label="Read-only team" nodes={nodes} value="frontend" readOnly />
<TreeSelect label="Unavailable team" nodes={nodes} value="design" disabled />
</div>;
}
Try TreeSelect
Inspect the bounded popup and hierarchical selection.
TreeSelect property playground
import { TreeSelect } from "@phuctech/ui/components/tree-select";
import type { TreeNode } from "@phuctech/ui/components/tree";
const nodes: readonly TreeNode[] = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] }, { id: "design", label: "Design" }];
export function Example() {
return <TreeSelect label="Team" nodes={nodes} defaultValue="frontend" />;
}
Usage
import { TreeSelect } from "@phuctech/ui/components/tree-select";
<TreeSelect label="Team" nodes={teams} value={teamId} onValueChange={setTeamId} />Search and request states
Without onSearch, TreeSelect filters plain text and textValue locally while retaining matching ancestors. With onSearch, it debounces the callback between 300 and 500 ms (350 ms default); the feature supplies new nodes, loading, and error state. The feature must protect asynchronous results from races and can batch requests. It does not fetch data or virtualize a large tree.
API
Accessibility
The trigger is named from the visible label and current value. The popup contains a named Tree; the tree supports arrow, Home/End, Enter, and Space behavior and announces branch expansion and selection. The search field has a specific accessible name.