@tanstack/table-core 8.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +112 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/cjs/aggregationTypes.js +130 -0
- package/build/cjs/aggregationTypes.js.map +1 -0
- package/build/cjs/core.js +545 -0
- package/build/cjs/core.js.map +1 -0
- package/build/cjs/createTable.js +65 -0
- package/build/cjs/createTable.js.map +1 -0
- package/build/cjs/features/ColumnSizing.js +316 -0
- package/build/cjs/features/ColumnSizing.js.map +1 -0
- package/build/cjs/features/Expanding.js +238 -0
- package/build/cjs/features/Expanding.js.map +1 -0
- package/build/cjs/features/Filters.js +413 -0
- package/build/cjs/features/Filters.js.map +1 -0
- package/build/cjs/features/Grouping.js +227 -0
- package/build/cjs/features/Grouping.js.map +1 -0
- package/build/cjs/features/Headers.js +630 -0
- package/build/cjs/features/Headers.js.map +1 -0
- package/build/cjs/features/Ordering.js +86 -0
- package/build/cjs/features/Ordering.js.map +1 -0
- package/build/cjs/features/Pagination.js +193 -0
- package/build/cjs/features/Pagination.js.map +1 -0
- package/build/cjs/features/Pinning.js +148 -0
- package/build/cjs/features/Pinning.js.map +1 -0
- package/build/cjs/features/RowSelection.js +525 -0
- package/build/cjs/features/RowSelection.js.map +1 -0
- package/build/cjs/features/Sorting.js +313 -0
- package/build/cjs/features/Sorting.js.map +1 -0
- package/build/cjs/features/Visibility.js +172 -0
- package/build/cjs/features/Visibility.js.map +1 -0
- package/build/cjs/filterTypes.js +172 -0
- package/build/cjs/filterTypes.js.map +1 -0
- package/build/cjs/index.js +75 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/sortTypes.js +122 -0
- package/build/cjs/sortTypes.js.map +1 -0
- package/build/cjs/types.js +22 -0
- package/build/cjs/types.js.map +1 -0
- package/build/cjs/utils/columnFilterRowsFn.js +131 -0
- package/build/cjs/utils/columnFilterRowsFn.js.map +1 -0
- package/build/cjs/utils/expandRowsFn.js +38 -0
- package/build/cjs/utils/expandRowsFn.js.map +1 -0
- package/build/cjs/utils/globalFilterRowsFn.js +101 -0
- package/build/cjs/utils/globalFilterRowsFn.js.map +1 -0
- package/build/cjs/utils/groupRowsFn.js +155 -0
- package/build/cjs/utils/groupRowsFn.js.map +1 -0
- package/build/cjs/utils/paginateRowsFn.js +44 -0
- package/build/cjs/utils/paginateRowsFn.js.map +1 -0
- package/build/cjs/utils/sortRowsFn.js +94 -0
- package/build/cjs/utils/sortRowsFn.js.map +1 -0
- package/build/cjs/utils.js +167 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +4689 -0
- package/build/esm/index.js.map +1 -0
- package/build/stats-html.html +2689 -0
- package/build/stats-react.json +939 -0
- package/build/types/aggregationTypes.d.ts +22 -0
- package/build/types/core.d.ts +105 -0
- package/build/types/createTable.d.ts +42 -0
- package/build/types/features/ColumnSizing.d.ts +75 -0
- package/build/types/features/Expanding.d.ts +50 -0
- package/build/types/features/Filters.d.ts +92 -0
- package/build/types/features/Grouping.d.ts +85 -0
- package/build/types/features/Headers.d.ts +43 -0
- package/build/types/features/Ordering.d.ts +21 -0
- package/build/types/features/Pagination.d.ts +41 -0
- package/build/types/features/Pinning.d.ts +41 -0
- package/build/types/features/RowSelection.d.ts +60 -0
- package/build/types/features/Sorting.d.ts +81 -0
- package/build/types/features/Visibility.d.ts +49 -0
- package/build/types/filterTypes.d.ts +50 -0
- package/build/types/index.d.ts +23 -0
- package/build/types/sortTypes.d.ts +18 -0
- package/build/types/types.d.ts +138 -0
- package/build/types/utils/columnFilterRowsFn.d.ts +2 -0
- package/build/types/utils/expandRowsFn.d.ts +2 -0
- package/build/types/utils/globalFilterRowsFn.d.ts +2 -0
- package/build/types/utils/groupRowsFn.d.ts +2 -0
- package/build/types/utils/paginateRowsFn.d.ts +2 -0
- package/build/types/utils/sortRowsFn.d.ts +2 -0
- package/build/types/utils.d.ts +23 -0
- package/build/umd/index.development.js +4728 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +12 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +40 -0
- package/src/.DS_Store +0 -0
- package/src/aggregationTypes.ts +115 -0
- package/src/core.tsx +763 -0
- package/src/createTable.tsx +137 -0
- package/src/features/ColumnSizing.ts +432 -0
- package/src/features/Expanding.ts +320 -0
- package/src/features/Filters.ts +567 -0
- package/src/features/Grouping.ts +363 -0
- package/src/features/Headers.ts +747 -0
- package/src/features/Ordering.ts +112 -0
- package/src/features/Pagination.ts +252 -0
- package/src/features/Pinning.ts +177 -0
- package/src/features/RowSelection.ts +655 -0
- package/src/features/Sorting.ts +460 -0
- package/src/features/Visibility.ts +242 -0
- package/src/filterTypes.ts +188 -0
- package/src/index.tsx +23 -0
- package/src/sortTypes.ts +147 -0
- package/src/types.ts +311 -0
- package/src/utils/columnFilterRowsFn.ts +113 -0
- package/src/utils/expandRowsFn.ts +30 -0
- package/src/utils/globalFilterRowsFn.ts +89 -0
- package/src/utils/groupRowsFn.ts +170 -0
- package/src/utils/paginateRowsFn.ts +28 -0
- package/src/utils/sortRowsFn.ts +95 -0
- package/src/utils.tsx +221 -0
package/src/core.tsx
ADDED
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
import {
|
|
2
|
+
flattenBy,
|
|
3
|
+
functionalUpdate,
|
|
4
|
+
propGetter,
|
|
5
|
+
memo,
|
|
6
|
+
flexRender,
|
|
7
|
+
RequiredKeys,
|
|
8
|
+
} from './utils'
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
Updater,
|
|
12
|
+
PropGetterValue,
|
|
13
|
+
Options,
|
|
14
|
+
TableState,
|
|
15
|
+
ColumnDef,
|
|
16
|
+
Row,
|
|
17
|
+
Column,
|
|
18
|
+
Cell,
|
|
19
|
+
Header,
|
|
20
|
+
AccessorFn,
|
|
21
|
+
HeaderRenderProps,
|
|
22
|
+
TableProps,
|
|
23
|
+
TableBodyProps,
|
|
24
|
+
PropGetter,
|
|
25
|
+
Getter,
|
|
26
|
+
RowProps,
|
|
27
|
+
CellProps,
|
|
28
|
+
TableInstance,
|
|
29
|
+
RowValues,
|
|
30
|
+
Renderable,
|
|
31
|
+
Please_use_the_create_table_column_utilities_to_define_columns,
|
|
32
|
+
PartialGenerics,
|
|
33
|
+
} from './types'
|
|
34
|
+
|
|
35
|
+
import { Visibility } from './features/Visibility'
|
|
36
|
+
import { Ordering } from './features/Ordering'
|
|
37
|
+
import { Pinning } from './features/Pinning'
|
|
38
|
+
import { Headers } from './features/Headers'
|
|
39
|
+
import { Filters } from './features/Filters'
|
|
40
|
+
import { Sorting } from './features/Sorting'
|
|
41
|
+
import { Grouping, GroupingCell } from './features/Grouping'
|
|
42
|
+
import { Expanding } from './features/Expanding'
|
|
43
|
+
import { ColumnSizing, defaultColumnSizing } from './features/ColumnSizing'
|
|
44
|
+
import { Pagination } from './features/Pagination'
|
|
45
|
+
import { RowSelection } from './features/RowSelection'
|
|
46
|
+
import { RowModel } from '.'
|
|
47
|
+
|
|
48
|
+
const features = [
|
|
49
|
+
Visibility,
|
|
50
|
+
Ordering,
|
|
51
|
+
Pinning,
|
|
52
|
+
Headers,
|
|
53
|
+
Filters,
|
|
54
|
+
Sorting,
|
|
55
|
+
Grouping,
|
|
56
|
+
Expanding,
|
|
57
|
+
ColumnSizing,
|
|
58
|
+
Pagination,
|
|
59
|
+
RowSelection,
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
export type CoreOptions<TGenerics extends PartialGenerics> = {
|
|
63
|
+
data: TGenerics['Row'][]
|
|
64
|
+
columns: ColumnDef<TGenerics>[]
|
|
65
|
+
state: Partial<TableState>
|
|
66
|
+
onStateChange: (updater: Updater<TableState>) => void
|
|
67
|
+
debugAll?: boolean
|
|
68
|
+
debugTable?: boolean
|
|
69
|
+
debugHeaders?: boolean
|
|
70
|
+
debugColumns?: boolean
|
|
71
|
+
debugRows?: boolean
|
|
72
|
+
defaultColumn?: Partial<ColumnDef<TGenerics>>
|
|
73
|
+
initialState?: Partial<TableState>
|
|
74
|
+
getSubRows?: (
|
|
75
|
+
originalRow: TGenerics['Row'],
|
|
76
|
+
index: number
|
|
77
|
+
) => TGenerics['Row'][]
|
|
78
|
+
getRowId?: (
|
|
79
|
+
originalRow: TGenerics['Row'],
|
|
80
|
+
index: number,
|
|
81
|
+
parent?: Row<TGenerics>
|
|
82
|
+
) => string
|
|
83
|
+
autoResetAll?: boolean
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type TableCore<TGenerics extends PartialGenerics> = {
|
|
87
|
+
initialState: TableState
|
|
88
|
+
reset: () => void
|
|
89
|
+
options: RequiredKeys<Options<TGenerics>, 'state'>
|
|
90
|
+
setOptions: (newOptions: Updater<Options<TGenerics>>) => void
|
|
91
|
+
getRowId: (
|
|
92
|
+
_: TGenerics['Row'],
|
|
93
|
+
index: number,
|
|
94
|
+
parent?: Row<TGenerics>
|
|
95
|
+
) => string
|
|
96
|
+
getState: () => TableState
|
|
97
|
+
setState: (updater: Updater<TableState>) => void
|
|
98
|
+
getDefaultColumn: () => Partial<ColumnDef<TGenerics>>
|
|
99
|
+
getColumnDefs: () => ColumnDef<TGenerics>[]
|
|
100
|
+
createColumn: (
|
|
101
|
+
columnDef: ColumnDef<TGenerics>,
|
|
102
|
+
depth: number,
|
|
103
|
+
parent?: Column<TGenerics>
|
|
104
|
+
) => Column<TGenerics>
|
|
105
|
+
getAllColumns: () => Column<TGenerics>[]
|
|
106
|
+
getAllFlatColumns: () => Column<TGenerics>[]
|
|
107
|
+
getAllFlatColumnsById: () => Record<string, Column<TGenerics>>
|
|
108
|
+
getAllLeafColumns: () => Column<TGenerics>[]
|
|
109
|
+
getColumn: (columnId: string) => Column<TGenerics>
|
|
110
|
+
getColumnWidth: (columnId: string) => number
|
|
111
|
+
getTotalWidth: () => number
|
|
112
|
+
createCell: (
|
|
113
|
+
row: Row<TGenerics>,
|
|
114
|
+
column: Column<TGenerics>,
|
|
115
|
+
value: any
|
|
116
|
+
) => Cell<TGenerics>
|
|
117
|
+
createRow: (
|
|
118
|
+
id: string,
|
|
119
|
+
original: TGenerics['Row'] | undefined,
|
|
120
|
+
rowIndex: number,
|
|
121
|
+
depth: number,
|
|
122
|
+
values: Record<string, any>
|
|
123
|
+
) => Row<TGenerics>
|
|
124
|
+
getCoreRowModel: () => RowModel<TGenerics>
|
|
125
|
+
getRowModel: () => RowModel<TGenerics>
|
|
126
|
+
getRow: (id: string) => Row<TGenerics>
|
|
127
|
+
getCell: (rowId: string, columnId: string) => Cell<TGenerics>
|
|
128
|
+
getTableProps: PropGetter<TableProps>
|
|
129
|
+
getTableBodyProps: PropGetter<TableBodyProps>
|
|
130
|
+
getRowProps: <TGetter extends Getter<RowProps>>(
|
|
131
|
+
rowId: string,
|
|
132
|
+
userProps?: TGetter
|
|
133
|
+
) => undefined | PropGetterValue<RowProps, TGetter>
|
|
134
|
+
getCellProps: <TGetter extends Getter<CellProps>>(
|
|
135
|
+
rowId: string,
|
|
136
|
+
columnId: string,
|
|
137
|
+
userProps?: TGetter
|
|
138
|
+
) => undefined | PropGetterValue<CellProps, TGetter>
|
|
139
|
+
getTableWidth: () => number
|
|
140
|
+
getLeftTableWidth: () => number
|
|
141
|
+
getCenterTableWidth: () => number
|
|
142
|
+
getRightTableWidth: () => number
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export type CoreRow<TGenerics extends PartialGenerics> = {
|
|
146
|
+
id: string
|
|
147
|
+
index: number
|
|
148
|
+
original?: TGenerics['Row']
|
|
149
|
+
depth: number
|
|
150
|
+
values: RowValues
|
|
151
|
+
leafRows: Row<TGenerics>[]
|
|
152
|
+
subRows: Row<TGenerics>[]
|
|
153
|
+
getRowProps: PropGetter<RowProps>
|
|
154
|
+
originalSubRows?: TGenerics['Row'][]
|
|
155
|
+
getAllCells: () => Cell<TGenerics>[]
|
|
156
|
+
getAllCellsByColumnId: () => Record<string, Cell<TGenerics>>
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type CoreColumnDef<TGenerics extends PartialGenerics> = {
|
|
160
|
+
id: string
|
|
161
|
+
accessorKey?: string & keyof TGenerics['Row']
|
|
162
|
+
accessorFn?: AccessorFn<TGenerics['Row']>
|
|
163
|
+
header?:
|
|
164
|
+
| string
|
|
165
|
+
| Renderable<{
|
|
166
|
+
instance: TableInstance<TGenerics>
|
|
167
|
+
header: Header<TGenerics>
|
|
168
|
+
column: Column<TGenerics>
|
|
169
|
+
}>
|
|
170
|
+
width?: number
|
|
171
|
+
minWidth?: number
|
|
172
|
+
maxWidth?: number
|
|
173
|
+
columns?: ColumnDef<TGenerics>[]
|
|
174
|
+
footer?: Renderable<{
|
|
175
|
+
instance: TableInstance<TGenerics>
|
|
176
|
+
header: Header<TGenerics>
|
|
177
|
+
column: Column<TGenerics>
|
|
178
|
+
}>
|
|
179
|
+
cell?: Renderable<{
|
|
180
|
+
instance: TableInstance<TGenerics>
|
|
181
|
+
row: Row<TGenerics>
|
|
182
|
+
column: Column<TGenerics>
|
|
183
|
+
cell: Cell<TGenerics>
|
|
184
|
+
value: TGenerics['Value']
|
|
185
|
+
}>
|
|
186
|
+
defaultIsVisible?: boolean
|
|
187
|
+
[Please_use_the_create_table_column_utilities_to_define_columns]: true
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export type CoreColumn<TGenerics extends PartialGenerics> = {
|
|
191
|
+
id: string
|
|
192
|
+
depth: number
|
|
193
|
+
accessorFn?: AccessorFn<TGenerics['Row']>
|
|
194
|
+
columnDef: ColumnDef<TGenerics>
|
|
195
|
+
getWidth: () => number
|
|
196
|
+
columns: Column<TGenerics>[]
|
|
197
|
+
parent?: Column<TGenerics>
|
|
198
|
+
getFlatColumns: () => Column<TGenerics>[]
|
|
199
|
+
getLeafColumns: () => Column<TGenerics>[]
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function createTableInstance<TGenerics extends PartialGenerics>(
|
|
203
|
+
options: Options<TGenerics>
|
|
204
|
+
): TableInstance<TGenerics> {
|
|
205
|
+
if (options.debugAll || options.debugTable) {
|
|
206
|
+
console.info('Creating React Table Instance...')
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let instance = {} as TableInstance<TGenerics>
|
|
210
|
+
|
|
211
|
+
let listeners: (() => void)[] = []
|
|
212
|
+
|
|
213
|
+
const defaultOptions = features.reduce((obj, feature) => {
|
|
214
|
+
return Object.assign(obj, (feature as any).getDefaultOptions?.(instance))
|
|
215
|
+
}, {})
|
|
216
|
+
|
|
217
|
+
const buildOptions = (options: Options<TGenerics>) => ({
|
|
218
|
+
...defaultOptions,
|
|
219
|
+
...options,
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
instance.options = buildOptions(options)
|
|
223
|
+
|
|
224
|
+
const initialState = {
|
|
225
|
+
...features.reduce((obj, feature) => {
|
|
226
|
+
return Object.assign(obj, (feature as any).getInitialState?.())
|
|
227
|
+
}, {}),
|
|
228
|
+
...(options.initialState ?? {}),
|
|
229
|
+
} as TableState
|
|
230
|
+
|
|
231
|
+
const finalInstance: TableInstance<TGenerics> = {
|
|
232
|
+
...instance,
|
|
233
|
+
...features.reduce((obj, feature) => {
|
|
234
|
+
return Object.assign(obj, (feature as any).getInstance?.(instance))
|
|
235
|
+
}, {}),
|
|
236
|
+
initialState,
|
|
237
|
+
reset: () => {
|
|
238
|
+
instance.setState(instance.initialState)
|
|
239
|
+
},
|
|
240
|
+
setOptions: updater => {
|
|
241
|
+
instance.options = buildOptions(
|
|
242
|
+
functionalUpdate(updater, instance.options)
|
|
243
|
+
)
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
getRowId: (_: TGenerics['Row'], index: number, parent?: Row<TGenerics>) =>
|
|
247
|
+
`${parent ? [parent.id, index].join('.') : index}`,
|
|
248
|
+
|
|
249
|
+
getState: () => {
|
|
250
|
+
return instance.options.state as TableState
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
setState: (updater: Updater<TableState>) => {
|
|
254
|
+
instance.options.onStateChange?.(updater)
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
getDefaultColumn: memo(
|
|
258
|
+
() => [instance.options.defaultColumn],
|
|
259
|
+
defaultColumn => {
|
|
260
|
+
defaultColumn = (defaultColumn ?? {}) as Partial<ColumnDef<TGenerics>>
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
header: (props: HeaderRenderProps<Header<TGenerics>>) =>
|
|
264
|
+
props.header.column.id,
|
|
265
|
+
footer: (props: HeaderRenderProps<Header<TGenerics>>) =>
|
|
266
|
+
props.header.column.id,
|
|
267
|
+
cell: ({ value = '' }: { value: any }): JSX.Element =>
|
|
268
|
+
typeof value === 'boolean' ? value.toString() : value,
|
|
269
|
+
...features.reduce((obj, feature) => {
|
|
270
|
+
return Object.assign(obj, (feature as any).getDefaultColumn?.())
|
|
271
|
+
}, {}),
|
|
272
|
+
...defaultColumn,
|
|
273
|
+
} as Partial<ColumnDef<TGenerics>>
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
277
|
+
key: 'getDefaultColumn',
|
|
278
|
+
}
|
|
279
|
+
),
|
|
280
|
+
|
|
281
|
+
getColumnDefs: () => instance.options.columns,
|
|
282
|
+
|
|
283
|
+
createColumn: (columnDef, depth: number, parent) => {
|
|
284
|
+
const defaultColumn = instance.getDefaultColumn()
|
|
285
|
+
|
|
286
|
+
let id =
|
|
287
|
+
columnDef.id ??
|
|
288
|
+
columnDef.accessorKey ??
|
|
289
|
+
(typeof columnDef.header === 'string' ? columnDef.header : undefined)
|
|
290
|
+
|
|
291
|
+
let accessorFn: AccessorFn<TGenerics['Row']> | undefined
|
|
292
|
+
|
|
293
|
+
if (columnDef.accessorFn) {
|
|
294
|
+
accessorFn = columnDef.accessorFn
|
|
295
|
+
} else if (columnDef.accessorKey) {
|
|
296
|
+
accessorFn = (originalRow?: TGenerics['Row']) =>
|
|
297
|
+
(originalRow as any)[columnDef.accessorKey]
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (!id) {
|
|
301
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
302
|
+
throw new Error(
|
|
303
|
+
columnDef.accessorFn
|
|
304
|
+
? `Columns require an id when using an accessorFn`
|
|
305
|
+
: `Columns require an id when using a non-string header`
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
throw new Error()
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
let column: CoreColumn<TGenerics> = {
|
|
312
|
+
...defaultColumnSizing,
|
|
313
|
+
...defaultColumn,
|
|
314
|
+
...columnDef,
|
|
315
|
+
id: `${id}`,
|
|
316
|
+
accessorFn,
|
|
317
|
+
parent: parent as any,
|
|
318
|
+
depth,
|
|
319
|
+
columnDef,
|
|
320
|
+
columns: [],
|
|
321
|
+
getWidth: () => instance.getColumnWidth(column.id),
|
|
322
|
+
getFlatColumns: memo(
|
|
323
|
+
() => [true],
|
|
324
|
+
() => {
|
|
325
|
+
return [
|
|
326
|
+
column as Column<TGenerics>,
|
|
327
|
+
...column.columns?.flatMap(d => d.getFlatColumns()),
|
|
328
|
+
]
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
key: 'column.getFlatColumns',
|
|
332
|
+
debug: () =>
|
|
333
|
+
instance.options.debugAll ?? instance.options.debugColumns,
|
|
334
|
+
}
|
|
335
|
+
),
|
|
336
|
+
getLeafColumns: memo(
|
|
337
|
+
() => [instance.getOrderColumnsFn()],
|
|
338
|
+
orderColumns => {
|
|
339
|
+
if (column.columns?.length) {
|
|
340
|
+
let leafColumns = column.columns.flatMap(column =>
|
|
341
|
+
column.getLeafColumns()
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
return orderColumns(leafColumns)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return [column as Column<TGenerics>]
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
key: 'column.getLeafColumns',
|
|
351
|
+
debug: () =>
|
|
352
|
+
instance.options.debugAll ?? instance.options.debugColumns,
|
|
353
|
+
}
|
|
354
|
+
),
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
column = features.reduce((obj, feature) => {
|
|
358
|
+
return Object.assign(
|
|
359
|
+
obj,
|
|
360
|
+
(feature as any).createColumn?.(column, instance)
|
|
361
|
+
)
|
|
362
|
+
}, column)
|
|
363
|
+
|
|
364
|
+
// Yes, we have to convert instance to uknown, because we know more than the compiler here.
|
|
365
|
+
return column as Column<TGenerics>
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
getAllColumns: memo(
|
|
369
|
+
() => [instance.getColumnDefs()],
|
|
370
|
+
columnDefs => {
|
|
371
|
+
const recurseColumns = (
|
|
372
|
+
columnDefs: ColumnDef<TGenerics>[],
|
|
373
|
+
parent?: Column<TGenerics>,
|
|
374
|
+
depth = 0
|
|
375
|
+
): Column<TGenerics>[] => {
|
|
376
|
+
return columnDefs.map(columnDef => {
|
|
377
|
+
const column = instance.createColumn(columnDef, depth, parent)
|
|
378
|
+
|
|
379
|
+
column.columns = columnDef.columns
|
|
380
|
+
? recurseColumns(columnDef.columns, column, depth + 1)
|
|
381
|
+
: []
|
|
382
|
+
|
|
383
|
+
return column
|
|
384
|
+
})
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return recurseColumns(columnDefs)
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
key: 'getAllColumns',
|
|
391
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
392
|
+
}
|
|
393
|
+
),
|
|
394
|
+
|
|
395
|
+
getAllFlatColumns: memo(
|
|
396
|
+
() => [instance.getAllColumns()],
|
|
397
|
+
allColumns => {
|
|
398
|
+
return allColumns.flatMap(column => {
|
|
399
|
+
return column.getFlatColumns()
|
|
400
|
+
})
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
key: 'getAllFlatColumns',
|
|
404
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
405
|
+
}
|
|
406
|
+
),
|
|
407
|
+
|
|
408
|
+
getAllFlatColumnsById: memo(
|
|
409
|
+
() => [instance.getAllFlatColumns()],
|
|
410
|
+
flatColumns => {
|
|
411
|
+
return flatColumns.reduce((acc, column) => {
|
|
412
|
+
acc[column.id] = column
|
|
413
|
+
return acc
|
|
414
|
+
}, {} as Record<string, Column<TGenerics>>)
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
key: 'getAllFlatColumnsById',
|
|
418
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
419
|
+
}
|
|
420
|
+
),
|
|
421
|
+
|
|
422
|
+
getAllLeafColumns: memo(
|
|
423
|
+
() => [instance.getAllColumns(), instance.getOrderColumnsFn()],
|
|
424
|
+
(allColumns, orderColumns) => {
|
|
425
|
+
let leafColumns = allColumns.flatMap(column => column.getLeafColumns())
|
|
426
|
+
return orderColumns(leafColumns)
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
key: 'getAllLeafColumns',
|
|
430
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
431
|
+
}
|
|
432
|
+
),
|
|
433
|
+
|
|
434
|
+
getColumn: columnId => {
|
|
435
|
+
const column = instance.getAllFlatColumnsById()[columnId]
|
|
436
|
+
|
|
437
|
+
if (!column) {
|
|
438
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
439
|
+
console.warn(
|
|
440
|
+
`[React Table] Column with id ${columnId} does not exist.`
|
|
441
|
+
)
|
|
442
|
+
}
|
|
443
|
+
throw new Error()
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return column
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
getColumnWidth: (columnId: string) => {
|
|
450
|
+
const column = instance.getColumn(columnId)
|
|
451
|
+
|
|
452
|
+
if (!column) {
|
|
453
|
+
throw new Error()
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const columnSize = instance.getState().columnSizing[column.id]
|
|
457
|
+
|
|
458
|
+
return Math.min(
|
|
459
|
+
Math.max(
|
|
460
|
+
column.minWidth ?? defaultColumnSizing.minWidth,
|
|
461
|
+
columnSize ?? column.width ?? defaultColumnSizing.width
|
|
462
|
+
),
|
|
463
|
+
column.maxWidth ?? defaultColumnSizing.maxWidth
|
|
464
|
+
)
|
|
465
|
+
},
|
|
466
|
+
|
|
467
|
+
createCell: (row, column, value) => {
|
|
468
|
+
const cell: Cell<TGenerics> = {
|
|
469
|
+
id: `${row.id}_${column.id}`,
|
|
470
|
+
rowId: row.id,
|
|
471
|
+
columnId: column.id,
|
|
472
|
+
row,
|
|
473
|
+
column,
|
|
474
|
+
value,
|
|
475
|
+
getCellProps: userProps =>
|
|
476
|
+
instance.getCellProps(row.id, column.id, userProps)!,
|
|
477
|
+
renderCell: () =>
|
|
478
|
+
flexRender(column.cell, { instance, column, row, cell, value }),
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
features.forEach(feature => {
|
|
482
|
+
Object.assign(
|
|
483
|
+
cell,
|
|
484
|
+
(feature as any).createCell?.(
|
|
485
|
+
cell as Cell<TGenerics> & GroupingCell,
|
|
486
|
+
column,
|
|
487
|
+
row as Row<TGenerics>,
|
|
488
|
+
instance
|
|
489
|
+
)
|
|
490
|
+
)
|
|
491
|
+
}, {})
|
|
492
|
+
|
|
493
|
+
return cell
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
createRow: (id, original, rowIndex, depth, values) => {
|
|
497
|
+
let row: CoreRow<TGenerics> = {
|
|
498
|
+
id,
|
|
499
|
+
index: rowIndex,
|
|
500
|
+
original,
|
|
501
|
+
depth,
|
|
502
|
+
values,
|
|
503
|
+
subRows: [],
|
|
504
|
+
leafRows: [],
|
|
505
|
+
getRowProps: userProps => instance.getRowProps(row.id, userProps)!,
|
|
506
|
+
getAllCells: undefined!,
|
|
507
|
+
getAllCellsByColumnId: undefined!,
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
row.getAllCells = memo(
|
|
511
|
+
() => [instance.getAllLeafColumns()],
|
|
512
|
+
leafColumns => {
|
|
513
|
+
return leafColumns.map(column => {
|
|
514
|
+
return instance.createCell(
|
|
515
|
+
row as Row<TGenerics>,
|
|
516
|
+
column,
|
|
517
|
+
row.values[column.id]
|
|
518
|
+
)
|
|
519
|
+
})
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
key: process.env.NODE_ENV !== 'production' ? 'row.getAllCells' : '',
|
|
523
|
+
debug: () => instance.options.debugAll ?? instance.options.debugRows,
|
|
524
|
+
}
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
row.getAllCellsByColumnId = memo(
|
|
528
|
+
() => [row.getAllCells()],
|
|
529
|
+
allCells => {
|
|
530
|
+
return allCells.reduce((acc, cell) => {
|
|
531
|
+
acc[cell.columnId] = cell
|
|
532
|
+
return acc
|
|
533
|
+
}, {} as Record<string, Cell<TGenerics>>)
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
key: 'row.getAllCellsByColumnId',
|
|
537
|
+
debug: () => instance.options.debugAll ?? instance.options.debugRows,
|
|
538
|
+
}
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
for (let i = 0; i < features.length; i++) {
|
|
542
|
+
const feature = features[i]
|
|
543
|
+
Object.assign(row, (feature as any).createRow?.(row, instance))
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return row as Row<TGenerics>
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
getCoreRowModel: memo(
|
|
550
|
+
() => [instance.options.data],
|
|
551
|
+
(
|
|
552
|
+
data
|
|
553
|
+
): {
|
|
554
|
+
rows: Row<TGenerics>[]
|
|
555
|
+
flatRows: Row<TGenerics>[]
|
|
556
|
+
rowsById: Record<string, Row<TGenerics>>
|
|
557
|
+
} => {
|
|
558
|
+
// Access the row model using initial columns
|
|
559
|
+
const rows: Row<TGenerics>[] = []
|
|
560
|
+
const flatRows: Row<TGenerics>[] = []
|
|
561
|
+
const rowsById: Record<string, Row<TGenerics>> = {}
|
|
562
|
+
|
|
563
|
+
const leafColumns = instance.getAllLeafColumns()
|
|
564
|
+
|
|
565
|
+
const accessRow = (
|
|
566
|
+
originalRow: TGenerics['Row'],
|
|
567
|
+
rowIndex: number,
|
|
568
|
+
depth = 0,
|
|
569
|
+
parentRows: Row<TGenerics>[],
|
|
570
|
+
parent?: Row<TGenerics>
|
|
571
|
+
) => {
|
|
572
|
+
const id = instance.getRowId(originalRow, rowIndex, parent)
|
|
573
|
+
|
|
574
|
+
if (!id) {
|
|
575
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
576
|
+
throw new Error(`getRowId expected an ID, but got ${id}`)
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const values: Record<string, any> = {}
|
|
581
|
+
|
|
582
|
+
for (let i = 0; i < leafColumns.length; i++) {
|
|
583
|
+
const column = leafColumns[i]
|
|
584
|
+
if (column && column.accessorFn) {
|
|
585
|
+
values[column.id] = column.accessorFn(originalRow, rowIndex)
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Make the row
|
|
590
|
+
const row = instance.createRow(
|
|
591
|
+
id,
|
|
592
|
+
originalRow,
|
|
593
|
+
rowIndex,
|
|
594
|
+
depth,
|
|
595
|
+
values
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
// Push instance row into the parentRows array
|
|
599
|
+
parentRows.push(row)
|
|
600
|
+
// Keep track of every row in a flat array
|
|
601
|
+
flatRows.push(row)
|
|
602
|
+
// Also keep track of every row by its ID
|
|
603
|
+
rowsById[id] = row
|
|
604
|
+
|
|
605
|
+
// Get the original subrows
|
|
606
|
+
if (instance.options.getSubRows) {
|
|
607
|
+
const originalSubRows = instance.options.getSubRows(
|
|
608
|
+
originalRow,
|
|
609
|
+
rowIndex
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
// Then recursively access them
|
|
613
|
+
if (originalSubRows?.length) {
|
|
614
|
+
row.originalSubRows = originalSubRows
|
|
615
|
+
const subRows: Row<TGenerics>[] = []
|
|
616
|
+
|
|
617
|
+
for (let i = 0; i < row.originalSubRows.length; i++) {
|
|
618
|
+
accessRow(
|
|
619
|
+
row.originalSubRows[i] as TGenerics['Row'],
|
|
620
|
+
i,
|
|
621
|
+
depth + 1,
|
|
622
|
+
subRows,
|
|
623
|
+
row
|
|
624
|
+
)
|
|
625
|
+
}
|
|
626
|
+
// Keep the new subRows array on the row
|
|
627
|
+
row.subRows = subRows
|
|
628
|
+
row.leafRows = flattenBy(subRows, d => d.leafRows)
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
for (let i = 0; i < data.length; i++) {
|
|
634
|
+
accessRow(data[i] as TGenerics['Row'], i, 0, rows)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
return { rows, flatRows, rowsById }
|
|
638
|
+
},
|
|
639
|
+
{
|
|
640
|
+
key: 'getRowModel',
|
|
641
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
642
|
+
onChange: () => {
|
|
643
|
+
instance._notifyRowSelectionReset()
|
|
644
|
+
instance._notifyFiltersReset()
|
|
645
|
+
},
|
|
646
|
+
}
|
|
647
|
+
),
|
|
648
|
+
|
|
649
|
+
// The final calls start at the bottom of the model,
|
|
650
|
+
// expanded rows, which then work their way up
|
|
651
|
+
|
|
652
|
+
getRowModel: () => {
|
|
653
|
+
return instance.getPaginationRowModel()
|
|
654
|
+
},
|
|
655
|
+
|
|
656
|
+
getRow: (id: string) => {
|
|
657
|
+
const row = instance.getRowModel().rowsById[id]
|
|
658
|
+
|
|
659
|
+
if (!row) {
|
|
660
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
661
|
+
throw new Error(`getRow expected an ID, but got ${id}`)
|
|
662
|
+
}
|
|
663
|
+
throw new Error()
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
return row
|
|
667
|
+
},
|
|
668
|
+
|
|
669
|
+
getCell: (rowId: string, columnId: string) => {
|
|
670
|
+
const row = instance.getRow(rowId)
|
|
671
|
+
|
|
672
|
+
if (!row) {
|
|
673
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
674
|
+
throw new Error(`[React Table] could not find row with id ${rowId}`)
|
|
675
|
+
}
|
|
676
|
+
throw new Error()
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const cell = row.getAllCellsByColumnId()[columnId]
|
|
680
|
+
|
|
681
|
+
if (!cell) {
|
|
682
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
683
|
+
throw new Error(
|
|
684
|
+
`[React Table] could not find cell ${columnId} in row ${rowId}`
|
|
685
|
+
)
|
|
686
|
+
}
|
|
687
|
+
throw new Error()
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
return cell
|
|
691
|
+
},
|
|
692
|
+
|
|
693
|
+
getTableProps: userProps => {
|
|
694
|
+
return propGetter(
|
|
695
|
+
{
|
|
696
|
+
role: 'table',
|
|
697
|
+
},
|
|
698
|
+
userProps
|
|
699
|
+
)
|
|
700
|
+
},
|
|
701
|
+
|
|
702
|
+
getTableBodyProps: userProps => {
|
|
703
|
+
return propGetter(
|
|
704
|
+
{
|
|
705
|
+
role: 'rowgroup',
|
|
706
|
+
},
|
|
707
|
+
userProps
|
|
708
|
+
)
|
|
709
|
+
},
|
|
710
|
+
|
|
711
|
+
getRowProps: (rowId, userProps) => {
|
|
712
|
+
const row = instance.getRow(rowId)
|
|
713
|
+
if (!row) {
|
|
714
|
+
return
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
return propGetter(
|
|
718
|
+
{
|
|
719
|
+
key: row.id,
|
|
720
|
+
role: 'row',
|
|
721
|
+
},
|
|
722
|
+
userProps
|
|
723
|
+
)
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
getCellProps: (rowId, columnId, userProps) => {
|
|
727
|
+
const cell = instance.getCell(rowId, columnId)
|
|
728
|
+
|
|
729
|
+
if (!cell) {
|
|
730
|
+
return
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return propGetter(
|
|
734
|
+
{
|
|
735
|
+
key: cell.id,
|
|
736
|
+
role: 'gridcell',
|
|
737
|
+
},
|
|
738
|
+
userProps
|
|
739
|
+
)
|
|
740
|
+
},
|
|
741
|
+
|
|
742
|
+
getTableWidth: () =>
|
|
743
|
+
instance.getHeaderGroups()[0]?.headers.reduce((sum, header) => {
|
|
744
|
+
return sum + header.getWidth()
|
|
745
|
+
}, 0) ?? 0,
|
|
746
|
+
getLeftTableWidth: () =>
|
|
747
|
+
instance.getLeftHeaderGroups()[0]?.headers.reduce((sum, header) => {
|
|
748
|
+
return sum + header.getWidth()
|
|
749
|
+
}, 0) ?? 0,
|
|
750
|
+
getCenterTableWidth: () =>
|
|
751
|
+
instance.getCenterHeaderGroups()[0]?.headers.reduce((sum, header) => {
|
|
752
|
+
return sum + header.getWidth()
|
|
753
|
+
}, 0) ?? 0,
|
|
754
|
+
getRightTableWidth: () =>
|
|
755
|
+
instance.getRightHeaderGroups()[0]?.headers.reduce((sum, header) => {
|
|
756
|
+
return sum + header.getWidth()
|
|
757
|
+
}, 0) ?? 0,
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
instance = Object.assign(instance, finalInstance)
|
|
761
|
+
|
|
762
|
+
return instance
|
|
763
|
+
}
|