LyteNyte Grid logo for light mode. Links back to the documentation home page.
Components

Tree View

Use the Tree View component to render hierarchical data in an expandable and collapsible list.

LyteNyte Grid exports a TreeView component for tree-based UIs, such as tree set filters. TreeView is a specialized, single-column variant of the grid, with styling and capabilities augmented to match a standard tree view.

Since the TreeView component shares its implementation with LyteNyte Grid, using the TreeView component alongside LyteNyte Grid does not increase your application’s bundle size.

Basic Tree View

Import TreeView from LyteNyte Grid to get started. Pass an array of path-based objects to the items property. Each item must conform to the TreeViewItem interface shown below:

interface TreeViewItem {
readonly id: string;
readonly path: string[];
readonly name?: string;
}

The items property is a flat array. The path property on each item determines the resulting tree structure.

The demo below shows a basic tree view. Since TreeView is a LyteNyte Grid variant, it requires a sized container.

Tree View Implementation

Fork code on stack blitzFork code on code sandbox

The code for this tree view example is shown below. The items property is set to an array of values. You can select rows by clicking the checkbox next to each item.

<TreeView items={items} defaultExpansion={0} />

Tree Selection

The TreeView component always uses linked row selection. The selection behavior matches the standard LyteNyte Grid selection model. You can provide a ref to the TreeView component to access the TreeView API, which includes the rowsSelected method for retrieving selected items.

const ref = useRef<TreeViewAPI | null>(null);
useEffect(() => {
ref.rowsSelected();
}, []);
<TreeView items={items} ref={ref} />;

To control row selection manually, set the rowSelection property on the TreeView component. Provide an onRowSelectionChange callback to respond to selection updates.

Since row selection is represented as a linked tree, you must determine which rows are selected by traversing the selection tree. The demo below demonstrates this behavior by tracking which files in the tree are selected.

Tree Selection Implementation

Selected Rows

  • src / index.ts
Fork code on stack blitzFork code on code sandbox

The code below determines selection by traversing the tree and filtering items based on their selection state. This is one simple approach to determining selected rows. You should choose an approach that matches your application’s requirements.

const [rowsSelected, setSelection] = useState<Grid.T.RowSelectionLinked>({
kind: "linked",
selected: false,
children: new Map([
["src", { id: "src", children: new Map([["src-index", { id: "src-index", selected: true }]]) }],
]),
});
const selectedRows = useMemo(() => {
const selected = items.filter((item) => {
const path = item.path;
let node: Grid.T.RowSelectionLinked | Grid.T.RowSelectNode = rowsSelected;
let selection = node.selected;
// Find the closest matching node for the current path. Path IDs
// are joined using '/' in the TreeView. Once the node is found,
// check whether it is selected.
for (let i = 0; i < path.length; i++) {
const p = path.slice(0, i + 1).join("/");
const n: Grid.T.RowSelectNode | undefined = node.children?.get(p);
if (!n) break;
node = n;
if (node.selected != undefined) selection = node.selected;
}
if (node.children?.get(item.id)?.selected !== undefined)
selection = node.children.get(item.id)!.selected!;
return !!selection;
});
return selected;
}, [rowsSelected]);

Custom Tree View Rendering

You can customize the rendered output of the TreeView component by providing a render prop as the children value. This render prop receives a set of TreeViewChildParams, shown below, which you can use to render custom content.

export interface TreeViewChildParams<T extends TreeViewItem> {
readonly row: RowGroup | RowLeaf<T>;
readonly toggle: (b?: boolean) => void;
readonly selected: boolean;
readonly indeterminate: boolean;
readonly selectEnabled: boolean;
readonly select: (b?: boolean) => void;
readonly handleSelect: (params: { readonly target: EventTarget; readonly shiftKey: boolean }) => void;
}

Using the children render prop, you can add additional functionality to rows, such as item removal. The demo below demonstrates this behavior.

Custom Tree View Nodes

Fork code on stack blitzFork code on code sandbox

Tree View Properties

Prop

Next Steps

  • Column Manager: Manage the visibility and arrangement of grid columns.
  • Menu Button: Display a list of actions or options in an accessible dropdown menu.
  • Pill Manager: Create and manage grid interactions using label pills.