UNPKG

@1771technologies/lytenyte-pro

Version:

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

35 lines (34 loc) 1.55 kB
import { equal } from "@1771technologies/lytenyte-shared"; import { useEffect, useRef } from "react"; import { dataRectFromCellPosition } from "../data-rect-from-cell-position.js"; import { adjustRectForRowAndCellSpan } from "../adjust-rect-for-row-and-cell-span.js"; export function useCellFocusChange(focusActive, excludeMarker, keepSelection, onCellSelectionChange, setCellSelectionAdditiveRects, api) { const focus = focusActive.useValue(); const prevRef = useRef(null); useEffect(() => { if (!focus) return; const prev = prevRef.current; if (equal(prev, focus)) return; prevRef.current = focus; if (focus.colIndex === 0 && excludeMarker) { return; } if (!keepSelection && focus?.kind !== "cell" && focus?.kind !== "full-width") { onCellSelectionChange([]); setCellSelectionAdditiveRects([]); } else if (focus.kind === "cell" || focus.kind === "full-width") { const rect = focus.kind === "full-width" ? { rowStart: focus.rowIndex, rowEnd: focus.rowIndex + 1, columnStart: focus.colIndex, columnEnd: focus.colIndex + 1, } : dataRectFromCellPosition(focus); onCellSelectionChange([adjustRectForRowAndCellSpan(api.cellRoot, rect)]); } }, [api, excludeMarker, focus, keepSelection, onCellSelectionChange, setCellSelectionAdditiveRects]); }