Next Chapter Begins šŸš€Read the full story
styledData

Tree

Tree is used to display hierarchical data.

    preview

    Usage#

    import { Tree } from '@primereact/ui/tree';

    Tree requires a collection of TreeNode instances as its value. Each visible row is produced by Tree.Nodes through its render function and identified by uKey.

    <Tree.Root value={nodes}>
        <Tree.Nodes>
            {({ node }) => (
                <Tree.Node uKey={node.key}>
                    <Tree.Content>
                        <Tree.Toggle>
                            <Tree.ToggleIndicator />
                        </Tree.Toggle>
                        <Tree.Label />
                    </Tree.Content>
                </Tree.Node>
            )}
        </Tree.Nodes>
    </Tree.Root>

    Examples#

    Basic#

    A basic Tree displays a collection of TreeNode instances.

      basic-demo

      Content#

      Custom node content is rendered inside Tree.Content; the row payload exposes leaf and expanded so the right icon can be chosen.

        node-demo

        Toggle Indicator#

        Toggle indicators are customized with Tree.ToggleIndicator and its match property of expanded, collapsed or always.

          plus-minus-toggle-demo

          Controlled#

          Expansion state is managed programmatically with expandedKeys and onExpandedChange properties.

            expand-collapse-all-demo

            Selection#

            Single#

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

              single-selection-demo

              Multiple#

              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 the metaKeySelection property.

                multiple-selection-demo

                Checkbox#

                Selection of multiple nodes via checkboxes is composed with Tree.Selection inside Tree.Node. The render prop exposes isSelected, isPartiallySelected and toggle; the down/up tri-state cascade runs in the headless layer.

                  checkbox-selection-demo

                  Radio#

                  Use Tree.Selection with mode="radio" to keep a single selection sticky. Re-clicking the selected node does not deselect it.

                    radio-selection-demo

                    Select All#

                    Outside a Tree.Node, Tree.Selection exposes isAllSelected, isSomeSelected and toggleAll for an aggregate checkbox in Tree.Header.

                    Select All
                      select-all-demo

                      Keyboard#

                      Tree fully supports keyboard navigation. Arrow keys move focus across visible rows, Right and Left expand and collapse, Space and Enter toggle selection on the focused node.

                      ↑ ↓ navigate, → expand, ← collapse, Space select
                      Selected
                        keyboard-demo

                        Filter#

                        Filtering is composed with the Tree.Filter input and a derived value. The useTreeFilter hook covers the common case of label-based search and exposes the filtered nodes along with an expandedKeys map.

                          No nodes match ""
                          filter-demo

                          Lazy#

                          Lazy loading is useful when dealing with huge datasets. Placeholder nodes are marked with leaf: false and children are loaded on demand using the onExpand event. The loading flag on the node toggles a spinner inside Tree.Toggle.

                          lazy-demo

                          Loading#

                          Overlay#

                          Setting the loading property displays a mask over the tree. The mask content is provided through Tree.Loading.

                            loading-overlay-demo

                            Skeleton#

                            Placeholder Skeleton primitives are rendered inside Tree.Content while the dataset loads.

                            loading-skeleton-demo

                            Empty#

                            When value is empty, Tree.Empty is rendered in place of the rows.

                              No folders yet

                              Create your first folder to start building a tree.

                              empty-demo

                              Drag and Drop#

                              Single#

                              Nodes can be reordered within a tree by setting draggable and droppable. The onMove callback receives the updated tree value along with dragNode, dropNode, dropIndex and dropPosition. Drop targets render Tree.DropIndicator with position="before" and position="after" around Tree.Content.

                              • app
                              • layout.tsx
                              • page.tsx
                              • components
                              • Header.tsx
                              • Footer.tsx
                              • package.json
                              • README.md
                              drag-drop-single-demo

                              Multiple#

                              Nodes can be transferred between trees with draggableScope and droppableScope. The draggableScope tags the nodes that leave a tree; droppableScope declares which scopes a tree accepts. Both accept a string or an array of strings.

                              • app
                              • layout.tsx
                              • page.tsx
                              • .env.local
                              • next.config.js
                              • package.json
                              • README.md
                              • /etc
                                Drop here
                                drag-drop-multiple-demo

                                Sub-Components#

                                See the Primitive API for Tree.Root, Tree.Nodes, Tree.Node, Tree.Content, Tree.Toggle, Tree.ToggleIndicator, Tree.Selection, Tree.DropIndicator, Tree.Label, Tree.Filter, Tree.Header, Tree.Footer, Tree.Empty and Tree.Loading.

                                Hooks#

                                See the Headless API for useTree and useTreeNode.

                                Accessibility#

                                See Tree Primitive for WAI-ARIA compliance details and keyboard support.