UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

64 lines (63 loc) 2.62 kB
import { useMemo } from "react"; import { getValidLeafs } from "./get-valid-leafs.js"; import { useSortFn } from "@1771technologies/lytenyte-core/internal"; export function useFlattenedGroups(root, agg, leafs, workingSet, sort, expandedFn, suppressLeafExpansion, ignoreIsLast = false) { const sortFallbackDim = useMemo(() => [{ dim: { id: "__ln_group__" } }], []); const sortFallback = useSortFn(sortFallbackDim); const flat = useMemo(() => { if (!root) return [null, 0]; const flatList = []; const ranges = []; let maxDepth = 0; function processRowsBetter(node, parent, start, isLast, depth = 0) { maxDepth = Math.max(depth + 1, maxDepth); const rows = nodeChildrenToRows(node, agg, leafs, workingSet, sort !== undefined ? sort : sortFallback, isLast && !ignoreIsLast); let offset = 0; for (let i = 0; i < rows.length; i++) { const row = rows[i]; const rowIndex = i + start + offset; if (row.kind === "leaf") { flatList.push(row); continue; } flatList.push(row); const notExpandable = suppressLeafExpansion && row.last; row.expandable = !notExpandable; if (!expandedFn(row.id, depth) || notExpandable) { row.expanded = false; continue; } row.expanded = true; offset += processRowsBetter(row.__children, row, rowIndex + 1, row.last, depth + 1); } ranges.push({ parent, start, end: offset + start + node.size }); return offset + node.size; } processRowsBetter(root.children, null, 0, root.maxDepth <= 1); return [flatList, maxDepth]; }, [agg, expandedFn, ignoreIsLast, leafs, root, sort, sortFallback, suppressLeafExpansion, workingSet]); return flat; } const nodeChildrenToRows = (root, agg, leafs, workingSet, sort, isLast) => { const values = root.values(); const rows = []; for (const v of values) { if (v.kind === "leaf") { rows.push(v.row); continue; } else { const row = v.row; if (row.__invalidate) { const data = agg ? agg(getValidLeafs(v, leafs, workingSet)) : {}; row.data = data; row.__invalidate = false; } rows.push(row); } } if (!isLast && sort) return rows.sort(sort); return rows; };