@tanstack/table-core 8.0.0-alpha.80 → 8.0.0-alpha.84
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/core/cell.js +38 -0
- package/build/cjs/core/cell.js.map +1 -0
- package/build/cjs/core/column.js +88 -0
- package/build/cjs/core/column.js.map +1 -0
- package/build/cjs/{features/Headers.js → core/headers.js} +54 -66
- package/build/cjs/core/headers.js.map +1 -0
- package/build/cjs/core/instance.js +255 -0
- package/build/cjs/core/instance.js.map +1 -0
- package/build/cjs/core/row.js +77 -0
- package/build/cjs/core/row.js.map +1 -0
- package/build/cjs/createTable.js +4 -2
- package/build/cjs/createTable.js.map +1 -1
- package/build/cjs/features/ColumnSizing.js +5 -5
- package/build/cjs/features/ColumnSizing.js.map +1 -1
- package/build/cjs/features/Expanding.js.map +1 -1
- package/build/cjs/features/Filters.js +12 -7
- package/build/cjs/features/Filters.js.map +1 -1
- package/build/cjs/features/Grouping.js +5 -5
- package/build/cjs/features/Grouping.js.map +1 -1
- package/build/cjs/features/Ordering.js.map +1 -1
- package/build/cjs/features/Pagination.js.map +1 -1
- package/build/cjs/features/Pinning.js +17 -11
- package/build/cjs/features/Pinning.js.map +1 -1
- package/build/cjs/features/RowSelection.js.map +1 -1
- package/build/cjs/features/Sorting.js +9 -9
- package/build/cjs/features/Sorting.js.map +1 -1
- package/build/cjs/features/Visibility.js +3 -3
- package/build/cjs/features/Visibility.js.map +1 -1
- package/build/cjs/index.js +17 -5
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/utils/filterRowsUtils.js +23 -21
- package/build/cjs/utils/filterRowsUtils.js.map +1 -1
- package/build/cjs/utils/getCoreRowModel.js +9 -8
- package/build/cjs/utils/getCoreRowModel.js.map +1 -1
- package/build/cjs/utils/getFacetedRowModel.js +1 -1
- package/build/cjs/utils/getFacetedRowModel.js.map +1 -1
- package/build/cjs/utils/getFilteredRowModel.js +20 -9
- package/build/cjs/utils/getFilteredRowModel.js.map +1 -1
- package/build/cjs/utils/getGroupedRowModel.js +14 -13
- package/build/cjs/utils/getGroupedRowModel.js.map +1 -1
- package/build/cjs/utils/getSortedRowModel.js +2 -2
- package/build/cjs/utils/getSortedRowModel.js.map +1 -1
- package/build/esm/index.js +731 -810
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +418 -377
- package/build/types/core/cell.d.ts +9 -0
- package/build/types/{features/Columns.d.ts → core/column.d.ts} +3 -17
- package/build/types/{features/Headers.d.ts → core/headers.d.ts} +26 -11
- package/build/types/core/instance.d.ts +57 -0
- package/build/types/core/row.d.ts +15 -0
- package/build/types/createTable.d.ts +8 -5
- package/build/types/features/ColumnSizing.d.ts +2 -1
- package/build/types/features/Expanding.d.ts +2 -1
- package/build/types/features/Filters.d.ts +5 -3
- package/build/types/features/Grouping.d.ts +3 -2
- package/build/types/features/Ordering.d.ts +2 -1
- package/build/types/features/Pagination.d.ts +2 -1
- package/build/types/features/Pinning.d.ts +3 -2
- package/build/types/features/RowSelection.d.ts +2 -1
- package/build/types/features/Sorting.d.ts +2 -1
- package/build/types/features/Visibility.d.ts +2 -1
- package/build/types/index.d.ts +7 -2
- package/build/types/sortingFns.d.ts +4 -4
- package/build/types/types.d.ts +13 -53
- package/build/umd/index.development.js +738 -806
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/core/cell.ts +47 -0
- package/src/core/column.ts +148 -0
- package/src/{features/Headers.ts → core/headers.ts} +98 -99
- package/src/core/instance.ts +361 -0
- package/src/core/row.ts +85 -0
- package/src/createTable.ts +14 -7
- package/src/features/ColumnSizing.ts +6 -6
- package/src/features/Expanding.ts +1 -2
- package/src/features/Filters.ts +20 -8
- package/src/features/Grouping.ts +7 -6
- package/src/features/Ordering.ts +1 -1
- package/src/features/Pagination.ts +1 -1
- package/src/features/Pinning.ts +18 -9
- package/src/features/RowSelection.ts +1 -1
- package/src/features/Sorting.ts +11 -11
- package/src/features/Visibility.ts +3 -3
- package/src/index.ts +7 -8
- package/src/types.ts +17 -64
- package/src/utils/filterRowsUtils.ts +5 -3
- package/src/utils/getCoreRowModel.ts +4 -2
- package/src/utils/getFacetedRowModel.ts +1 -1
- package/src/utils/getFilteredRowModel.ts +24 -13
- package/src/utils/getGroupedRowModel.ts +9 -8
- package/src/utils/getSortedRowModel.ts +2 -2
- package/build/cjs/core.js +0 -168
- package/build/cjs/core.js.map +0 -1
- package/build/cjs/features/Cells.js +0 -101
- package/build/cjs/features/Cells.js.map +0 -1
- package/build/cjs/features/Columns.js +0 -189
- package/build/cjs/features/Columns.js.map +0 -1
- package/build/cjs/features/Headers.js.map +0 -1
- package/build/cjs/features/Rows.js +0 -94
- package/build/cjs/features/Rows.js.map +0 -1
- package/build/types/core.d.ts +0 -30
- package/build/types/features/Cells.d.ts +0 -13
- package/build/types/features/Rows.d.ts +0 -28
- package/src/core.ts +0 -246
- package/src/features/Cells.ts +0 -128
- package/src/features/Columns.ts +0 -289
- package/src/features/Rows.ts +0 -142
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { flattenBy, functionalUpdate, memo, RequiredKeys } from '../utils'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Updater,
|
|
5
|
+
TableOptionsResolved,
|
|
6
|
+
TableState,
|
|
7
|
+
TableInstance,
|
|
8
|
+
Renderable,
|
|
9
|
+
TableGenerics,
|
|
10
|
+
InitialTableState,
|
|
11
|
+
Row,
|
|
12
|
+
Column,
|
|
13
|
+
RowModel,
|
|
14
|
+
ColumnDef,
|
|
15
|
+
} from '../types'
|
|
16
|
+
|
|
17
|
+
//
|
|
18
|
+
import { createColumn } from './column'
|
|
19
|
+
import { Headers } from './headers'
|
|
20
|
+
//
|
|
21
|
+
|
|
22
|
+
import { ColumnSizing } from '../features/ColumnSizing'
|
|
23
|
+
import { Expanding } from '../features/Expanding'
|
|
24
|
+
import { Filters } from '../features/Filters'
|
|
25
|
+
import { Grouping } from '../features/Grouping'
|
|
26
|
+
import { Ordering } from '../features/Ordering'
|
|
27
|
+
import { Pagination } from '../features/Pagination'
|
|
28
|
+
import { Pinning } from '../features/Pinning'
|
|
29
|
+
import { RowSelection } from '../features/RowSelection'
|
|
30
|
+
import { Sorting } from '../features/Sorting'
|
|
31
|
+
import { Visibility } from '../features/Visibility'
|
|
32
|
+
|
|
33
|
+
export type TableFeature = {
|
|
34
|
+
getDefaultOptions?: (instance: any) => any
|
|
35
|
+
getInitialState?: (initialState?: InitialTableState) => any
|
|
36
|
+
createInstance?: (instance: any) => any
|
|
37
|
+
getDefaultColumnDef?: () => any
|
|
38
|
+
createColumn?: (column: any, instance: any) => any
|
|
39
|
+
createHeader?: (column: any, instance: any) => any
|
|
40
|
+
createCell?: (cell: any, column: any, row: any, instance: any) => any
|
|
41
|
+
createRow?: (row: any, instance: any) => any
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const features = [
|
|
45
|
+
Headers,
|
|
46
|
+
Visibility,
|
|
47
|
+
Ordering,
|
|
48
|
+
Pinning,
|
|
49
|
+
Filters,
|
|
50
|
+
Sorting,
|
|
51
|
+
Grouping,
|
|
52
|
+
Expanding,
|
|
53
|
+
Pagination,
|
|
54
|
+
RowSelection,
|
|
55
|
+
ColumnSizing,
|
|
56
|
+
] as const
|
|
57
|
+
|
|
58
|
+
//
|
|
59
|
+
|
|
60
|
+
export type CoreTableState = {}
|
|
61
|
+
|
|
62
|
+
export type CoreOptions<TGenerics extends TableGenerics> = {
|
|
63
|
+
data: TGenerics['Row'][]
|
|
64
|
+
state: Partial<TableState>
|
|
65
|
+
onStateChange: (updater: Updater<TableState>) => void
|
|
66
|
+
render: TGenerics['Renderer']
|
|
67
|
+
debugAll?: boolean
|
|
68
|
+
debugTable?: boolean
|
|
69
|
+
debugHeaders?: boolean
|
|
70
|
+
debugColumns?: boolean
|
|
71
|
+
debugRows?: boolean
|
|
72
|
+
initialState?: InitialTableState
|
|
73
|
+
autoResetAll?: boolean
|
|
74
|
+
mergeOptions?: <T>(defaultOptions: T, options: Partial<T>) => T
|
|
75
|
+
meta?: TGenerics['TableMeta']
|
|
76
|
+
getCoreRowModel: (
|
|
77
|
+
instance: TableInstance<TGenerics>
|
|
78
|
+
) => () => RowModel<TGenerics>
|
|
79
|
+
getSubRows?: (
|
|
80
|
+
originalRow: TGenerics['Row'],
|
|
81
|
+
index: number
|
|
82
|
+
) => undefined | TGenerics['Row'][]
|
|
83
|
+
getRowId?: (
|
|
84
|
+
originalRow: TGenerics['Row'],
|
|
85
|
+
index: number,
|
|
86
|
+
parent?: Row<TGenerics>
|
|
87
|
+
) => string
|
|
88
|
+
columns: ColumnDef<TGenerics>[]
|
|
89
|
+
defaultColumn?: Partial<ColumnDef<TGenerics>>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type CoreInstance<TGenerics extends TableGenerics> = {
|
|
93
|
+
initialState: TableState
|
|
94
|
+
reset: () => void
|
|
95
|
+
options: RequiredKeys<TableOptionsResolved<TGenerics>, 'state'>
|
|
96
|
+
setOptions: (newOptions: Updater<TableOptionsResolved<TGenerics>>) => void
|
|
97
|
+
getState: () => TableState
|
|
98
|
+
setState: (updater: Updater<TableState>) => void
|
|
99
|
+
_features: readonly TableFeature[]
|
|
100
|
+
_queue: (cb: () => void) => void
|
|
101
|
+
_render: <TProps>(
|
|
102
|
+
template: Renderable<TGenerics, TProps>,
|
|
103
|
+
props: TProps
|
|
104
|
+
) => string | null | TGenerics['Rendered']
|
|
105
|
+
_getRowId: (
|
|
106
|
+
_: TGenerics['Row'],
|
|
107
|
+
index: number,
|
|
108
|
+
parent?: Row<TGenerics>
|
|
109
|
+
) => string
|
|
110
|
+
getCoreRowModel: () => RowModel<TGenerics>
|
|
111
|
+
_getCoreRowModel?: () => RowModel<TGenerics>
|
|
112
|
+
getRowModel: () => RowModel<TGenerics>
|
|
113
|
+
getRow: (id: string) => Row<TGenerics>
|
|
114
|
+
_getDefaultColumnDef: () => Partial<ColumnDef<TGenerics>>
|
|
115
|
+
_getColumnDefs: () => ColumnDef<TGenerics>[]
|
|
116
|
+
_getAllFlatColumnsById: () => Record<string, Column<TGenerics>>
|
|
117
|
+
getAllColumns: () => Column<TGenerics>[]
|
|
118
|
+
getAllFlatColumns: () => Column<TGenerics>[]
|
|
119
|
+
getAllLeafColumns: () => Column<TGenerics>[]
|
|
120
|
+
getColumn: (columnId: string) => Column<TGenerics>
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function createTableInstance<TGenerics extends TableGenerics>(
|
|
124
|
+
options: TableOptionsResolved<TGenerics>
|
|
125
|
+
): TableInstance<TGenerics> {
|
|
126
|
+
if (options.debugAll || options.debugTable) {
|
|
127
|
+
console.info('Creating Table Instance...')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let instance = { _features: features } as unknown as TableInstance<TGenerics>
|
|
131
|
+
|
|
132
|
+
const defaultOptions = instance._features.reduce((obj, feature) => {
|
|
133
|
+
return Object.assign(obj, feature.getDefaultOptions?.(instance))
|
|
134
|
+
}, {}) as TableOptionsResolved<TGenerics>
|
|
135
|
+
|
|
136
|
+
const mergeOptions = (options: TableOptionsResolved<TGenerics>) => {
|
|
137
|
+
if (instance.options.mergeOptions) {
|
|
138
|
+
return instance.options.mergeOptions(defaultOptions, options)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
...defaultOptions,
|
|
143
|
+
...options,
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const coreInitialState: CoreTableState = {}
|
|
148
|
+
|
|
149
|
+
let initialState = {
|
|
150
|
+
...coreInitialState,
|
|
151
|
+
...(options.initialState ?? {}),
|
|
152
|
+
} as TableState
|
|
153
|
+
|
|
154
|
+
instance._features.forEach(feature => {
|
|
155
|
+
initialState = feature.getInitialState?.(initialState) ?? initialState
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
const queued: (() => void)[] = []
|
|
159
|
+
let queuedTimeout = false
|
|
160
|
+
|
|
161
|
+
const coreInstance: CoreInstance<TGenerics> = {
|
|
162
|
+
_features: features,
|
|
163
|
+
options: {
|
|
164
|
+
...defaultOptions,
|
|
165
|
+
...options,
|
|
166
|
+
},
|
|
167
|
+
initialState,
|
|
168
|
+
_queue: cb => {
|
|
169
|
+
queued.push(cb)
|
|
170
|
+
|
|
171
|
+
if (!queuedTimeout) {
|
|
172
|
+
queuedTimeout = true
|
|
173
|
+
|
|
174
|
+
// Schedule a microtask to run the queued callbacks after
|
|
175
|
+
// the current call stack (render, etc) has finished.
|
|
176
|
+
Promise.resolve()
|
|
177
|
+
.then(() => {
|
|
178
|
+
while (queued.length) {
|
|
179
|
+
queued.shift()!()
|
|
180
|
+
}
|
|
181
|
+
queuedTimeout = false
|
|
182
|
+
})
|
|
183
|
+
.catch(error =>
|
|
184
|
+
setTimeout(() => {
|
|
185
|
+
throw error
|
|
186
|
+
})
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
reset: () => {
|
|
191
|
+
instance.setState(instance.initialState)
|
|
192
|
+
},
|
|
193
|
+
setOptions: updater => {
|
|
194
|
+
const newOptions = functionalUpdate(updater, instance.options)
|
|
195
|
+
instance.options = mergeOptions(newOptions)
|
|
196
|
+
},
|
|
197
|
+
_render: (template, props) => {
|
|
198
|
+
if (typeof instance.options.render === 'function') {
|
|
199
|
+
return instance.options.render(template, props)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (typeof template === 'function') {
|
|
203
|
+
return (template as Function)(props)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return template
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
getState: () => {
|
|
210
|
+
return instance.options.state as TableState
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
setState: (updater: Updater<TableState>) => {
|
|
214
|
+
instance.options.onStateChange?.(updater)
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
_getRowId: (
|
|
218
|
+
row: TGenerics['Row'],
|
|
219
|
+
index: number,
|
|
220
|
+
parent?: Row<TGenerics>
|
|
221
|
+
) =>
|
|
222
|
+
instance.options.getRowId?.(row, index, parent) ??
|
|
223
|
+
`${parent ? [parent.id, index].join('.') : index}`,
|
|
224
|
+
|
|
225
|
+
getCoreRowModel: () => {
|
|
226
|
+
if (!instance._getCoreRowModel) {
|
|
227
|
+
instance._getCoreRowModel = instance.options.getCoreRowModel(instance)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return instance._getCoreRowModel()
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// The final calls start at the bottom of the model,
|
|
234
|
+
// expanded rows, which then work their way up
|
|
235
|
+
|
|
236
|
+
getRowModel: () => {
|
|
237
|
+
return instance.getPaginationRowModel()
|
|
238
|
+
},
|
|
239
|
+
getRow: (id: string) => {
|
|
240
|
+
const row = instance.getRowModel().rowsById[id]
|
|
241
|
+
|
|
242
|
+
if (!row) {
|
|
243
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
244
|
+
throw new Error(`getRow expected an ID, but got ${id}`)
|
|
245
|
+
}
|
|
246
|
+
throw new Error()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return row
|
|
250
|
+
},
|
|
251
|
+
_getDefaultColumnDef: memo(
|
|
252
|
+
() => [instance.options.defaultColumn],
|
|
253
|
+
defaultColumn => {
|
|
254
|
+
defaultColumn = (defaultColumn ?? {}) as Partial<ColumnDef<TGenerics>>
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
header: props => props.header.column.id,
|
|
258
|
+
footer: props => props.header.column.id,
|
|
259
|
+
cell: props => props.getValue().toString?.() ?? null,
|
|
260
|
+
...instance._features.reduce((obj, feature) => {
|
|
261
|
+
return Object.assign(obj, feature.getDefaultColumnDef?.())
|
|
262
|
+
}, {}),
|
|
263
|
+
...defaultColumn,
|
|
264
|
+
} as Partial<ColumnDef<TGenerics>>
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
268
|
+
key: process.env.NODE_ENV === 'development' && 'getDefaultColumnDef',
|
|
269
|
+
}
|
|
270
|
+
),
|
|
271
|
+
|
|
272
|
+
_getColumnDefs: () => instance.options.columns,
|
|
273
|
+
|
|
274
|
+
getAllColumns: memo(
|
|
275
|
+
() => [instance._getColumnDefs()],
|
|
276
|
+
columnDefs => {
|
|
277
|
+
const recurseColumns = (
|
|
278
|
+
columnDefs: ColumnDef<TGenerics>[],
|
|
279
|
+
parent?: Column<TGenerics>,
|
|
280
|
+
depth = 0
|
|
281
|
+
): Column<TGenerics>[] => {
|
|
282
|
+
return columnDefs.map(columnDef => {
|
|
283
|
+
const column = createColumn(instance, columnDef, depth, parent)
|
|
284
|
+
|
|
285
|
+
column.columns = columnDef.columns
|
|
286
|
+
? recurseColumns(columnDef.columns, column, depth + 1)
|
|
287
|
+
: []
|
|
288
|
+
|
|
289
|
+
return column
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return recurseColumns(columnDefs)
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
key: process.env.NODE_ENV === 'development' && 'getAllColumns',
|
|
297
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
298
|
+
}
|
|
299
|
+
),
|
|
300
|
+
|
|
301
|
+
getAllFlatColumns: memo(
|
|
302
|
+
() => [instance.getAllColumns()],
|
|
303
|
+
allColumns => {
|
|
304
|
+
return allColumns.flatMap(column => {
|
|
305
|
+
return column.getFlatColumns()
|
|
306
|
+
})
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
key: process.env.NODE_ENV === 'development' && 'getAllFlatColumns',
|
|
310
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
311
|
+
}
|
|
312
|
+
),
|
|
313
|
+
|
|
314
|
+
_getAllFlatColumnsById: memo(
|
|
315
|
+
() => [instance.getAllFlatColumns()],
|
|
316
|
+
flatColumns => {
|
|
317
|
+
return flatColumns.reduce((acc, column) => {
|
|
318
|
+
acc[column.id] = column
|
|
319
|
+
return acc
|
|
320
|
+
}, {} as Record<string, Column<TGenerics>>)
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
key: process.env.NODE_ENV === 'development' && 'getAllFlatColumnsById',
|
|
324
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
325
|
+
}
|
|
326
|
+
),
|
|
327
|
+
|
|
328
|
+
getAllLeafColumns: memo(
|
|
329
|
+
() => [instance.getAllColumns(), instance._getOrderColumnsFn()],
|
|
330
|
+
(allColumns, orderColumns) => {
|
|
331
|
+
let leafColumns = allColumns.flatMap(column => column.getLeafColumns())
|
|
332
|
+
return orderColumns(leafColumns)
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
key: process.env.NODE_ENV === 'development' && 'getAllLeafColumns',
|
|
336
|
+
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
337
|
+
}
|
|
338
|
+
),
|
|
339
|
+
|
|
340
|
+
getColumn: columnId => {
|
|
341
|
+
const column = instance._getAllFlatColumnsById()[columnId]
|
|
342
|
+
|
|
343
|
+
if (!column) {
|
|
344
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
345
|
+
console.warn(`[Table] Column with id ${columnId} does not exist.`)
|
|
346
|
+
}
|
|
347
|
+
throw new Error()
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return column
|
|
351
|
+
},
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
Object.assign(instance, coreInstance)
|
|
355
|
+
|
|
356
|
+
instance._features.forEach(feature => {
|
|
357
|
+
return Object.assign(instance, feature.createInstance?.(instance))
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
return instance
|
|
361
|
+
}
|
package/src/core/row.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Cell, Row, TableGenerics, TableInstance } from '../types'
|
|
2
|
+
import { flattenBy, memo } from '../utils'
|
|
3
|
+
import { createCell } from './cell'
|
|
4
|
+
|
|
5
|
+
export type CoreRow<TGenerics extends TableGenerics> = {
|
|
6
|
+
id: string
|
|
7
|
+
index: number
|
|
8
|
+
original?: TGenerics['Row']
|
|
9
|
+
depth: number
|
|
10
|
+
_valuesCache: Record<string, any>
|
|
11
|
+
getValue: (columnId: string) => any
|
|
12
|
+
subRows: Row<TGenerics>[]
|
|
13
|
+
getLeafRows: () => Row<TGenerics>[]
|
|
14
|
+
originalSubRows?: TGenerics['Row'][]
|
|
15
|
+
getAllCells: () => Cell<TGenerics>[]
|
|
16
|
+
_getAllCellsByColumnId: () => Record<string, Cell<TGenerics>>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createRow = <TGenerics extends TableGenerics>(
|
|
20
|
+
instance: TableInstance<TGenerics>,
|
|
21
|
+
id: string,
|
|
22
|
+
original: TGenerics['Row'] | undefined,
|
|
23
|
+
rowIndex: number,
|
|
24
|
+
depth: number,
|
|
25
|
+
subRows?: Row<TGenerics>[]
|
|
26
|
+
): Row<TGenerics> => {
|
|
27
|
+
let row: CoreRow<TGenerics> = {
|
|
28
|
+
id,
|
|
29
|
+
index: rowIndex,
|
|
30
|
+
original,
|
|
31
|
+
depth,
|
|
32
|
+
_valuesCache: {},
|
|
33
|
+
getValue: columnId => {
|
|
34
|
+
if (row._valuesCache.hasOwnProperty(columnId)) {
|
|
35
|
+
return row._valuesCache[columnId]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const column = instance.getColumn(columnId)
|
|
39
|
+
|
|
40
|
+
if (!column.accessorFn) {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
row._valuesCache[columnId] = column.accessorFn(row.original, rowIndex)
|
|
45
|
+
|
|
46
|
+
return row._valuesCache[columnId]
|
|
47
|
+
},
|
|
48
|
+
subRows: subRows ?? [],
|
|
49
|
+
getLeafRows: () => flattenBy(row.subRows, d => d.subRows),
|
|
50
|
+
getAllCells: memo(
|
|
51
|
+
() => [instance.getAllLeafColumns()],
|
|
52
|
+
leafColumns => {
|
|
53
|
+
return leafColumns.map(column => {
|
|
54
|
+
return createCell(instance, row as Row<TGenerics>, column, column.id)
|
|
55
|
+
})
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
key: process.env.NODE_ENV === 'development' && 'row.getAllCells',
|
|
59
|
+
debug: () => instance.options.debugAll ?? instance.options.debugRows,
|
|
60
|
+
}
|
|
61
|
+
),
|
|
62
|
+
|
|
63
|
+
_getAllCellsByColumnId: memo(
|
|
64
|
+
() => [row.getAllCells()],
|
|
65
|
+
allCells => {
|
|
66
|
+
return allCells.reduce((acc, cell) => {
|
|
67
|
+
acc[cell.column.id] = cell
|
|
68
|
+
return acc
|
|
69
|
+
}, {} as Record<string, Cell<TGenerics>>)
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key:
|
|
73
|
+
process.env.NODE_ENV === 'production' && 'row.getAllCellsByColumnId',
|
|
74
|
+
debug: () => instance.options.debugAll ?? instance.options.debugRows,
|
|
75
|
+
}
|
|
76
|
+
),
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < instance._features.length; i++) {
|
|
80
|
+
const feature = instance._features[i]
|
|
81
|
+
Object.assign(row, feature?.createRow?.(row, instance))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return row as Row<TGenerics>
|
|
85
|
+
}
|
package/src/createTable.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
AccessorFn,
|
|
7
7
|
AnyRender,
|
|
8
8
|
TableGenerics,
|
|
9
|
+
TableOptionsResolved,
|
|
9
10
|
TableOptions,
|
|
10
11
|
} from './types'
|
|
11
12
|
import { IfDefined, Overwrite } from './utils'
|
|
@@ -26,15 +27,15 @@ export type CreateTableOptions<
|
|
|
26
27
|
sortingFns?: TSortingFns
|
|
27
28
|
aggregationFns?: TAggregationFns
|
|
28
29
|
} & Omit<
|
|
29
|
-
|
|
30
|
+
TableOptionsResolved<TGenerics>,
|
|
30
31
|
'filterFns' | 'sortingFns' | 'aggregationFns'
|
|
31
32
|
>
|
|
32
33
|
>
|
|
33
34
|
|
|
34
35
|
export type Table<TGenerics extends TableGenerics> = {
|
|
35
36
|
generics: TGenerics
|
|
36
|
-
options: Partial<
|
|
37
|
-
setGenerics: <T extends TableGenerics>() => Table<T>
|
|
37
|
+
options: Partial<TableOptionsResolved<TGenerics>>
|
|
38
|
+
// setGenerics: <T extends TableGenerics>() => Table<T>
|
|
38
39
|
setRowType: <TRow>() => Table<Overwrite<TGenerics, { Row: TRow }>>
|
|
39
40
|
setTableMetaType: <TTableMeta>() => Table<
|
|
40
41
|
Overwrite<TGenerics, { TableMeta: TTableMeta }>
|
|
@@ -42,10 +43,13 @@ export type Table<TGenerics extends TableGenerics> = {
|
|
|
42
43
|
setColumnMetaType: <TColumnMeta>() => Table<
|
|
43
44
|
Overwrite<TGenerics, { ColumnMeta: TColumnMeta }>
|
|
44
45
|
>
|
|
46
|
+
setFilterMetaType: <TFilterMeta>() => Table<
|
|
47
|
+
Overwrite<TGenerics, { FilterMeta: TFilterMeta }>
|
|
48
|
+
>
|
|
45
49
|
setOptions: <
|
|
46
|
-
TFilterFns extends Record<string, FilterFn<
|
|
47
|
-
TSortingFns extends Record<string, SortingFn<
|
|
48
|
-
TAggregationFns extends Record<string, AggregationFn<
|
|
50
|
+
TFilterFns extends Record<string, FilterFn<TGenerics>>,
|
|
51
|
+
TSortingFns extends Record<string, SortingFn<TGenerics>>,
|
|
52
|
+
TAggregationFns extends Record<string, AggregationFn<TGenerics>>
|
|
49
53
|
>(
|
|
50
54
|
options: CreateTableOptions<
|
|
51
55
|
any,
|
|
@@ -116,6 +120,7 @@ export type Table<TGenerics extends TableGenerics> = {
|
|
|
116
120
|
}
|
|
117
121
|
>
|
|
118
122
|
) => ColumnDef<TGenerics>
|
|
123
|
+
createOptions: (options: TableOptions<TGenerics>) => TableOptions<TGenerics>
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
//
|
|
@@ -140,10 +145,11 @@ function createTable<TGenerics extends TableGenerics>(
|
|
|
140
145
|
throw new Error('')
|
|
141
146
|
})(),
|
|
142
147
|
},
|
|
143
|
-
setGenerics: () => table as any,
|
|
148
|
+
// setGenerics: () => table as any,
|
|
144
149
|
setRowType: () => table as any,
|
|
145
150
|
setTableMetaType: () => table as any,
|
|
146
151
|
setColumnMetaType: () => table as any,
|
|
152
|
+
setFilterMetaType: () => table as any,
|
|
147
153
|
setOptions: newOptions =>
|
|
148
154
|
createTable(_, __, {
|
|
149
155
|
...options,
|
|
@@ -175,6 +181,7 @@ function createTable<TGenerics extends TableGenerics>(
|
|
|
175
181
|
|
|
176
182
|
throw new Error('Invalid accessor')
|
|
177
183
|
},
|
|
184
|
+
createOptions: options => options,
|
|
178
185
|
}
|
|
179
186
|
|
|
180
187
|
return table
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TableFeature } from '../core/instance'
|
|
1
2
|
import {
|
|
2
3
|
Column,
|
|
3
4
|
Header,
|
|
@@ -5,7 +6,6 @@ import {
|
|
|
5
6
|
TableGenerics,
|
|
6
7
|
TableInstance,
|
|
7
8
|
Updater,
|
|
8
|
-
TableFeature,
|
|
9
9
|
} from '../types'
|
|
10
10
|
import { makeStateUpdater } from '../utils'
|
|
11
11
|
import { ColumnPinningPosition } from './Pinning'
|
|
@@ -93,7 +93,7 @@ const getDefaultColumnSizingInfoState = (): ColumnSizingInfoState => ({
|
|
|
93
93
|
})
|
|
94
94
|
|
|
95
95
|
export const ColumnSizing: TableFeature = {
|
|
96
|
-
|
|
96
|
+
getDefaultColumnDef: (): ColumnSizingColumnDef => {
|
|
97
97
|
return defaultColumnSizing
|
|
98
98
|
},
|
|
99
99
|
getInitialState: (state): ColumnSizingTableState => {
|
|
@@ -124,10 +124,10 @@ export const ColumnSizing: TableFeature = {
|
|
|
124
124
|
|
|
125
125
|
return Math.min(
|
|
126
126
|
Math.max(
|
|
127
|
-
column.minSize ?? defaultColumnSizing.minSize,
|
|
128
|
-
columnSize ?? column.size ?? defaultColumnSizing.size
|
|
127
|
+
column.columnDef.minSize ?? defaultColumnSizing.minSize,
|
|
128
|
+
columnSize ?? column.columnDef.size ?? defaultColumnSizing.size
|
|
129
129
|
),
|
|
130
|
-
column.maxSize ?? defaultColumnSizing.maxSize
|
|
130
|
+
column.columnDef.maxSize ?? defaultColumnSizing.maxSize
|
|
131
131
|
)
|
|
132
132
|
},
|
|
133
133
|
getStart: position => {
|
|
@@ -156,7 +156,7 @@ export const ColumnSizing: TableFeature = {
|
|
|
156
156
|
},
|
|
157
157
|
getCanResize: () => {
|
|
158
158
|
return (
|
|
159
|
-
(column.enableResizing ?? true) &&
|
|
159
|
+
(column.columnDef.enableResizing ?? true) &&
|
|
160
160
|
(instance.options.enableColumnResizing ?? true)
|
|
161
161
|
)
|
|
162
162
|
},
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { RowModel } from '..'
|
|
2
|
+
import { TableFeature } from '../core/instance'
|
|
2
3
|
import {
|
|
3
4
|
OnChangeFn,
|
|
4
5
|
TableGenerics,
|
|
5
6
|
TableInstance,
|
|
6
7
|
Row,
|
|
7
8
|
Updater,
|
|
8
|
-
TableFeature,
|
|
9
9
|
} from '../types'
|
|
10
10
|
import { makeStateUpdater } from '../utils'
|
|
11
|
-
import { Rows } from './Rows'
|
|
12
11
|
|
|
13
12
|
export type ExpandedStateList = Record<string, boolean>
|
|
14
13
|
export type ExpandedState = true | Record<string, boolean>
|
package/src/features/Filters.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RowModel } from '..'
|
|
2
|
+
import { TableFeature } from '../core/instance'
|
|
2
3
|
import { BuiltInFilterFn, filterFns } from '../filterFns'
|
|
3
4
|
import {
|
|
4
5
|
Column,
|
|
@@ -7,7 +8,6 @@ import {
|
|
|
7
8
|
TableInstance,
|
|
8
9
|
Row,
|
|
9
10
|
Updater,
|
|
10
|
-
TableFeature,
|
|
11
11
|
} from '../types'
|
|
12
12
|
import {
|
|
13
13
|
functionalUpdate,
|
|
@@ -38,7 +38,13 @@ export type ResolvedColumnFilter<TGenerics extends TableGenerics> = {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export type FilterFn<TGenerics extends TableGenerics> = {
|
|
41
|
-
(
|
|
41
|
+
(
|
|
42
|
+
row: Row<TGenerics>,
|
|
43
|
+
columnId: string,
|
|
44
|
+
filterValue: any,
|
|
45
|
+
addMeta: (meta: TGenerics['FilterMeta']) => void
|
|
46
|
+
): boolean
|
|
47
|
+
|
|
42
48
|
resolveFilterValue?: TransformFilterValueFn<TGenerics>
|
|
43
49
|
autoRemove?: ColumnFilterAutoRemoveTestFn<TGenerics>
|
|
44
50
|
}
|
|
@@ -90,7 +96,8 @@ export type FiltersColumn<TGenerics extends TableGenerics> = {
|
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
export type FiltersRow<TGenerics extends TableGenerics> = {
|
|
93
|
-
|
|
99
|
+
columnFilters: Record<string, boolean>
|
|
100
|
+
columnFiltersMeta: Record<string, TGenerics['FilterMeta']>
|
|
94
101
|
subRowsByFacetId: Record<string, Row<TGenerics>[]>
|
|
95
102
|
}
|
|
96
103
|
|
|
@@ -154,7 +161,7 @@ export type FiltersInstance<TGenerics extends TableGenerics> = {
|
|
|
154
161
|
//
|
|
155
162
|
|
|
156
163
|
export const Filters: TableFeature = {
|
|
157
|
-
|
|
164
|
+
getDefaultColumnDef: <
|
|
158
165
|
TGenerics extends TableGenerics
|
|
159
166
|
>(): FiltersColumnDef<TGenerics> => {
|
|
160
167
|
return {
|
|
@@ -183,7 +190,7 @@ export const Filters: TableFeature = {
|
|
|
183
190
|
getColumnCanGlobalFilter: column => {
|
|
184
191
|
const value = instance
|
|
185
192
|
.getCoreRowModel()
|
|
186
|
-
.flatRows[0]?.
|
|
193
|
+
.flatRows[0]?._getAllCellsByColumnId()
|
|
187
194
|
[column.id]?.getValue()
|
|
188
195
|
|
|
189
196
|
return typeof value === 'string'
|
|
@@ -210,6 +217,10 @@ export const Filters: TableFeature = {
|
|
|
210
217
|
return filterFns.inNumberRange
|
|
211
218
|
}
|
|
212
219
|
|
|
220
|
+
if (typeof value === 'boolean') {
|
|
221
|
+
return filterFns.equals
|
|
222
|
+
}
|
|
223
|
+
|
|
213
224
|
if (value !== null && typeof value === 'object') {
|
|
214
225
|
return filterFns.equals
|
|
215
226
|
}
|
|
@@ -236,7 +247,7 @@ export const Filters: TableFeature = {
|
|
|
236
247
|
},
|
|
237
248
|
getCanFilter: () => {
|
|
238
249
|
return (
|
|
239
|
-
(column.enableColumnFilter ?? true) &&
|
|
250
|
+
(column.columnDef.enableColumnFilter ?? true) &&
|
|
240
251
|
(instance.options.enableColumnFilters ?? true) &&
|
|
241
252
|
(instance.options.enableFilters ?? true) &&
|
|
242
253
|
!!column.accessorFn
|
|
@@ -245,7 +256,7 @@ export const Filters: TableFeature = {
|
|
|
245
256
|
|
|
246
257
|
getCanGlobalFilter: () => {
|
|
247
258
|
return (
|
|
248
|
-
(column.enableGlobalFilter ?? true) &&
|
|
259
|
+
(column.columnDef.enableGlobalFilter ?? true) &&
|
|
249
260
|
(instance.options.enableGlobalFilter ?? true) &&
|
|
250
261
|
(instance.options.enableFilters ?? true) &&
|
|
251
262
|
(instance.options.getColumnCanGlobalFilter?.(column) ?? true) &&
|
|
@@ -343,7 +354,8 @@ export const Filters: TableFeature = {
|
|
|
343
354
|
instance: TableInstance<TGenerics>
|
|
344
355
|
): FiltersRow<TGenerics> => {
|
|
345
356
|
return {
|
|
346
|
-
|
|
357
|
+
columnFilters: {},
|
|
358
|
+
columnFiltersMeta: {},
|
|
347
359
|
subRowsByFacetId: {},
|
|
348
360
|
}
|
|
349
361
|
},
|