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

Tree View

The tree view component is a UI component that displays hierarchical data as an expand/collapse list.

LyteNyte Grid exports a TreeView component that you can use for various UI elements, such as tree set filters. The TreeView component is a specialized version of LyteNyte Grid that contains a single column. The styling and capabilities are augmented to match a standard tree view component.

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 the TreeView component from LyteNyte Grid to begin using it. The TreeView expects an array of path-based objects to be provided through 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 in action. As the TreeView component is a variant of LyteNyte Grid, it requires a sized container.

Tree View

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

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]);

Children Render Prop

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.

Node Deletion

Fork code on stack blitzFork code on code sandbox

Properties

Prop

Next Steps

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