Raw
1 import { FocusEvent, MouseEvent, useCallback } from "react"
2 import { FieldValues, UseFormSetValue } from "react-hook-form"
3 import { DataGridMatrix, DataGridUpdateCommand } from "../models"
4 import { DataGridCoordinates } from "../types"
5
6 type UseDataGridCellHandlersOptions<TData, TFieldValues extends FieldValues> = {
7 matrix: DataGridMatrix<TData, TFieldValues>
8 anchor: DataGridCoordinates | null
9 rangeEnd: DataGridCoordinates | null
10 setRangeEnd: (coords: DataGridCoordinates | null) => void
11 isSelecting: boolean
12 setIsSelecting: (isSelecting: boolean) => void
13 isDragging: boolean
14 setIsDragging: (isDragging: boolean) => void
15 setSingleRange: (coords: DataGridCoordinates) => void
16 dragEnd: DataGridCoordinates | null
17 setDragEnd: (coords: DataGridCoordinates | null) => void
18 setValue: UseFormSetValue<TFieldValues>
19 execute: (command: DataGridUpdateCommand) => void
20 multiColumnSelection?: boolean
21 }
22
23 export const useDataGridCellHandlers = <
24 TData,
25 TFieldValues extends FieldValues
26 >({
27 matrix,
28 anchor,
29 rangeEnd,
30 setRangeEnd,
31 isDragging,
32 setIsDragging,
33 isSelecting,
34 setIsSelecting,
35 setSingleRange,
36 dragEnd,
37 setDragEnd,
38 setValue,
39 execute,
40 multiColumnSelection,
41 }: UseDataGridCellHandlersOptions<TData, TFieldValues>) => {
42 const getWrapperFocusHandler = useCallback(
43 (coords: DataGridCoordinates) => {
44 return (_e: FocusEvent<HTMLElement>) => {
45 setSingleRange(coords)
46 }
47 },
48 [setSingleRange]
49 )
50
51 const getOverlayMouseDownHandler = useCallback(
52 (coords: DataGridCoordinates) => {
53 return (e: MouseEvent<HTMLElement>) => {
54 e.stopPropagation()
55 e.preventDefault()
56
57 if (e.shiftKey) {
58 setRangeEnd(coords)
59 return
60 }
61
62 setIsSelecting(true)
63
64 setSingleRange(coords)
65 }
66 },
67 [setIsSelecting, setRangeEnd, setSingleRange]
68 )
69
70 const getWrapperMouseOverHandler = useCallback(
71 (coords: DataGridCoordinates) => {
72 if (!isDragging && !isSelecting) {
73 return
74 }
75
76 return (_e: MouseEvent<HTMLElement>) => {
77 /**
78 * If the column is not the same as the anchor col,
79 * we don't want to select the cell. Unless multiColumnSelection is true.
80 */
81 if (anchor?.col !== coords.col && !multiColumnSelection) {
82 return
83 }
84
85 if (isSelecting) {
86 setRangeEnd(coords)
87 } else {
88 setDragEnd(coords)
89 }
90 }
91 },
92 [
93 anchor?.col,
94 isDragging,
95 isSelecting,
96 setDragEnd,
97 setRangeEnd,
98 multiColumnSelection,
99 ]
100 )
101
102 const getInputChangeHandler = useCallback(
103 // Using `any` here as the generic type of Path<TFieldValues> will
104 // not be inferred correctly.
105 (field: any) => {
106 return (next: any, prev: any) => {
107 const command = new DataGridUpdateCommand({
108 next,
109 prev,
110 setter: (value) => {
111 setValue(field, value, {
112 shouldDirty: true,
113 shouldTouch: true,
114 })
115 },
116 })
117
118 execute(command)
119 }
120 },
121 [setValue, execute]
122 )
123
124 const onDragToFillStart = useCallback(
125 (_e: MouseEvent<HTMLElement>) => {
126 setIsDragging(true)
127 },
128 [setIsDragging]
129 )
130
131 const getIsCellSelected = useCallback(
132 (cell: DataGridCoordinates | null) => {
133 if (!cell || !anchor || !rangeEnd) {
134 return false
135 }
136
137 return matrix.getIsCellSelected(cell, anchor, rangeEnd)
138 },
139 [anchor, rangeEnd, matrix]
140 )
141
142 const getIsCellDragSelected = useCallback(
143 (cell: DataGridCoordinates | null) => {
144 if (!cell || !anchor || !dragEnd) {
145 return false
146 }
147
148 return matrix.getIsCellSelected(cell, anchor, dragEnd)
149 },
150 [anchor, dragEnd, matrix]
151 )
152
153 return {
154 getWrapperFocusHandler,
155 getOverlayMouseDownHandler,
156 getWrapperMouseOverHandler,
157 getInputChangeHandler,
158 getIsCellSelected,
159 getIsCellDragSelected,
160 onDragToFillStart,
161 }
162 }