|
1
|
import { FocusEvent, MouseEvent, createContext } from "react" |
|
2
|
import { |
|
3
|
Control, |
|
4
|
FieldErrors, |
|
5
|
FieldValues, |
|
6
|
Path, |
|
7
|
UseFormRegister, |
|
8
|
} from "react-hook-form" |
|
9
|
import { CellErrorMetadata, CellMetadata, DataGridCoordinates } from "../types" |
|
10
|
|
|
11
|
export type DataGridContextType<TFieldValues extends FieldValues> = { |
|
12
|
// Grid state |
|
13
|
anchor: DataGridCoordinates | null |
|
14
|
trapActive: boolean |
|
15
|
setTrapActive: (value: boolean) => void |
|
16
|
errors: FieldErrors<TFieldValues> |
|
17
|
// Cell handlers |
|
18
|
getIsCellSelected: (coords: DataGridCoordinates) => boolean |
|
19
|
getIsCellDragSelected: (coords: DataGridCoordinates) => boolean |
|
20
|
// Grid handlers |
|
21
|
setIsEditing: (value: boolean) => void |
|
22
|
setIsSelecting: (value: boolean) => void |
|
23
|
setRangeEnd: (coords: DataGridCoordinates) => void |
|
24
|
setSingleRange: (coords: DataGridCoordinates) => void |
|
25
|
// Form state and handlers |
|
26
|
register: UseFormRegister<TFieldValues> |
|
27
|
control: Control<TFieldValues> |
|
28
|
getInputChangeHandler: ( |
|
29
|
field: Path<TFieldValues> |
|
30
|
) => (next: any, prev: any) => void |
|
31
|
// Wrapper handlers |
|
32
|
getWrapperFocusHandler: ( |
|
33
|
coordinates: DataGridCoordinates |
|
34
|
) => (e: FocusEvent<HTMLElement>) => void |
|
35
|
getWrapperMouseOverHandler: ( |
|
36
|
coordinates: DataGridCoordinates |
|
37
|
) => ((e: MouseEvent<HTMLElement>) => void) | undefined |
|
38
|
getCellMetadata: (coords: DataGridCoordinates) => CellMetadata |
|
39
|
getCellErrorMetadata: (coords: DataGridCoordinates) => CellErrorMetadata |
|
40
|
navigateToField: (field: string) => void |
|
41
|
} |
|
42
|
|
|
43
|
export const DataGridContext = createContext<DataGridContextType<any> | null>( |
|
44
|
null |
|
45
|
) |