esc
navigate select
Browse documentation

Utilities

TreeKit's rendering layer is a thin consumer of these pure functions — every one is exported so you can use the same tree math outside React, or build a fully custom renderer with useTree.

Building an index

PropTypeDefaultDescription
buildTreeIndex(data)TreeIndex<T>Walks the tree once, building id→node/parent/level maps and a flattened, pre-order list. Every other utility below takes this index rather than raw data, so you only pay traversal cost once.

Lookups

PropTypeDefaultDescription
findNode(index, id)TreeNode<T> | undefinedO(1) lookup by id.
findParent(index, id)TreeNode<T> | undefinedO(1) immediate parent lookup.
getAncestorIds(index, id)string[]Immediate parent → root, as ids.
getAncestors(index, id)TreeNode<T>[]Same, as full nodes.
getDescendantIds(node)string[]Every nested descendant id, any depth.
getDescendants(node)TreeNode<T>[]Same, as full nodes.
getLeafIds(node)string[]Ids of every leaf under node.
hasChildren(node)booleanTrue for a non-empty children array or hasChildren: true.

Visibility & search

PropTypeDefaultDescription
getVisibleEntries(index, expandedIds, visibleIds?)FlatTreeEntry<T>[]The rows that should currently render — roots plus any node whose full ancestor chain is expanded, optionally further restricted to a visibleIds set (used for search).
filterTreeIds(index, searchValue, filterFn?){ visibleIds, matchedIds }Computes which ids should stay visible for a search term, including ancestors of matches, without mutating the tree.
defaultFilterFn(node, value)booleanTreeKit's built-in case-insensitive label match.
collectAllIds(index)string[]Every node id in the tree.
collectExpandableIds(index)string[]Ids of every node that has children — the set you'd pass as expandedIds for an 'expand all'.

Selection & indeterminate state

PropTypeDefaultDescription
computeCheckedStates(index, checkedIds, propagation?)Map<string, CheckedState>Tri-state value for every node in one linear pass — the algorithm behind Indeterminate State.
toggleCheckedId(index, checkedIds, id, next, propagation?)Set<string>Returns the next checked-id set after toggling one node, applying cascade rules. Never mutates the input.
getCheckedNodeIds(states)string[]Filters a computeCheckedStates() map down to fully-checked ids.
getIndeterminateNodeIds(states)string[]Same, for indeterminate ids.
getNodesByIds(index, ids)TreeNode<T>[]Resolves an id list back to full node objects.
computeEffectiveDisabledIds(index, cascade?)Set<string>Every id that should be treated as disabled, including cascaded descendants.

Expansion

PropTypeDefaultDescription
toggleExpandedId(expandedIds, id, next)Set<string>Returns the next expanded-id set after toggling one node. Never mutates the input.
expandAllIds(index)Set<string>Every expandable node id, as a Set.
collapseAllIds()Set<string>Always an empty Set — provided for API symmetry with expandAllIds.

Async loading

PropTypeDefaultDescription
mergeLoadedChildren(nodes, loadedChildren)TreeNode<T>[]Merges a Map<id, TreeNode[]> of lazily-loaded children into a tree without mutating it, preserving object identity for untouched branches.

All pure, all unit tested

None of these functions read or write React state, and none mutate their inputs — every one returns a new value. This is deliberate: it's what makes them independently testable (see the package's test suite) and safe to call from outside a component, e.g. in a server-side script that needs to compute an "expand all" id list ahead of time.