Tree

Tree is used to display hierarchical data.


import { Tree } from 'primereact/tree';
         

Tree requires a collection of TreeNode instances as a value.

    
    <Tree value={nodes} className="w-full md:w-30rem" />
             

    Expanded state of nodes is managed programmatically with expandedKeys and onToggle properties.

      
      <div className="flex flex-wrap gap-2 mb-4">
          <Button type="button" icon="pi pi-plus" label="Expand All" onClick={expandAll} />
          <Button type="button" icon="pi pi-minus" label="Collapse All" onClick={collapseAll} />
      </div>
      
      <Tree value={nodes} expandedKeys={expandedKeys} onToggle={(e) => setExpandedKeys(e.value)} className="w-full md:w-30rem" />
               

      Single node selection is configured by setting selectionMode as single along with selectionKeys and onSelectionChange properties to manage the selection value binding.

        
        <Tree value={nodes} selectionMode="single" selectionKeys={selectedKey} 
            onSelectionChange={(e) => setSelectedKey(e.value)} className="w-full md:w-30rem" />
                 

        More than one node is selectable by setting selectionMode to multiple. By default in multiple selection mode, metaKey press (e.g. ) is necessary to add to existing selections however this can be configured with disabling the metaKeySelection property. Note that in touch enabled devices, Tree always ignores metaKey.

        In multiple selection mode, value binding should be a key-value pair where key is the node key and value is a boolean to indicate selection.

        
        {
            '0-0': true,
            '0-1-0': true
        }
                 
          
          <div className="flex align-items-center mb-4 gap-2">
              <InputSwitch inputId="input-metakey" checked={metaKey} onChange={(e) => setMetaKey(e.value)} />
              <label htmlFor="input-metakey">MetaKey</label>
          </div>
          <Tree value={nodes} metaKeySelection={metaKey} selectionMode="multiple" selectionKeys={selectedKeys} 
              onSelectionChange={(e) => setSelectedKeys(e.value)} className="w-full md:w-30rem" />
                   

          Selection of multiple nodes via checkboxes is enabled by configuring selectionMode as checkbox.

          In checkbox selection mode, value binding should be a key-value pair where key is the node key and value is an object that has checked and partialChecked properties to represent the checked state of a node.

          
          {
              '0-0': {
                  partialChecked: false,
                  checked: true
              }
          }
                   
            
            <Tree value={nodes} selectionMode="checkbox" selectionKeys={selectedKeys} 
                onSelectionChange={(e) => setSelectedKeys(e.value)} className="w-full md:w-30rem" />
                     

            An event is provided for each type of user interaction such as expand, collapse and selection.

              
              <Toast ref={toast} />
              <Tree value={nodes} selectionMode="single" selectionKeys={selectedNodeKey} onSelectionChange={(e) => setSelectedNodeKey(e.value)} 
                  onExpand={onExpand} onCollapse={onCollapse} onSelect={onSelect} onUnselect={onUnselect} className="w-full md:w-30rem" />
                       

              Lazy loading is useful when dealing with huge datasets, in this example nodes are dynamically loaded on demand using onExpand event.

                
                <Tree value={nodes} onExpand={loadOnExpand} loading={loading} className="w-full md:w-30rem" />
                         

                Custom node content instead of a node label is defined with the nodeTemplate property. The toggler can be customized with the togglerTemplate property.

                • Installation
                • Main Concepts
                
                <Tree value={nodes} nodeTemplate={nodeTemplate} togglerTemplate={togglerTemplate} className="w-full md:w-30rem" />
                         

                Nodes can be reordered with dragdrop using dragdropScope and onDragDrop properties. The dragdropScope defines a unique scope of the component so that other drag events do not intervene with the component whereas onDragDrop is a callback to update the new state after a drop.

                  
                  <Tree value={nodes} dragdropScope="demo" onDragDrop={(e) => setNodes(e.value)} className="w-full md:w-30rem" />
                           

                  Tree has exclusive integration with ContextMenu using contextMenuSelectionKey, onContextMenuSelectionChange and onContextMenu properties.

                    
                    <Toast ref={toast} />
                    
                    <ContextMenu model={menu} ref={cm} />
                    
                    <Tree value={nodes} expandedKeys={expandedKeys} onToggle={(e) => setExpandedKeys(e.value)} 
                        contextMenuSelectionKey={selectedNodeKey} onContextMenuSelectionChange={(e) => setSelectedNodeKey(e.value)} 
                        onContextMenu={(e) => cm.current.show(e.originalEvent)} className="w-full md:w-30rem" />
                             

                    Filtering is enabled by adding the filter property, by default label property of a node is used to compare against the value in the text field, in order to customize which field(s) should be used during search define filterBy property. In addition filterMode specifies the filtering strategy. In lenient mode when the query matches a node, children of the node are not searched further as all descendants of the node are included. On the other hand, in strict mode when the query matches a node, filtering continues on all descendants.

                        
                        <Tree value={nodes} filter filterMode="lenient" filterPlaceholder="Lenient Filter" className="w-full md:w-30rem" />
                        <Tree value={nodes} filter filterMode="strict" filterPlaceholder="Strict Filter" className="w-full md:w-30rem" />
                                 

                        Following is the list of structural style classes

                        NameElement
                        p-treeMain container element
                        p-tree-horizontalMain container element in horizontal mode
                        p-tree-containerContainer of nodes
                        p-treenodeA treenode element
                        p-treenode-contentContent of a treenode
                        p-treenode-togglerToggle element
                        p-treenode-toggler-iconToggle icon
                        p-treenode-iconIcon of a treenode
                        p-treenode-labelLabel of a treenode
                        p-treenode-childrenContainer element for node children
                        Accessibility guide documents the specification of this component based on WCAG guidelines, the implementation is in progress.

                        Screen Reader

                        Value to describe the component can either be provided with aria-labelledby or aria-label props. The root list element has a tree role whereas each list item has a treeitem role along with aria-label, aria-selected and aria-expanded attributes. In checkbox selection, aria-checked is used instead of aria-selected. The container element of a treenode has the group role. Checkbox and toggle icons are hidden from screen readers as their parent element with treeitem role and attributes are used instead for readers and keyboard support. The aria-setsize, aria-posinset and aria-level attributes are calculated implicitly and added to each treeitem.

                        Keyboard Support

                        KeyFunction
                        tabMoves focus to the first selected node when focus enters the component, if there is none then first element receives the focus. If focus is already inside the component, moves focus to the next focusable element in the page tab sequence.
                        shift + tabMoves focus to the last selected node when focus enters the component, if there is none then first element receives the focus. If focus is already inside the component, moves focus to the previous focusable element in the page tab sequence.
                        enterSelects the focused treenode.
                        spaceSelects the focused treenode.
                        down arrowMoves focus to the next treenode.
                        up arrowMoves focus to the previous treenode.
                        right arrowIf node is closed, opens the node otherwise moves focus to the first child node.
                        left arrowIf node is open, closes the node otherwise moves focus to the parent node.