The tree view component displays hierarchical data in a collapsible tree structure. It provides three distinct interaction modes (default, selectable items, and checkboxes), supports asynchronous loading of child nodes, and integrates with PatternFly's collection management patterns. This page documents the architecture, usage patterns, and internal mechanisms of the tree view system.
For information about other navigation components like expandable groups, see Navigation and Tabs. For general collection management patterns, see Collection Management.
The tree view system consists of two primary classes that form a parent-child relationship with recursive nesting capabilities.
Sources: components/src/main/java/org/patternfly/component/tree/TreeView.java78-430 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java106-671
The tree view supports three interaction modes defined by TreeViewType, each with distinct DOM structure and selection behavior.
| Type | Selection Mechanism | Toggle Behavior | Use Case |
|---|---|---|---|
default_ | Click node to select and toggle | Single click for both | Simple navigation trees |
selectableItems | Separate toggle button and text button | Independent toggle/select | Trees with actions on items |
checkboxes | Checkbox for selection, button for toggle | Multi-select with cascading | Forms with hierarchical choices |
Sources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java495-571
Tree view items follow the HasItems pattern, enabling recursive nesting where each TreeViewItem can contain child TreeViewItem instances.
TreeView maintains:
LinkedHashMap<String, TreeViewItem> items - Top-level items indexed by identifierTreeViewType type - Determines interaction mode and DOM structureSupplier<Element> icon and expandedIcon - Default icons for all itemsTreeViewItem maintains:
LinkedHashMap<String, TreeViewItem> items - Child itemsTreeViewItem parent - Reference to parent item (null for root-level)String identifier - Unique identifier within parent's collectionMap<String, Object> data - Component context for arbitrary data storageAsyncStatus status - Tracks async loading state: static_, pending, resolved, rejectedAsyncItems<TreeViewItem, TreeViewItem> asyncItems - Function returning Promise of child itemsSources: components/src/main/java/org/patternfly/component/tree/TreeView.java94-117 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java139-185
Tree view items undergo a two-phase initialization: construction followed by DOM finalization when attached to the parent tree.
The finishDOM(TreeView tv) method at TreeViewItem.java481-603 performs the following:
nodeElement, toggleElement, and textElement based on tv.typefinishDOM(tv) on all child itemsSources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java481-603
Tree view items support lazy loading of children via the AsyncItems pattern, enabling large hierarchies to load on-demand.
add(AsyncItems<TreeViewItem, TreeViewItem> items) at TreeViewItem.java202-206: Sets status to pending and stores the async functionload() at TreeViewItem.java346-384: Executes the async function and handles loading/error statesreset() at TreeViewItem.java386-395: Clears resolved/rejected items and resets to pending statereload() at TreeViewItem.java397-406: Calls reset() then load(), preserving expansion stateSources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java202-206 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java346-406
Selection behavior varies by tree view type, with checkboxes supporting cascading selection and indeterminate states.
The markSelected(TreeViewType type, boolean selected) method at TreeViewItem.java605-617 implements:
tabIndex=0, others get tabIndex=-1.pf-m-current classcheck(this, selected) which recursively sets all descendant checkboxesindeterminate(parent) which walks up the tree, setting parent checkboxes to:
checked if all children are checkedindeterminate if some (but not all) children are checkedunchecked if no children are checkedSources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java605-653
TreeView methods:
select(String identifier) / select(TreeViewItem item) at TreeView.java251-290select(TreeViewItem item, boolean selected, boolean fireEvent) - For default_ and selectableItems types, unselects all other items first (single selection)selectedItems() at TreeView.java292-300 - Returns list of all selected items by traversing the treeunselect() / unselect(boolean fireEvent) at TreeView.java302-308 - Deselects all itemsTreeViewItem methods:
selected() at TreeViewItem.java307-314 - Returns selection state based on checkbox checked state or .pf-m-current modifier presenceSources: components/src/main/java/org/patternfly/component/tree/TreeView.java251-308 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java307-314
Tree view items implement the Expandable interface, controlling visibility of child items and icon state.
The toggle element (chevron icon) is conditionally shown based on async status and item count:
Sources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java316-344 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java577-579 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java365-368
The tree view supports multiple event handlers for item lifecycle, selection, and expansion.
| Handler | Scope | Purpose | Method Signature |
|---|---|---|---|
AddItemHandler | TreeView, TreeViewItem | Fired when item added | onAdd(TreeView/TreeViewItem, TreeViewItem) |
UpdateItemHandler | TreeView, TreeViewItem | Fired when item updated | onUpdate(TreeView/TreeViewItem, old, new) |
RemoveItemHandler | TreeView, TreeViewItem | Fired when item removed | onRemove(TreeView/TreeViewItem, TreeViewItem) |
ToggleHandler | TreeView, TreeViewItem | Fired on expand/collapse | onToggle(Event, TreeViewItem, boolean) |
SelectHandler | TreeView | Fired on item selection (single) | onSelect(Event, TreeViewItem, boolean) |
MultiSelectHandler | TreeView | Fired on selection change (multi) | onSelect(Event, TreeView, List<TreeViewItem>) |
Sources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java495-571 components/src/main/java/org/patternfly/component/tree/TreeView.java263-289
The static traverseItems(HasItems<?, ?, TreeViewItem> items, Consumer<TreeViewItem> code) method at TreeView.java382-387 recursively visits all items in the tree, enabling operations like:
selectedItems())reset())unselect())Sources: components/src/main/java/org/patternfly/component/tree/TreeView.java382-387
Keyboard navigation is registered in the attach() lifecycle method at TreeView.java120-140 and routes to type-specific handlers.
handleKeys(KeyboardEvent event) at TreeView.java415-421 - For default_ typehandleKeysSelectable(KeyboardEvent event) at TreeView.java423-429 - For selectableItems and checkboxes typesBoth handlers check that the event target has the .pf-v6-c-tree-view__node class before processing.
Note: The keyboard navigation implementation appears incomplete in the current codebase, as the handler methods contain only target validation without key-specific logic.
Sources: components/src/main/java/org/patternfly/component/tree/TreeView.java120-147 components/src/main/java/org/patternfly/component/tree/TreeView.java415-429
Tree view items support custom data storage and flexible icon configuration.
TreeViewItem implements ComponentContext<HTMLLIElement, TreeViewItem>, providing:
store(String key, T value) at TreeViewItem.java264-267 - Stores arbitrary datahas(String key) at TreeViewItem.java413-415 - Checks if key existsget(String key) at TreeViewItem.java418-423 - Retrieves stored valueThis enables associating domain objects or metadata with tree items without subclassing.
Icons are applied in finishDOM() at TreeViewItem.java586-595:
icon, use it; otherwise call tv.icon.get()expandedIcon, use it; otherwise call tv.expandedIcon.get()Sources: components/src/main/java/org/patternfly/component/tree/TreeViewItem.java231-261 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java264-267 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java413-423 components/src/main/java/org/patternfly/component/tree/TreeViewItem.java586-595
Refresh this wiki