-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
columnHelper.ts
88 lines (80 loc) 路 2.03 KB
/
columnHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
AccessorFn,
ColumnDef,
DisplayColumnDef,
GroupColumnDef,
IdentifiedColumnDef,
RowData,
} from './types'
import { DeepKeys, DeepValue } from './utils'
// type Person = {
// firstName: string
// lastName: string
// age: number
// visits: number
// status: string
// progress: number
// createdAt: Date
// nested: {
// foo: [
// {
// bar: 'bar'
// }
// ]
// bar: { subBar: boolean }[]
// baz: {
// foo: 'foo'
// bar: {
// baz: 'baz'
// }
// }
// }
// }
// const test: DeepKeys<Person> = 'nested.foo.0.bar'
// const test2: DeepKeys<Person> = 'nested.bar'
// const helper = createColumnHelper<Person>()
// helper.accessor('nested.foo', {
// cell: info => info.getValue(),
// })
// helper.accessor('nested.foo.0.bar', {
// cell: info => info.getValue(),
// })
// helper.accessor('nested.bar', {
// cell: info => info.getValue(),
// })
export type ColumnHelper<TData extends RowData> = {
accessor: <
TAccessor extends AccessorFn<TData> | DeepKeys<TData>,
TValue extends TAccessor extends AccessorFn<TData, infer TReturn>
? TReturn
: TAccessor extends DeepKeys<TData>
? DeepValue<TData, TAccessor>
: never,
>(
accessor: TAccessor,
column: TAccessor extends AccessorFn<TData>
? DisplayColumnDef<TData, TValue>
: IdentifiedColumnDef<TData, TValue>
) => ColumnDef<TData, TValue>
display: (column: DisplayColumnDef<TData>) => ColumnDef<TData, unknown>
group: (column: GroupColumnDef<TData>) => ColumnDef<TData, unknown>
}
export function createColumnHelper<
TData extends RowData,
>(): ColumnHelper<TData> {
return {
accessor: (accessor, column) => {
return typeof accessor === 'function'
? ({
...column,
accessorFn: accessor,
} as any)
: {
...column,
accessorKey: accessor,
}
},
display: column => column as ColumnDef<TData, unknown>,
group: column => column as ColumnDef<TData, unknown>,
}
}