UNPKG

@1771technologies/lytenyte-pro

Version:

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

102 lines (101 loc) 4.38 kB
import { useTree } from "./source/use-tree.js"; import { useMemo } from "react"; import { useRowById } from "./source/use-row-by-id.js"; import { useControlledState } from "./source/use-controlled-state.js"; import { useFlattened } from "./source/use-flattened.js"; import { useEvent, useGlobalRefresh, useOnRowsSelected, usePiece, useRowByIndex, useRowIsSelected, useRows, useRowsBetween, useRowSelection, useRowSelectionState, useRowsSelected, useSortFn, } from "@1771technologies/lytenyte-core/internal"; import { useRowParents } from "./source/use-row-parents.js"; import { useRowLeafs } from "./source/use-row-leafs.js"; import { useRowChildren } from "./source/use-row-children.js"; import { useOnRowsUpdated } from "./source/use-on-rows-updated.js"; import { useRowSiblings } from "./source/use-row-siblings.js"; const emptyKey = []; export function useTreeDataSource(p) { const sortFn = useSortFn(p.sort); const state = useControlledState(p); const tree = useTree(p); const flat = useFlattened(tree, state.expandedFn, p.topData, p.botData, p.rowSelectionIdUniverseAdditions, p.rowSelectionIdUniverseSubtractions, sortFn); const top$ = usePiece(flat.topCount); const bot$ = usePiece(flat.botCount); const rowCount$ = usePiece(flat.rowCount); const maxDepth$ = usePiece(flat.maxDepth); const rows$ = usePiece(flat.rows); const rows = useRows(flat.rows); const globalSignal = useGlobalRefresh(); const rowById = useRowById(tree); const rowParents = useRowParents(tree); const idToSpec = useEvent((id) => { const node = tree.rowIdToNode.get(id); if (!node) return null; return { size: node.children.size, children: node.children }; }); const selectionState = useRowSelection(p.rowSelection, p.onRowSelectionChange, p.rowsIsolatedSelection ?? false, p.rowSelectKey ?? emptyKey, flat.idUniverse, flat.rootIds, flat.rootCount, globalSignal); const onRowsSelected = useOnRowsSelected(selectionState, idToSpec, rowParents, p.rowsIsolatedSelection ?? false, globalSignal); const rowIsSelected = useRowIsSelected(selectionState, rowParents, rowById); const { rowByIndex, rowInvalidate } = useRowByIndex(rows$, globalSignal, selectionState, rowParents); const rowsSelected = useRowsSelected(selectionState, tree.rowIdToNode, rowParents); const rowSelectionState = useRowSelectionState(selectionState); const rowsBetween = useRowsBetween({ current: flat.idToIndex }, rowByIndex); const rowLeafs = useRowLeafs(tree); const rowChildren = useRowChildren(tree); const rowSiblings = useRowSiblings(tree); const rowIdToIndex = usePiece(flat.idToIndex); const setExpansions = state.onExpansionsChange; const onRowsUpdated = useOnRowsUpdated(tree, p); const selection$ = usePiece(selectionState.rowSelectionsRaw); const source = useMemo(() => { const s = { rowByIndex, rowById, rowInvalidate, rowParents, rowIsSelected, rowsSelected, rowSelectionState, rowsBetween, rowLeafs, rowChildren, rowSiblings, rowIndexToRowId: (i) => s.rowByIndex(i).get()?.id ?? null, rowIdToRowIndex: (id) => rowIdToIndex.get().get(id) ?? null, rowUpdate: onRowsUpdated, rowGroupExpansionChange: (delta) => { setExpansions(delta); }, onViewChange: () => { }, useBottomCount: bot$.useValue, useRowCount: rowCount$.useValue, useTopCount: top$.useValue, useRows: rows.useValue, useMaxRowGroupDepth: maxDepth$.useValue, useSelectionState: selection$.useValue, onRowsSelected, onRowsUpdated, }; return s; }, [ bot$.useValue, maxDepth$.useValue, onRowsSelected, onRowsUpdated, rowById, rowByIndex, rowChildren, rowCount$.useValue, rowIdToIndex, rowInvalidate, rowIsSelected, rowLeafs, rowParents, rowSelectionState, rowSiblings, rows.useValue, rowsBetween, rowsSelected, selection$.useValue, setExpansions, top$.useValue, ]); return source; }