UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

42 lines (40 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDeepestNode = getDeepestNode; exports.getNodeAncestors = getNodeAncestors; exports.getNodeChildren = getNodeChildren; /* eslint-disable @typescript-eslint/no-loop-func */ function getNodeChildren(nodes, id, onlyOpenChildren = true) { const directChildren = nodes.filter(node => node.parentId === id && (!onlyOpenChildren || node.context?.open)); return directChildren.flatMap(child => [child, ...getNodeChildren(nodes, child.id, onlyOpenChildren)]); } function getDeepestNode(nodes, id) { let deepestNodeId; let maxDepth = -1; function findDeepest(nodeId, depth) { if (depth > maxDepth) { deepestNodeId = nodeId; maxDepth = depth; } const children = getNodeChildren(nodes, nodeId); children.forEach(child => { findDeepest(child.id, depth + 1); }); } findDeepest(id, 0); return nodes.find(node => node.id === deepestNodeId); } function getNodeAncestors(nodes, id) { let allAncestors = []; let currentParentId = nodes.find(node => node.id === id)?.parentId; while (currentParentId) { const currentNode = nodes.find(node => node.id === currentParentId); currentParentId = currentNode?.parentId; if (currentNode) { allAncestors = allAncestors.concat(currentNode); } } return allAncestors; }