|
1
|
import { |
|
2
|
CellContext, |
|
3
|
ColumnDefTemplate, |
|
4
|
createColumnHelper, |
|
5
|
HeaderContext, |
|
6
|
} from "@tanstack/react-table" |
|
7
|
import { FieldValues } from "react-hook-form" |
|
8
|
|
|
9
|
import { DataGridColumnType, FieldFunction } from "../types" |
|
10
|
|
|
11
|
type DataGridHelperColumnsProps<TData, TFieldValues extends FieldValues> = { |
|
12
|
/** |
|
13
|
* The id of the column. |
|
14
|
*/ |
|
15
|
id: string |
|
16
|
/** |
|
17
|
* The name of the column, shown in the column visibility menu. |
|
18
|
*/ |
|
19
|
name?: string |
|
20
|
/** |
|
21
|
* The header template for the column. |
|
22
|
*/ |
|
23
|
header: ColumnDefTemplate<HeaderContext<TData, unknown>> | undefined |
|
24
|
/** |
|
25
|
* The cell template for the column. |
|
26
|
*/ |
|
27
|
cell: ColumnDefTemplate<CellContext<TData, unknown>> | undefined |
|
28
|
/** |
|
29
|
* Callback to set the field path for each cell in the column. |
|
30
|
* If a callback is not provided, or returns null, the cell will not be editable. |
|
31
|
*/ |
|
32
|
field?: FieldFunction<TData, TFieldValues> |
|
33
|
/** |
|
34
|
* Whether the column cannot be hidden by the user. |
|
35
|
* |
|
36
|
* @default false |
|
37
|
*/ |
|
38
|
disableHiding?: boolean |
|
39
|
} & ( |
|
40
|
| { |
|
41
|
field: FieldFunction<TData, TFieldValues> |
|
42
|
type: DataGridColumnType |
|
43
|
} |
|
44
|
| { field?: null | undefined; type?: never } |
|
45
|
) |
|
46
|
|
|
47
|
export function createDataGridHelper< |
|
48
|
TData, |
|
49
|
TFieldValues extends FieldValues |
|
50
|
>() { |
|
51
|
const columnHelper = createColumnHelper<TData>() |
|
52
|
|
|
53
|
return { |
|
54
|
column: ({ |
|
55
|
id, |
|
56
|
name, |
|
57
|
header, |
|
58
|
cell, |
|
59
|
disableHiding = false, |
|
60
|
field, |
|
61
|
type, |
|
62
|
}: DataGridHelperColumnsProps<TData, TFieldValues>) => |
|
63
|
columnHelper.display({ |
|
64
|
id, |
|
65
|
header, |
|
66
|
cell, |
|
67
|
enableHiding: !disableHiding, |
|
68
|
meta: { |
|
69
|
name, |
|
70
|
field, |
|
71
|
type, |
|
72
|
}, |
|
73
|
}), |
|
74
|
} |
|
75
|
} |