|
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
|
( |
|
35
|
coords: DataGridCoordinates, |
|
36
|
direction: "horizontal" | "vertical" | "both" |
|
37
|
) => { |
|
38
|
if (!anchor) { |
|
39
|
return |
|
40
|
} |
|
41
|
|
|
42
|
const { row, col } = coords |
|
43
|
const { row: anchorRow, col: anchorCol } = anchor |
|
44
|
|
|
45
|
const rowDirection = row >= anchorRow ? "down" : "up" |
|
46
|
const colDirection = col >= anchorCol ? "right" : "left" |
|
47
|
|
|
48
|
let toRow = rowDirection === "down" ? row + 1 : row - 1 |
|
49
|
if (visibleRows[toRow] === undefined) { |
|
50
|
toRow = row |
|
51
|
} |
|
52
|
|
|
53
|
let toCol = colDirection === "right" ? col + 1 : col - 1 |
|
54
|
if (visibleColumns[toCol] === undefined) { |
|
55
|
toCol = col |
|
56
|
} |
|
57
|
|
|
58
|
const scrollOptions: ScrollToOptions = { align: "auto", behavior: "auto" } |
|
59
|
|
|
60
|
if (direction === "horizontal" || direction === "both") { |
|
61
|
columnVirtualizer.scrollToIndex(toCol, scrollOptions) |
|
62
|
} |
|
63
|
|
|
64
|
if (direction === "vertical" || direction === "both") { |
|
65
|
rowVirtualizer.scrollToIndex(toRow, scrollOptions) |
|
66
|
} |
|
67
|
}, |
|
68
|
[anchor, columnVirtualizer, visibleRows, rowVirtualizer, visibleColumns] |
|
69
|
) |
|
70
|
|
|
71
|
const navigateToField = useCallback( |
|
72
|
(field: string) => { |
|
73
|
const coords = matrix.getCoordinatesByField(field) |
|
74
|
|
|
75
|
if (!coords) { |
|
76
|
return |
|
77
|
} |
|
78
|
|
|
79
|
const column = flatColumns[coords.col] |
|
80
|
|
|
81
|
// Ensure that the column is visible |
|
82
|
setColumnVisibility((prev) => { |
|
83
|
return { |
|
84
|
...prev, |
|
85
|
[column.id]: true, |
|
86
|
} |
|
87
|
}) |
|
88
|
|
|
89
|
requestAnimationFrame(() => { |
|
90
|
scrollToCoordinates(coords, "both") |
|
91
|
setSingleRange(coords) |
|
92
|
}) |
|
93
|
|
|
94
|
requestAnimationFrame(() => { |
|
95
|
const input = queryTool?.getInput(coords) |
|
96
|
|
|
97
|
if (input) { |
|
98
|
input.focus() |
|
99
|
} |
|
100
|
}) |
|
101
|
}, |
|
102
|
[ |
|
103
|
matrix, |
|
104
|
flatColumns, |
|
105
|
setColumnVisibility, |
|
106
|
scrollToCoordinates, |
|
107
|
setSingleRange, |
|
108
|
queryTool, |
|
109
|
] |
|
110
|
) |
|
111
|
|
|
112
|
return { |
|
113
|
scrollToCoordinates, |
|
114
|
navigateToField, |
|
115
|
} |
|
116
|
} |