Raw
1 import { Column, Row, VisibilityState } from "@tanstack/react-table"
2 import { ScrollToOptions, Virtualizer } from "@tanstack/react-virtual"
3 import { Dispatch, SetStateAction, useCallback } from "react"
4 import { FieldValues } from "react-hook-form"
5 import { DataGridMatrix, DataGridQueryTool } from "../models"
6 import { DataGridCoordinates } from "../types"
7
8 type UseDataGridNavigationOptions<TData, TFieldValues extends FieldValues> = {
9 matrix: DataGridMatrix<TData, TFieldValues>
10 anchor: DataGridCoordinates | null
11 visibleRows: Row<TData>[]
12 visibleColumns: Column<TData, unknown>[]
13 rowVirtualizer: Virtualizer<HTMLDivElement, Element>
14 columnVirtualizer: Virtualizer<HTMLDivElement, Element>
15 setColumnVisibility: Dispatch<SetStateAction<VisibilityState>>
16 flatColumns: Column<TData, unknown>[]
17 queryTool: DataGridQueryTool | null
18 setSingleRange: (coords: DataGridCoordinates | null) => void
19 }
20
21 export const useDataGridNavigation = <TData, TFieldValues extends FieldValues>({
22 matrix,
23 anchor,
24 visibleColumns,
25 visibleRows,
26 columnVirtualizer,
27 rowVirtualizer,
28 setColumnVisibility,
29 flatColumns,
30 queryTool,
31 setSingleRange,
32 }: UseDataGridNavigationOptions<TData, TFieldValues>) => {
33 const scrollToCoordinates = useCallback(
34 (coords: DataGridCoordinates, direction: "horizontal" | "vertical" | "both") => {
35 if (!anchor) {
36 return
37 }
38
39 const { row, col } = coords
40 const { row: anchorRow, col: anchorCol } = anchor
41
42 const rowDirection = row >= anchorRow ? "down" : "up"
43 const colDirection = col >= anchorCol ? "right" : "left"
44
45 let toRow = rowDirection === "down" ? row + 1 : row - 1
46 if (visibleRows[toRow] === undefined) {
47 toRow = row
48 }
49
50 let toCol = colDirection === "right" ? col + 1 : col - 1
51 if (visibleColumns[toCol] === undefined) {
52 toCol = col
53 }
54
55 const scrollOptions: ScrollToOptions = { align: "auto", behavior: "auto" }
56
57 if (direction === "horizontal" || direction === "both") {
58 columnVirtualizer.scrollToIndex(toCol, scrollOptions)
59 }
60
61 if (direction === "vertical" || direction === "both") {
62 rowVirtualizer.scrollToIndex(toRow, scrollOptions)
63 }
64 },
65 [anchor, columnVirtualizer, visibleRows, rowVirtualizer, visibleColumns]
66 )
67
68 const navigateToField = useCallback(
69 (field: string) => {
70 const coords = matrix.getCoordinatesByField(field)
71
72 if (!coords) {
73 return
74 }
75
76 const column = flatColumns[coords.col]
77
78 // Ensure that the column is visible
79 setColumnVisibility((prev) => {
80 return {
81 ...prev,
82 [column.id]: true,
83 }
84 })
85
86 requestAnimationFrame(() => {
87 scrollToCoordinates(coords, "both")
88 setSingleRange(coords)
89 })
90
91 requestAnimationFrame(() => {
92 const input = queryTool?.getInput(coords)
93
94 if (input) {
95 input.focus()
96 }
97 })
98 },
99 [
100 matrix,
101 flatColumns,
102 setColumnVisibility,
103 scrollToCoordinates,
104 setSingleRange,
105 queryTool,
106 ]
107 )
108
109 return {
110 scrollToCoordinates,
111 navigateToField,
112 }
113 }