TreeView
A hierarchical tree for displaying nested items such as files, folders, navigation, and categories.
Import TreeView
Import TreeView from Tavix:
import { TreeView } from "tavix";Component API
<TreeView
items={items}
defaultSelectedId
defaultExpandedIds
curved={false}
showIcons={false}
onSelect
/>Props
items: One tree item or an array of tree items to display.defaultSelectedId: Initially selected item ID.defaultExpandedIds: Initially expanded item IDs. Root nodes are expanded by default when omitted.curved: Changes connector styling. Defaults tofalse.showIcons: Renders item icons and changes the toggle treatment. Defaults tofalse.onSelect: Callback called with the selected item.
Item shape
{
id: "src",
label: "src",
icon: "folder",
children: [
{ id: "index", label: "index.js" },
],
}id: Unique identifier for the item.label: Text displayed for the item.icon: Optional Tavix icon name displayed whenshowIconsis enabled.children: Optional nested items.
Behavior
- Renders nested items from the
itemstree. - Supports one root item or an array of root items.
- Expands root nodes by default when
defaultExpandedIdsis omitted. - Uses
defaultExpandedIdsto set the initial expanded nodes. - Uses
defaultSelectedIdto set the initial selected item. - Manages selection and expansion internally after the initial values are applied.
- Calls
onSelectwith the selected item when a row is selected. - Renders item icons when
showIconsistrue. - Changes connector styling when
curvedistrue. - Does not expose controlled selected or expanded state props.
Examples
Basic tree
<TreeView
items={treeItems}
defaultSelectedId="src"
defaultExpandedIds={["src"]}
showIcons
onSelect={(item) => console.log(item)}
/>File tree
const treeItems = [
{
id: "src",
label: "src",
icon: "folder",
children: [
{ id: "app", label: "app.js", icon: "file" },
{ id: "styles", label: "styles.css", icon: "file" },
],
},
{
id: "readme",
label: "README.md",
icon: "file",
},
];
<TreeView items={treeItems} showIcons />;Selected and expanded items
<TreeView
items={treeItems}
defaultSelectedId="app"
defaultExpandedIds={["src"]}
showIcons
onSelect={(item) => {
console.log("Selected:", item.id);
}}
/>;Curved connectors
<TreeView
items={treeItems}
curved
showIcons
/>;Nested categories
<TreeView
items={[
{
id: "components",
label: "Components",
children: [
{
id: "forms",
label: "Forms",
children: [
{ id: "input", label: "Input" },
{ id: "select", label: "Select" },
],
},
],
},
]}
onSelect={(item) => console.log(item)}
/>;Selection and expansion
defaultSelectedId and defaultExpandedIds set the initial state. Selection and expansion are then managed internally; use onSelect to respond to selection changes.
Accessibility
- Use descriptive
labelvalues so each row is understandable. - Provide unique
idvalues for all items. - Use
onSelectto update surrounding context when a user selects an item. - The current implementation uses clickable rows without tree roles, keyboard handling, or expanded/selected ARIA state.
- Provide surrounding labels and context when stronger accessibility semantics are required.
- Do not rely on connector styling or icons alone to communicate hierarchy.
Notes
- Items can be nested through the
childrenproperty. - Root nodes are expanded by default when
defaultExpandedIdsis omitted. - Selection and expansion are internally controlled after initialization.
showIconschanges both item icon visibility and toggle treatment.curvedchanges connector styling.onSelectreceives the selected item object.