|
1
|
import { CellContext } from "@tanstack/react-table" |
|
2
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react" |
|
3
|
|
|
4
|
import { useDataGridContext } from "../context" |
|
5
|
import { |
|
6
|
DataGridCellContext, |
|
7
|
DataGridCellRenderProps, |
|
8
|
DataGridCoordinates, |
|
9
|
} from "../types" |
|
10
|
import { isCellMatch, isSpecialFocusKey } from "../utils" |
|
11
|
|
|
12
|
type UseDataGridCellOptions<TData, TValue> = { |
|
13
|
context: CellContext<TData, TValue> |
|
14
|
} |
|
15
|
|
|
16
|
const textCharacterRegex = /^.$/u |
|
17
|
const numberCharacterRegex = /^[0-9]$/u |
|
18
|
|
|
19
|
export const useDataGridCell = <TData, TValue>({ |
|
20
|
context, |
|
21
|
}: UseDataGridCellOptions<TData, TValue>) => { |
|
22
|
const { |
|
23
|
register, |
|
24
|
control, |
|
25
|
anchor, |
|
26
|
setIsEditing, |
|
27
|
setSingleRange, |
|
28
|
setIsSelecting, |
|
29
|
setRangeEnd, |
|
30
|
getWrapperFocusHandler, |
|
31
|
getWrapperMouseOverHandler, |
|
32
|
getInputChangeHandler, |
|
33
|
getIsCellSelected, |
|
34
|
getIsCellDragSelected, |
|
35
|
getCellMetadata, |
|
36
|
} = useDataGridContext() |
|
37
|
|
|
38
|
const { rowIndex, columnIndex } = context as DataGridCellContext< |
|
39
|
TData, |
|
40
|
TValue |
|
41
|
> |
|
42
|
|
|
43
|
const coords: DataGridCoordinates = useMemo( |
|
44
|
() => ({ row: rowIndex, col: columnIndex }), |
|
45
|
[rowIndex, columnIndex] |
|
46
|
) |
|
47
|
|
|
48
|
const { id, field, type, innerAttributes, inputAttributes } = useMemo(() => { |
|
49
|
return getCellMetadata(coords) |
|
50
|
}, [coords, getCellMetadata]) |
|
51
|
|
|
52
|
const [showOverlay, setShowOverlay] = useState(true) |
|
53
|
|
|
54
|
const containerRef = useRef<HTMLDivElement>(null) |
|
55
|
const inputRef = useRef<HTMLElement>(null) |
|
56
|
|
|
57
|
const handleOverlayMouseDown = useCallback( |
|
58
|
(e: React.MouseEvent) => { |
|
59
|
e.preventDefault() |
|
60
|
e.stopPropagation() |
|
61
|
|
|
62
|
if (e.detail === 2) { |
|
63
|
if (inputRef.current) { |
|
64
|
setShowOverlay(false) |
|
65
|
|
|
66
|
inputRef.current.focus() |
|
67
|
|
|
68
|
return |
|
69
|
} |
|
70
|
} |
|
71
|
|
|
72
|
if (e.shiftKey) { |
|
73
|
// Only allow setting the rangeEnd if the column matches the anchor column. |
|
74
|
// If not we let the function continue and treat the click as if the shift key was not pressed. |
|
75
|
if (coords.col === anchor?.col) { |
|
76
|
setRangeEnd(coords) |
|
77
|
return |
|
78
|
} |
|
79
|
} |
|
80
|
|
|
81
|
if (containerRef.current) { |
|
82
|
setSingleRange(coords) |
|
83
|
setIsSelecting(true) |
|
84
|
containerRef.current.focus() |
|
85
|
} |
|
86
|
}, |
|
87
|
[coords, anchor, setRangeEnd, setSingleRange, setIsSelecting] |
|
88
|
) |
|
89
|
|
|
90
|
const handleBooleanInnerMouseDown = useCallback( |
|
91
|
(e: React.MouseEvent<HTMLElement>) => { |
|
92
|
e.preventDefault() |
|
93
|
e.stopPropagation() |
|
94
|
|
|
95
|
if (e.detail === 2) { |
|
96
|
inputRef.current?.focus() |
|
97
|
return |
|
98
|
} |
|
99
|
|
|
100
|
if (e.shiftKey) { |
|
101
|
setRangeEnd(coords) |
|
102
|
return |
|
103
|
} |
|
104
|
|
|
105
|
if (containerRef.current) { |
|
106
|
setSingleRange(coords) |
|
107
|
setIsSelecting(true) |
|
108
|
containerRef.current.focus() |
|
109
|
} |
|
110
|
}, |
|
111
|
[setIsSelecting, setSingleRange, setRangeEnd, coords] |
|
112
|
) |
|
113
|
|
|
114
|
const handleInputBlur = useCallback(() => { |
|
115
|
setShowOverlay(true) |
|
116
|
setIsEditing(false) |
|
117
|
}, [setIsEditing]) |
|
118
|
|
|
119
|
const handleInputFocus = useCallback(() => { |
|
120
|
setShowOverlay(false) |
|
121
|
setIsEditing(true) |
|
122
|
}, [setIsEditing]) |
|
123
|
|
|
124
|
const validateKeyStroke = useCallback( |
|
125
|
(key: string) => { |
|
126
|
switch (type) { |
|
127
|
case "togglable-number": |
|
128
|
case "number": |
|
129
|
return numberCharacterRegex.test(key) |
|
130
|
case "text": |
|
131
|
return textCharacterRegex.test(key) |
|
132
|
default: |
|
133
|
// KeyboardEvents should not be forwareded to other types of cells |
|
134
|
return false |
|
135
|
} |
|
136
|
}, |
|
137
|
[type] |
|
138
|
) |
|
139
|
|
|
140
|
const handleContainerKeyDown = useCallback( |
|
141
|
(e: React.KeyboardEvent<HTMLDivElement>) => { |
|
142
|
if (!inputRef.current || !validateKeyStroke(e.key) || !showOverlay) { |
|
143
|
return |
|
144
|
} |
|
145
|
|
|
146
|
// Allow the user to undo/redo |
|
147
|
if (e.key.toLowerCase() === "z" && (e.ctrlKey || e.metaKey)) { |
|
148
|
return |
|
149
|
} |
|
150
|
|
|
151
|
// Allow the user to copy |
|
152
|
if (e.key.toLowerCase() === "c" && (e.ctrlKey || e.metaKey)) { |
|
153
|
return |
|
154
|
} |
|
155
|
|
|
156
|
// Allow the user to paste |
|
157
|
if (e.key.toLowerCase() === "v" && (e.ctrlKey || e.metaKey)) { |
|
158
|
return |
|
159
|
} |
|
160
|
|
|
161
|
if (e.key === "Enter") { |
|
162
|
return |
|
163
|
} |
|
164
|
|
|
165
|
if (isSpecialFocusKey(e.nativeEvent)) { |
|
166
|
return |
|
167
|
} |
|
168
|
|
|
169
|
inputRef.current.focus() |
|
170
|
setShowOverlay(false) |
|
171
|
|
|
172
|
if (inputRef.current instanceof HTMLInputElement) { |
|
173
|
// Clear the current value |
|
174
|
inputRef.current.value = "" |
|
175
|
|
|
176
|
// Simulate typing the new key |
|
177
|
const nativeInputValueSetter = Object.getOwnPropertyDescriptor( |
|
178
|
window.HTMLInputElement.prototype, |
|
179
|
"value" |
|
180
|
)?.set |
|
181
|
nativeInputValueSetter?.call(inputRef.current, e.key) |
|
182
|
|
|
183
|
// Trigger input event to notify react-hook-form |
|
184
|
const event = new Event("input", { bubbles: true }) |
|
185
|
inputRef.current.dispatchEvent(event) |
|
186
|
} |
|
187
|
|
|
188
|
// Prevent the original event from propagating |
|
189
|
e.stopPropagation() |
|
190
|
e.preventDefault() |
|
191
|
}, |
|
192
|
[showOverlay, validateKeyStroke] |
|
193
|
) |
|
194
|
|
|
195
|
const isAnchor = useMemo(() => { |
|
196
|
return anchor ? isCellMatch(coords, anchor) : false |
|
197
|
}, [anchor, coords]) |
|
198
|
|
|
199
|
const fieldWithoutOverlay = useMemo(() => { |
|
200
|
return type === "boolean" |
|
201
|
}, [type]) |
|
202
|
|
|
203
|
useEffect(() => { |
|
204
|
if (isAnchor && !containerRef.current?.contains(document.activeElement)) { |
|
205
|
containerRef.current?.focus() |
|
206
|
} |
|
207
|
}, [isAnchor]) |
|
208
|
|
|
209
|
const renderProps: DataGridCellRenderProps = { |
|
210
|
container: { |
|
211
|
field, |
|
212
|
isAnchor, |
|
213
|
isSelected: getIsCellSelected(coords), |
|
214
|
isDragSelected: getIsCellDragSelected(coords), |
|
215
|
showOverlay: fieldWithoutOverlay ? false : showOverlay, |
|
216
|
innerProps: { |
|
217
|
ref: containerRef, |
|
218
|
onMouseOver: getWrapperMouseOverHandler(coords), |
|
219
|
onMouseDown: |
|
220
|
type === "boolean" ? handleBooleanInnerMouseDown : undefined, |
|
221
|
onKeyDown: handleContainerKeyDown, |
|
222
|
onFocus: getWrapperFocusHandler(coords), |
|
223
|
...innerAttributes, |
|
224
|
}, |
|
225
|
overlayProps: { |
|
226
|
onMouseDown: handleOverlayMouseDown, |
|
227
|
}, |
|
228
|
}, |
|
229
|
input: { |
|
230
|
ref: inputRef, |
|
231
|
onBlur: handleInputBlur, |
|
232
|
onFocus: handleInputFocus, |
|
233
|
onChange: getInputChangeHandler(field), |
|
234
|
...inputAttributes, |
|
235
|
}, |
|
236
|
} |
|
237
|
|
|
238
|
return { |
|
239
|
id, |
|
240
|
field, |
|
241
|
register, |
|
242
|
control, |
|
243
|
renderProps, |
|
244
|
} |
|
245
|
} |