Managing the tree expand/collapse state

By default, the tree will be rendered with all nodes expanded. This is fine for basic use cases, but as soon as you go into more complex scenarios, you will want to control which nodes are expanded or collapsed.
This is easy to achieve via the defaultTreeExpandState prop. This is an uncontrolled prop and allows you to initially specify the expand/collapse state of the tree - all subsequent user updates will result in the tree state being updated to match the UI actions.
#
Specifying an initial tree expand state
open
loading demo…

Understanding the tree expand state#

You can specify the expand/collapse state of the tree in two ways:
  1. With node paths (recommended)
When using node paths, the object should have the following properties:
  • defaultExpanded: boolean - whether the tree nodes are expanded by default or not.
  • collapsedPaths: string[] - when defaultExpanded is true, this is a mandatory prop.
  • expandedPaths: string[] - when defaultExpanded is false, this is a mandatory prop.
Example of treeExpandState with node paths
const treeExpandState = {
  defaultExpanded: true,
  collapsedPaths: [
    ['1', '10'],
    ['2', '20'],
    ['5']
  ],
  expandedPaths: [
    ['1', '4'],
    ['5','nested node in 5'],
  ],
};
  1. With node ids
When using node ids, the object should have the following properties:
  • defaultExpanded: boolean - whether the tree nodes are expanded by default or not.
  • collapsedIds: string[] - when defaultExpanded is true, this is a mandatory prop.
  • expandedIds: string[] - when defaultExpanded is false, this is a mandatory prop.
Example of treeExpandState with node ids
const treeExpandState = {
  defaultExpanded: true,
  collapsedIds: ['1', '2', '5'],
  expandedIds: ['10', '20', 'nested node in 5'],
};

Reacting to user actions#

You can listen to the user interactions with the tree by using the onTreeExpandStateChange(treeExpandState, {dataSourceApi, nodePath, nodeState}) callback. This callback is called with the new tree state whenever the user expands or collapses a node.
In addition to this callback, you can also use the following:
The onNodeExpand and onNodeCollapse callbacks are called when a node is expanded or collapsed, respectively - either via user interaction or by an API call. However, they will not be called when the expandAll or collapseAll methods are called.

Using controlled expand/collapse state#

If you want maximum control over the collapse/expand state, you should use the controlled treeExpandState prop.
This will allow you to own the collapse/expand state entirely - but make sure you use the onTreeExpandStateChange callback to react to user actions or API calls being made to update the tree state.
#
Using controlled expand/collapse state
open
loading demo…
When using controlled treeExpandState, you no longer need to use API calls.
When you need to expand all nodes, simply set the treeExpandState to {defaultExpanded: true, collapsedPaths: []}.
When you need to collapse all nodes, simply set the treeExpandState to {defaultExpanded: false, expandedPaths: []}.