|
1
|
import { useCallback } from "react" |
|
2
|
import { FieldValues, Path, PathValue } from "react-hook-form" |
|
3
|
import { DataGridBulkUpdateCommand, DataGridMatrix } from "../models" |
|
4
|
import { DataGridCoordinates } from "../types" |
|
5
|
|
|
6
|
type UseDataGridMouseUpEventOptions<TData, TFieldValues extends FieldValues> = { |
|
7
|
matrix: DataGridMatrix<TData, TFieldValues> |
|
8
|
anchor: DataGridCoordinates | null |
|
9
|
dragEnd: DataGridCoordinates | null |
|
10
|
setDragEnd: (coords: DataGridCoordinates | null) => void |
|
11
|
setRangeEnd: (coords: DataGridCoordinates | null) => void |
|
12
|
setIsSelecting: (isSelecting: boolean) => void |
|
13
|
setIsDragging: (isDragging: boolean) => void |
|
14
|
getSelectionValues: ( |
|
15
|
fields: string[] |
|
16
|
) => PathValue<TFieldValues, Path<TFieldValues>>[] |
|
17
|
setSelectionValues: ( |
|
18
|
fields: string[], |
|
19
|
values: PathValue<TFieldValues, Path<TFieldValues>>[] |
|
20
|
) => void |
|
21
|
execute: (command: DataGridBulkUpdateCommand) => void |
|
22
|
isDragging: boolean |
|
23
|
} |
|
24
|
|
|
25
|
export const useDataGridMouseUpEvent = < |
|
26
|
TData, |
|
27
|
TFieldValues extends FieldValues |
|
28
|
>({ |
|
29
|
matrix, |
|
30
|
anchor, |
|
31
|
dragEnd, |
|
32
|
setDragEnd, |
|
33
|
isDragging, |
|
34
|
setIsDragging, |
|
35
|
setRangeEnd, |
|
36
|
setIsSelecting, |
|
37
|
getSelectionValues, |
|
38
|
setSelectionValues, |
|
39
|
execute, |
|
40
|
}: UseDataGridMouseUpEventOptions<TData, TFieldValues>) => { |
|
41
|
const handleDragEnd = useCallback(() => { |
|
42
|
if (!isDragging) { |
|
43
|
return |
|
44
|
} |
|
45
|
|
|
46
|
if (!anchor || !dragEnd) { |
|
47
|
return |
|
48
|
} |
|
49
|
const dragSelection = matrix.getFieldsInSelection(anchor, dragEnd) |
|
50
|
const anchorField = matrix.getCellField(anchor) |
|
51
|
|
|
52
|
if (!anchorField || !dragSelection.length) { |
|
53
|
return |
|
54
|
} |
|
55
|
|
|
56
|
const anchorValue = getSelectionValues([anchorField]) |
|
57
|
const fields = dragSelection.filter((field) => field !== anchorField) |
|
58
|
|
|
59
|
const prev = getSelectionValues(fields) |
|
60
|
const next = Array.from({ length: prev.length }, () => anchorValue[0]) |
|
61
|
|
|
62
|
const command = new DataGridBulkUpdateCommand({ |
|
63
|
fields, |
|
64
|
prev, |
|
65
|
next, |
|
66
|
setter: setSelectionValues, |
|
67
|
}) |
|
68
|
|
|
69
|
execute(command) |
|
70
|
|
|
71
|
setIsDragging(false) |
|
72
|
setDragEnd(null) |
|
73
|
|
|
74
|
setRangeEnd(dragEnd) |
|
75
|
}, [ |
|
76
|
isDragging, |
|
77
|
anchor, |
|
78
|
dragEnd, |
|
79
|
matrix, |
|
80
|
getSelectionValues, |
|
81
|
setSelectionValues, |
|
82
|
execute, |
|
83
|
setIsDragging, |
|
84
|
setDragEnd, |
|
85
|
setRangeEnd, |
|
86
|
]) |
|
87
|
|
|
88
|
const handleMouseUpEvent = useCallback(() => { |
|
89
|
handleDragEnd() |
|
90
|
setIsSelecting(false) |
|
91
|
}, [handleDragEnd, setIsSelecting]) |
|
92
|
|
|
93
|
return { |
|
94
|
handleMouseUpEvent, |
|
95
|
} |
|
96
|
} |