@tanstack/table-core 8.9.7 → 8.9.8

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.
Files changed (54) hide show
  1. package/build/lib/core/cell.js +1 -1
  2. package/build/lib/core/cell.js.map +1 -1
  3. package/build/lib/core/column.js +3 -3
  4. package/build/lib/core/column.js.map +1 -1
  5. package/build/lib/core/headers.js +182 -181
  6. package/build/lib/core/headers.js.map +1 -1
  7. package/build/lib/core/row.js +1 -1
  8. package/build/lib/core/row.js.map +1 -1
  9. package/build/lib/core/table.js +4 -3
  10. package/build/lib/core/table.js.map +1 -1
  11. package/build/lib/features/ColumnSizing.js +173 -179
  12. package/build/lib/features/ColumnSizing.js.map +1 -1
  13. package/build/lib/features/Expanding.js +119 -123
  14. package/build/lib/features/Expanding.js.map +1 -1
  15. package/build/lib/features/Filters.js +157 -165
  16. package/build/lib/features/Filters.js.map +1 -1
  17. package/build/lib/features/Grouping.js +71 -79
  18. package/build/lib/features/Grouping.js.map +1 -1
  19. package/build/lib/features/Ordering.js +32 -34
  20. package/build/lib/features/Ordering.js.map +1 -1
  21. package/build/lib/features/Pagination.js +112 -114
  22. package/build/lib/features/Pagination.js.map +1 -1
  23. package/build/lib/features/Pinning.js +120 -126
  24. package/build/lib/features/Pinning.js.map +1 -1
  25. package/build/lib/features/RowSelection.js +245 -247
  26. package/build/lib/features/RowSelection.js.map +1 -1
  27. package/build/lib/features/Sorting.js +163 -167
  28. package/build/lib/features/Sorting.js.map +1 -1
  29. package/build/lib/features/Visibility.js +60 -66
  30. package/build/lib/features/Visibility.js.map +1 -1
  31. package/build/lib/index.esm.js +1469 -1515
  32. package/build/lib/index.esm.js.map +1 -1
  33. package/build/lib/index.mjs +1469 -1515
  34. package/build/lib/index.mjs.map +1 -1
  35. package/build/umd/index.development.js +1469 -1515
  36. package/build/umd/index.development.js.map +1 -1
  37. package/build/umd/index.production.js +1 -1
  38. package/build/umd/index.production.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/core/cell.ts +5 -8
  41. package/src/core/column.ts +3 -3
  42. package/src/core/headers.ts +264 -280
  43. package/src/core/row.ts +1 -1
  44. package/src/core/table.ts +4 -3
  45. package/src/features/ColumnSizing.ts +220 -231
  46. package/src/features/Expanding.ts +132 -140
  47. package/src/features/Filters.ts +193 -206
  48. package/src/features/Grouping.ts +94 -110
  49. package/src/features/Ordering.ts +48 -51
  50. package/src/features/Pagination.ts +150 -154
  51. package/src/features/Pinning.ts +158 -178
  52. package/src/features/RowSelection.ts +280 -286
  53. package/src/features/Sorting.ts +196 -206
  54. package/src/features/Visibility.ts +98 -107
@@ -78,340 +78,334 @@ export const RowSelection: TableFeature = {
78
78
  }
79
79
  },
80
80
 
81
- createTable: <TData extends RowData>(
82
- table: Table<TData>
83
- ): RowSelectionInstance<TData> => {
84
- return {
85
- setRowSelection: updater => table.options.onRowSelectionChange?.(updater),
86
- resetRowSelection: defaultState =>
87
- table.setRowSelection(
88
- defaultState ? {} : table.initialState.rowSelection ?? {}
89
- ),
90
- toggleAllRowsSelected: value => {
91
- table.setRowSelection(old => {
92
- value =
93
- typeof value !== 'undefined' ? value : !table.getIsAllRowsSelected()
94
-
95
- const rowSelection = { ...old }
96
-
97
- const preGroupedFlatRows = table.getPreGroupedRowModel().flatRows
98
-
99
- // We don't use `mutateRowIsSelected` here for performance reasons.
100
- // All of the rows are flat already, so it wouldn't be worth it
101
- if (value) {
102
- preGroupedFlatRows.forEach(row => {
103
- if (!row.getCanSelect()) {
104
- return
105
- }
106
- rowSelection[row.id] = true
107
- })
108
- } else {
109
- preGroupedFlatRows.forEach(row => {
110
- delete rowSelection[row.id]
111
- })
112
- }
81
+ createTable: <TData extends RowData>(table: Table<TData>): void => {
82
+ table.setRowSelection = updater =>
83
+ table.options.onRowSelectionChange?.(updater)
84
+ table.resetRowSelection = defaultState =>
85
+ table.setRowSelection(
86
+ defaultState ? {} : table.initialState.rowSelection ?? {}
87
+ )
88
+ table.toggleAllRowsSelected = value => {
89
+ table.setRowSelection(old => {
90
+ value =
91
+ typeof value !== 'undefined' ? value : !table.getIsAllRowsSelected()
92
+
93
+ const rowSelection = { ...old }
94
+
95
+ const preGroupedFlatRows = table.getPreGroupedRowModel().flatRows
96
+
97
+ // We don't use `mutateRowIsSelected` here for performance reasons.
98
+ // All of the rows are flat already, so it wouldn't be worth it
99
+ if (value) {
100
+ preGroupedFlatRows.forEach(row => {
101
+ if (!row.getCanSelect()) {
102
+ return
103
+ }
104
+ rowSelection[row.id] = true
105
+ })
106
+ } else {
107
+ preGroupedFlatRows.forEach(row => {
108
+ delete rowSelection[row.id]
109
+ })
110
+ }
113
111
 
114
- return rowSelection
115
- })
116
- },
117
- toggleAllPageRowsSelected: value =>
118
- table.setRowSelection(old => {
119
- const resolvedValue =
120
- typeof value !== 'undefined'
121
- ? value
122
- : !table.getIsAllPageRowsSelected()
112
+ return rowSelection
113
+ })
114
+ }
115
+ table.toggleAllPageRowsSelected = value =>
116
+ table.setRowSelection(old => {
117
+ const resolvedValue =
118
+ typeof value !== 'undefined'
119
+ ? value
120
+ : !table.getIsAllPageRowsSelected()
123
121
 
124
- const rowSelection: RowSelectionState = { ...old }
122
+ const rowSelection: RowSelectionState = { ...old }
125
123
 
126
- table.getRowModel().rows.forEach(row => {
127
- mutateRowIsSelected(rowSelection, row.id, resolvedValue, table)
128
- })
124
+ table.getRowModel().rows.forEach(row => {
125
+ mutateRowIsSelected(rowSelection, row.id, resolvedValue, table)
126
+ })
129
127
 
130
- return rowSelection
131
- }),
132
-
133
- // addRowSelectionRange: rowId => {
134
- // const {
135
- // rows,
136
- // rowsById,
137
- // options: { selectGroupingRows, selectSubRows },
138
- // } = table
139
-
140
- // const findSelectedRow = (rows: Row[]) => {
141
- // let found
142
- // rows.find(d => {
143
- // if (d.getIsSelected()) {
144
- // found = d
145
- // return true
146
- // }
147
- // const subFound = findSelectedRow(d.subRows || [])
148
- // if (subFound) {
149
- // found = subFound
150
- // return true
151
- // }
152
- // return false
153
- // })
154
- // return found
155
- // }
156
-
157
- // const firstRow = findSelectedRow(rows) || rows[0]
158
- // const lastRow = rowsById[rowId]
159
-
160
- // let include = false
161
- // const selectedRowIds = {}
162
-
163
- // const addRow = (row: Row) => {
164
- // mutateRowIsSelected(selectedRowIds, row.id, true, {
165
- // rowsById,
166
- // selectGroupingRows: selectGroupingRows!,
167
- // selectSubRows: selectSubRows!,
168
- // })
169
- // }
170
-
171
- // table.rows.forEach(row => {
172
- // const isFirstRow = row.id === firstRow.id
173
- // const isLastRow = row.id === lastRow.id
174
-
175
- // if (isFirstRow || isLastRow) {
176
- // if (!include) {
177
- // include = true
178
- // } else if (include) {
179
- // addRow(row)
180
- // include = false
181
- // }
182
- // }
183
-
184
- // if (include) {
185
- // addRow(row)
186
- // }
187
- // })
188
-
189
- // table.setRowSelection(selectedRowIds)
190
- // },
191
- getPreSelectedRowModel: () => table.getCoreRowModel(),
192
- getSelectedRowModel: memo(
193
- () => [table.getState().rowSelection, table.getCoreRowModel()],
194
- (rowSelection, rowModel) => {
195
- if (!Object.keys(rowSelection).length) {
196
- return {
197
- rows: [],
198
- flatRows: [],
199
- rowsById: {},
200
- }
201
- }
128
+ return rowSelection
129
+ })
202
130
 
203
- return selectRowsFn(table, rowModel)
204
- },
205
- {
206
- key: process.env.NODE_ENV === 'development' && 'getSelectedRowModel',
207
- debug: () => table.options.debugAll ?? table.options.debugTable,
208
- }
209
- ),
210
-
211
- getFilteredSelectedRowModel: memo(
212
- () => [table.getState().rowSelection, table.getFilteredRowModel()],
213
- (rowSelection, rowModel) => {
214
- if (!Object.keys(rowSelection).length) {
215
- return {
216
- rows: [],
217
- flatRows: [],
218
- rowsById: {},
219
- }
131
+ // addRowSelectionRange: rowId => {
132
+ // const {
133
+ // rows,
134
+ // rowsById,
135
+ // options: { selectGroupingRows, selectSubRows },
136
+ // } = table
137
+
138
+ // const findSelectedRow = (rows: Row[]) => {
139
+ // let found
140
+ // rows.find(d => {
141
+ // if (d.getIsSelected()) {
142
+ // found = d
143
+ // return true
144
+ // }
145
+ // const subFound = findSelectedRow(d.subRows || [])
146
+ // if (subFound) {
147
+ // found = subFound
148
+ // return true
149
+ // }
150
+ // return false
151
+ // })
152
+ // return found
153
+ // }
154
+
155
+ // const firstRow = findSelectedRow(rows) || rows[0]
156
+ // const lastRow = rowsById[rowId]
157
+
158
+ // let include = false
159
+ // const selectedRowIds = {}
160
+
161
+ // const addRow = (row: Row) => {
162
+ // mutateRowIsSelected(selectedRowIds, row.id, true, {
163
+ // rowsById,
164
+ // selectGroupingRows: selectGroupingRows!,
165
+ // selectSubRows: selectSubRows!,
166
+ // })
167
+ // }
168
+
169
+ // table.rows.forEach(row => {
170
+ // const isFirstRow = row.id === firstRow.id
171
+ // const isLastRow = row.id === lastRow.id
172
+
173
+ // if (isFirstRow || isLastRow) {
174
+ // if (!include) {
175
+ // include = true
176
+ // } else if (include) {
177
+ // addRow(row)
178
+ // include = false
179
+ // }
180
+ // }
181
+
182
+ // if (include) {
183
+ // addRow(row)
184
+ // }
185
+ // })
186
+
187
+ // table.setRowSelection(selectedRowIds)
188
+ // },
189
+ table.getPreSelectedRowModel = () => table.getCoreRowModel()
190
+ table.getSelectedRowModel = memo(
191
+ () => [table.getState().rowSelection, table.getCoreRowModel()],
192
+ (rowSelection, rowModel) => {
193
+ if (!Object.keys(rowSelection).length) {
194
+ return {
195
+ rows: [],
196
+ flatRows: [],
197
+ rowsById: {},
220
198
  }
221
-
222
- return selectRowsFn(table, rowModel)
223
- },
224
- {
225
- key:
226
- process.env.NODE_ENV === 'production' &&
227
- 'getFilteredSelectedRowModel',
228
- debug: () => table.options.debugAll ?? table.options.debugTable,
229
199
  }
230
- ),
231
-
232
- getGroupedSelectedRowModel: memo(
233
- () => [table.getState().rowSelection, table.getSortedRowModel()],
234
- (rowSelection, rowModel) => {
235
- if (!Object.keys(rowSelection).length) {
236
- return {
237
- rows: [],
238
- flatRows: [],
239
- rowsById: {},
240
- }
241
- }
242
200
 
243
- return selectRowsFn(table, rowModel)
244
- },
245
- {
246
- key:
247
- process.env.NODE_ENV === 'production' &&
248
- 'getGroupedSelectedRowModel',
249
- debug: () => table.options.debugAll ?? table.options.debugTable,
250
- }
251
- ),
201
+ return selectRowsFn(table, rowModel)
202
+ },
203
+ {
204
+ key: process.env.NODE_ENV === 'development' && 'getSelectedRowModel',
205
+ debug: () => table.options.debugAll ?? table.options.debugTable,
206
+ }
207
+ )
252
208
 
253
- ///
209
+ table.getFilteredSelectedRowModel = memo(
210
+ () => [table.getState().rowSelection, table.getFilteredRowModel()],
211
+ (rowSelection, rowModel) => {
212
+ if (!Object.keys(rowSelection).length) {
213
+ return {
214
+ rows: [],
215
+ flatRows: [],
216
+ rowsById: {},
217
+ }
218
+ }
254
219
 
255
- // getGroupingRowCanSelect: rowId => {
256
- // const row = table.getRow(rowId)
220
+ return selectRowsFn(table, rowModel)
221
+ },
222
+ {
223
+ key:
224
+ process.env.NODE_ENV === 'production' &&
225
+ 'getFilteredSelectedRowModel',
226
+ debug: () => table.options.debugAll ?? table.options.debugTable,
227
+ }
228
+ )
257
229
 
258
- // if (!row) {
259
- // throw new Error()
260
- // }
230
+ table.getGroupedSelectedRowModel = memo(
231
+ () => [table.getState().rowSelection, table.getSortedRowModel()],
232
+ (rowSelection, rowModel) => {
233
+ if (!Object.keys(rowSelection).length) {
234
+ return {
235
+ rows: [],
236
+ flatRows: [],
237
+ rowsById: {},
238
+ }
239
+ }
261
240
 
262
- // if (typeof table.options.enableGroupingRowSelection === 'function') {
263
- // return table.options.enableGroupingRowSelection(row)
264
- // }
241
+ return selectRowsFn(table, rowModel)
242
+ },
243
+ {
244
+ key:
245
+ process.env.NODE_ENV === 'production' && 'getGroupedSelectedRowModel',
246
+ debug: () => table.options.debugAll ?? table.options.debugTable,
247
+ }
248
+ )
265
249
 
266
- // return table.options.enableGroupingRowSelection ?? false
267
- // },
250
+ ///
268
251
 
269
- getIsAllRowsSelected: () => {
270
- const preGroupedFlatRows = table.getFilteredRowModel().flatRows
271
- const { rowSelection } = table.getState()
252
+ // getGroupingRowCanSelect: rowId => {
253
+ // const row = table.getRow(rowId)
272
254
 
273
- let isAllRowsSelected = Boolean(
274
- preGroupedFlatRows.length && Object.keys(rowSelection).length
275
- )
255
+ // if (!row) {
256
+ // throw new Error()
257
+ // }
276
258
 
277
- if (isAllRowsSelected) {
278
- if (
279
- preGroupedFlatRows.some(
280
- row => row.getCanSelect() && !rowSelection[row.id]
281
- )
282
- ) {
283
- isAllRowsSelected = false
284
- }
285
- }
259
+ // if (typeof table.options.enableGroupingRowSelection === 'function') {
260
+ // return table.options.enableGroupingRowSelection(row)
261
+ // }
286
262
 
287
- return isAllRowsSelected
288
- },
263
+ // return table.options.enableGroupingRowSelection ?? false
264
+ // },
289
265
 
290
- getIsAllPageRowsSelected: () => {
291
- const paginationFlatRows = table
292
- .getPaginationRowModel()
293
- .flatRows.filter(row => row.getCanSelect())
294
- const { rowSelection } = table.getState()
266
+ table.getIsAllRowsSelected = () => {
267
+ const preGroupedFlatRows = table.getFilteredRowModel().flatRows
268
+ const { rowSelection } = table.getState()
295
269
 
296
- let isAllPageRowsSelected = !!paginationFlatRows.length
270
+ let isAllRowsSelected = Boolean(
271
+ preGroupedFlatRows.length && Object.keys(rowSelection).length
272
+ )
297
273
 
274
+ if (isAllRowsSelected) {
298
275
  if (
299
- isAllPageRowsSelected &&
300
- paginationFlatRows.some(row => !rowSelection[row.id])
276
+ preGroupedFlatRows.some(
277
+ row => row.getCanSelect() && !rowSelection[row.id]
278
+ )
301
279
  ) {
302
- isAllPageRowsSelected = false
280
+ isAllRowsSelected = false
303
281
  }
282
+ }
304
283
 
305
- return isAllPageRowsSelected
306
- },
284
+ return isAllRowsSelected
285
+ }
307
286
 
308
- getIsSomeRowsSelected: () => {
309
- const totalSelected = Object.keys(
310
- table.getState().rowSelection ?? {}
311
- ).length
312
- return (
313
- totalSelected > 0 &&
314
- totalSelected < table.getFilteredRowModel().flatRows.length
315
- )
316
- },
287
+ table.getIsAllPageRowsSelected = () => {
288
+ const paginationFlatRows = table
289
+ .getPaginationRowModel()
290
+ .flatRows.filter(row => row.getCanSelect())
291
+ const { rowSelection } = table.getState()
317
292
 
318
- getIsSomePageRowsSelected: () => {
319
- const paginationFlatRows = table.getPaginationRowModel().flatRows
320
- return table.getIsAllPageRowsSelected()
321
- ? false
322
- : paginationFlatRows
323
- .filter(row => row.getCanSelect())
324
- .some(d => d.getIsSelected() || d.getIsSomeSelected())
325
- },
293
+ let isAllPageRowsSelected = !!paginationFlatRows.length
326
294
 
327
- getToggleAllRowsSelectedHandler: () => {
328
- return (e: unknown) => {
329
- table.toggleAllRowsSelected(
330
- ((e as MouseEvent).target as HTMLInputElement).checked
331
- )
332
- }
333
- },
295
+ if (
296
+ isAllPageRowsSelected &&
297
+ paginationFlatRows.some(row => !rowSelection[row.id])
298
+ ) {
299
+ isAllPageRowsSelected = false
300
+ }
334
301
 
335
- getToggleAllPageRowsSelectedHandler: () => {
336
- return (e: unknown) => {
337
- table.toggleAllPageRowsSelected(
338
- ((e as MouseEvent).target as HTMLInputElement).checked
339
- )
340
- }
341
- },
302
+ return isAllPageRowsSelected
303
+ }
304
+
305
+ table.getIsSomeRowsSelected = () => {
306
+ const totalSelected = Object.keys(
307
+ table.getState().rowSelection ?? {}
308
+ ).length
309
+ return (
310
+ totalSelected > 0 &&
311
+ totalSelected < table.getFilteredRowModel().flatRows.length
312
+ )
313
+ }
314
+
315
+ table.getIsSomePageRowsSelected = () => {
316
+ const paginationFlatRows = table.getPaginationRowModel().flatRows
317
+ return table.getIsAllPageRowsSelected()
318
+ ? false
319
+ : paginationFlatRows
320
+ .filter(row => row.getCanSelect())
321
+ .some(d => d.getIsSelected() || d.getIsSomeSelected())
322
+ }
323
+
324
+ table.getToggleAllRowsSelectedHandler = () => {
325
+ return (e: unknown) => {
326
+ table.toggleAllRowsSelected(
327
+ ((e as MouseEvent).target as HTMLInputElement).checked
328
+ )
329
+ }
330
+ }
331
+
332
+ table.getToggleAllPageRowsSelectedHandler = () => {
333
+ return (e: unknown) => {
334
+ table.toggleAllPageRowsSelected(
335
+ ((e as MouseEvent).target as HTMLInputElement).checked
336
+ )
337
+ }
342
338
  }
343
339
  },
344
340
 
345
341
  createRow: <TData extends RowData>(
346
342
  row: Row<TData>,
347
343
  table: Table<TData>
348
- ): RowSelectionRow => {
349
- return {
350
- toggleSelected: value => {
351
- const isSelected = row.getIsSelected()
344
+ ): void => {
345
+ row.toggleSelected = value => {
346
+ const isSelected = row.getIsSelected()
352
347
 
353
- table.setRowSelection(old => {
354
- value = typeof value !== 'undefined' ? value : !isSelected
348
+ table.setRowSelection(old => {
349
+ value = typeof value !== 'undefined' ? value : !isSelected
355
350
 
356
- if (isSelected === value) {
357
- return old
358
- }
351
+ if (isSelected === value) {
352
+ return old
353
+ }
359
354
 
360
- const selectedRowIds = { ...old }
355
+ const selectedRowIds = { ...old }
361
356
 
362
- mutateRowIsSelected(selectedRowIds, row.id, value, table)
357
+ mutateRowIsSelected(selectedRowIds, row.id, value, table)
363
358
 
364
- return selectedRowIds
365
- })
366
- },
367
- getIsSelected: () => {
368
- const { rowSelection } = table.getState()
369
- return isRowSelected(row, rowSelection)
370
- },
359
+ return selectedRowIds
360
+ })
361
+ }
362
+ row.getIsSelected = () => {
363
+ const { rowSelection } = table.getState()
364
+ return isRowSelected(row, rowSelection)
365
+ }
371
366
 
372
- getIsSomeSelected: () => {
373
- const { rowSelection } = table.getState()
374
- return isSubRowSelected(row, rowSelection, table) === 'some'
375
- },
367
+ row.getIsSomeSelected = () => {
368
+ const { rowSelection } = table.getState()
369
+ return isSubRowSelected(row, rowSelection, table) === 'some'
370
+ }
376
371
 
377
- getIsAllSubRowsSelected: () => {
378
- const { rowSelection } = table.getState()
379
- return isSubRowSelected(row, rowSelection, table) === 'all'
380
- },
372
+ row.getIsAllSubRowsSelected = () => {
373
+ const { rowSelection } = table.getState()
374
+ return isSubRowSelected(row, rowSelection, table) === 'all'
375
+ }
381
376
 
382
- getCanSelect: () => {
383
- if (typeof table.options.enableRowSelection === 'function') {
384
- return table.options.enableRowSelection(row)
385
- }
377
+ row.getCanSelect = () => {
378
+ if (typeof table.options.enableRowSelection === 'function') {
379
+ return table.options.enableRowSelection(row)
380
+ }
386
381
 
387
- return table.options.enableRowSelection ?? true
388
- },
382
+ return table.options.enableRowSelection ?? true
383
+ }
389
384
 
390
- getCanSelectSubRows: () => {
391
- if (typeof table.options.enableSubRowSelection === 'function') {
392
- return table.options.enableSubRowSelection(row)
393
- }
385
+ row.getCanSelectSubRows = () => {
386
+ if (typeof table.options.enableSubRowSelection === 'function') {
387
+ return table.options.enableSubRowSelection(row)
388
+ }
394
389
 
395
- return table.options.enableSubRowSelection ?? true
396
- },
390
+ return table.options.enableSubRowSelection ?? true
391
+ }
397
392
 
398
- getCanMultiSelect: () => {
399
- if (typeof table.options.enableMultiRowSelection === 'function') {
400
- return table.options.enableMultiRowSelection(row)
401
- }
393
+ row.getCanMultiSelect = () => {
394
+ if (typeof table.options.enableMultiRowSelection === 'function') {
395
+ return table.options.enableMultiRowSelection(row)
396
+ }
402
397
 
403
- return table.options.enableMultiRowSelection ?? true
404
- },
405
- getToggleSelectedHandler: () => {
406
- const canSelect = row.getCanSelect()
398
+ return table.options.enableMultiRowSelection ?? true
399
+ }
400
+ row.getToggleSelectedHandler = () => {
401
+ const canSelect = row.getCanSelect()
407
402
 
408
- return (e: unknown) => {
409
- if (!canSelect) return
410
- row.toggleSelected(
411
- ((e as MouseEvent).target as HTMLInputElement)?.checked
412
- )
413
- }
414
- },
403
+ return (e: unknown) => {
404
+ if (!canSelect) return
405
+ row.toggleSelected(
406
+ ((e as MouseEvent).target as HTMLInputElement)?.checked
407
+ )
408
+ }
415
409
  }
416
410
  },
417
411
  }