@tanstack/table-core 8.0.0-beta.3 → 8.0.0-beta.6

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 (34) hide show
  1. package/build/cjs/core/cell.js +7 -1
  2. package/build/cjs/core/cell.js.map +1 -1
  3. package/build/cjs/core/instance.js.map +1 -1
  4. package/build/cjs/features/Expanding.js +4 -7
  5. package/build/cjs/features/Expanding.js.map +1 -1
  6. package/build/cjs/features/Grouping.js +14 -4
  7. package/build/cjs/features/Grouping.js.map +1 -1
  8. package/build/cjs/features/RowSelection.js +4 -4
  9. package/build/cjs/features/RowSelection.js.map +1 -1
  10. package/build/cjs/features/Sorting.js +28 -10
  11. package/build/cjs/features/Sorting.js.map +1 -1
  12. package/build/cjs/utils/getPaginationRowModel.js +19 -5
  13. package/build/cjs/utils/getPaginationRowModel.js.map +1 -1
  14. package/build/esm/index.js +76 -31
  15. package/build/esm/index.js.map +1 -1
  16. package/build/stats-html.html +1 -1
  17. package/build/stats-react.json +318 -318
  18. package/build/types/core/instance.d.ts +1 -0
  19. package/build/types/features/RowSelection.d.ts +2 -2
  20. package/build/types/features/Sorting.d.ts +1 -0
  21. package/build/types/types.d.ts +1 -1
  22. package/build/umd/index.development.js +76 -31
  23. package/build/umd/index.development.js.map +1 -1
  24. package/build/umd/index.production.js +1 -1
  25. package/build/umd/index.production.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/core/cell.ts +4 -1
  28. package/src/core/instance.ts +1 -0
  29. package/src/features/Expanding.ts +4 -8
  30. package/src/features/Grouping.ts +14 -3
  31. package/src/features/RowSelection.ts +8 -6
  32. package/src/features/Sorting.ts +28 -15
  33. package/src/types.ts +1 -1
  34. package/src/utils/getPaginationRowModel.ts +21 -6
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/table-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "8.0.0-beta.3",
4
+ "version": "8.0.0-beta.6",
5
5
  "description": "Headless UI for building powerful tables & datagrids for TS/JS.",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/tanstack/table#readme",
package/src/core/cell.ts CHANGED
@@ -14,6 +14,9 @@ export function createCell<TGenerics extends TableGenerics>(
14
14
  column: Column<TGenerics>,
15
15
  columnId: string
16
16
  ) {
17
+ const getRenderValue = () =>
18
+ cell.getValue() ?? instance.options.renderFallbackValue
19
+
17
20
  const cell: CoreCell<TGenerics> = {
18
21
  id: `${row.id}_${column.id}`,
19
22
  row,
@@ -26,7 +29,7 @@ export function createCell<TGenerics extends TableGenerics>(
26
29
  column,
27
30
  row,
28
31
  cell: cell as Cell<TGenerics>,
29
- getValue: cell.getValue,
32
+ getValue: getRenderValue,
30
33
  })
31
34
  : null
32
35
  },
@@ -85,6 +85,7 @@ export type CoreOptions<TGenerics extends TableGenerics> = {
85
85
  ) => string
86
86
  columns: ColumnDef<TGenerics>[]
87
87
  defaultColumn?: Partial<ColumnDef<TGenerics>>
88
+ renderFallbackValue: any
88
89
  }
89
90
 
90
91
  export type CoreInstance<TGenerics extends TableGenerics> = {
@@ -63,7 +63,6 @@ export const Expanding: TableFeature = {
63
63
  ): ExpandedOptions<TGenerics> => {
64
64
  return {
65
65
  onExpandedChange: makeStateUpdater('expanded', instance),
66
- autoResetExpanded: true,
67
66
  paginateExpandedRows: true,
68
67
  }
69
68
  },
@@ -83,13 +82,10 @@ export const Expanding: TableFeature = {
83
82
  return
84
83
  }
85
84
 
86
- if (instance.options.autoResetAll === false) {
87
- return
88
- }
89
-
90
85
  if (
91
- instance.options.autoResetAll === true ||
92
- instance.options.autoResetExpanded
86
+ instance.options.autoResetAll ??
87
+ instance.options.autoResetExpanded ??
88
+ !instance.options.manualExpanding
93
89
  ) {
94
90
  if (queued) return
95
91
  queued = true
@@ -160,7 +156,7 @@ export const Expanding: TableFeature = {
160
156
 
161
157
  return maxDepth
162
158
  },
163
- getPreExpandedRowModel: () => instance.getGroupedRowModel(),
159
+ getPreExpandedRowModel: () => instance.getSortedRowModel(),
164
160
  getExpandedRowModel: () => {
165
161
  if (
166
162
  !instance._getExpandedRowModel &&
@@ -214,7 +214,7 @@ export const Grouping: TableFeature = {
214
214
  )
215
215
  },
216
216
 
217
- getPreGroupedRowModel: () => instance.getSortedRowModel(),
217
+ getPreGroupedRowModel: () => instance.getFilteredRowModel(),
218
218
  getGroupedRowModel: () => {
219
219
  if (
220
220
  !instance._getGroupedRowModel &&
@@ -248,6 +248,9 @@ export const Grouping: TableFeature = {
248
248
  row: Row<TGenerics>,
249
249
  instance: TableInstance<TGenerics>
250
250
  ): GroupingCell<TGenerics> => {
251
+ const getRenderValue = () =>
252
+ cell.getValue() ?? instance.options.renderFallbackValue
253
+
251
254
  return {
252
255
  getIsGrouped: () =>
253
256
  column.getIsGrouped() && column.id === row.groupingColumnId,
@@ -257,8 +260,16 @@ export const Grouping: TableFeature = {
257
260
  !cell.getIsPlaceholder() &&
258
261
  row.subRows?.length > 1,
259
262
  renderAggregatedCell: () => {
263
+ if (process.env.NODE_ENV === 'development') {
264
+ if (!column.columnDef.aggregatedCell) {
265
+ console.warn(
266
+ 'A columnDef.aggregatedCell template is recommended for displaying aggregated values.'
267
+ )
268
+ }
269
+ }
270
+
260
271
  const template =
261
- column.columnDef.aggregatedCell ?? column.columnDef.cell
272
+ column.columnDef.aggregatedCell || column.columnDef.cell
262
273
 
263
274
  return template
264
275
  ? instance._render(template, {
@@ -266,7 +277,7 @@ export const Grouping: TableFeature = {
266
277
  column,
267
278
  row,
268
279
  cell,
269
- getValue: cell.getValue,
280
+ getValue: getRenderValue,
270
281
  })
271
282
  : null
272
283
  },
@@ -58,8 +58,8 @@ export type RowSelectionInstance<TGenerics extends TableGenerics> = {
58
58
  getIsAllPageRowsSelected: () => boolean
59
59
  getIsSomeRowsSelected: () => boolean
60
60
  getIsSomePageRowsSelected: () => boolean
61
- toggleAllRowsSelected: (value: boolean) => void
62
- toggleAllPageRowsSelected: (value: boolean) => void
61
+ toggleAllRowsSelected: (value?: boolean) => void
62
+ toggleAllPageRowsSelected: (value?: boolean) => void
63
63
  getPreSelectedRowModel: () => RowModel<TGenerics>
64
64
  getSelectedRowModel: () => RowModel<TGenerics>
65
65
  getFilteredSelectedRowModel: () => RowModel<TGenerics>
@@ -128,7 +128,7 @@ export const RowSelection: TableFeature = {
128
128
  },
129
129
  toggleAllPageRowsSelected: value =>
130
130
  instance.setRowSelection(old => {
131
- const selectAll =
131
+ const resolvedValue =
132
132
  typeof value !== 'undefined'
133
133
  ? value
134
134
  : !instance.getIsAllPageRowsSelected()
@@ -136,7 +136,7 @@ export const RowSelection: TableFeature = {
136
136
  const rowSelection: RowSelectionState = { ...old }
137
137
 
138
138
  instance.getRowModel().rows.forEach(row => {
139
- mutateRowIsSelected(rowSelection, row.id, value, instance)
139
+ mutateRowIsSelected(rowSelection, row.id, resolvedValue, instance)
140
140
  })
141
141
 
142
142
  return rowSelection
@@ -245,7 +245,7 @@ export const RowSelection: TableFeature = {
245
245
  ),
246
246
 
247
247
  getGroupedSelectedRowModel: memo(
248
- () => [instance.getState().rowSelection, instance.getGroupedRowModel()],
248
+ () => [instance.getState().rowSelection, instance.getSortedRowModel()],
249
249
  (rowSelection, rowModel) => {
250
250
  if (!Object.keys(rowSelection).length) {
251
251
  return {
@@ -325,7 +325,9 @@ export const RowSelection: TableFeature = {
325
325
  const paginationFlatRows = instance.getPaginationRowModel().flatRows
326
326
  return instance.getIsAllPageRowsSelected()
327
327
  ? false
328
- : !!paginationFlatRows?.length
328
+ : paginationFlatRows.some(
329
+ d => d.getIsSelected() || d.getIsSomeSelected()
330
+ )
329
331
  },
330
332
 
331
333
  getToggleAllRowsSelectedHandler: () => {
@@ -58,6 +58,7 @@ export type SortingColumn<TGenerics extends TableGenerics> = {
58
58
  getAutoSortingFn: () => SortingFn<TGenerics>
59
59
  getAutoSortDir: () => SortDirection
60
60
  getSortingFn: () => SortingFn<TGenerics>
61
+ getNextSortingOrder: () => SortDirection | false
61
62
  getCanSort: () => boolean
62
63
  getCanMultiSort: () => boolean
63
64
  getSortIndex: () => number
@@ -189,6 +190,9 @@ export const Sorting: TableFeature = {
189
190
  // return
190
191
  // }
191
192
 
193
+ // this needs to be outside of instance.setSorting to be in sync with rerender
194
+ const nextSortingOrder = column.getNextSortingOrder()
195
+
192
196
  instance.setSorting(old => {
193
197
  // Find any existing sorting for this column
194
198
  const existingSorting = old?.find(d => d.id === column.id)
@@ -198,7 +202,7 @@ export const Sorting: TableFeature = {
198
202
  let newSorting: SortingState = []
199
203
 
200
204
  // What should we do with this sort action?
201
- let sortAction
205
+ let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
202
206
 
203
207
  if (column.getCanMultiSort() && multi) {
204
208
  if (existingSorting) {
@@ -217,20 +221,13 @@ export const Sorting: TableFeature = {
217
221
  }
218
222
  }
219
223
 
220
- const sortDescFirst =
221
- column.columnDef.sortDescFirst ??
222
- instance.options.sortDescFirst ??
223
- column.getAutoSortDir() === 'desc'
224
-
225
224
  // Handle toggle states that will remove the sorting
226
225
  if (
227
226
  sortAction === 'toggle' && // Must be toggling
228
227
  (instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
229
228
  !hasDescDefined && // Must not be setting desc
230
229
  (multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
231
- (existingSorting?.desc // Finally, detect if it should indeed be removed
232
- ? !sortDescFirst
233
- : sortDescFirst)
230
+ !nextSortingOrder // Finally, detect if it should indeed be removed
234
231
  ) {
235
232
  sortAction = 'remove'
236
233
  }
@@ -239,7 +236,7 @@ export const Sorting: TableFeature = {
239
236
  newSorting = [
240
237
  {
241
238
  id: column.id,
242
- desc: hasDescDefined ? desc! : !!sortDescFirst,
239
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
243
240
  },
244
241
  ]
245
242
  } else if (sortAction === 'add' && old?.length) {
@@ -247,7 +244,7 @@ export const Sorting: TableFeature = {
247
244
  ...old,
248
245
  {
249
246
  id: column.id,
250
- desc: hasDescDefined ? desc! : !!sortDescFirst,
247
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
251
248
  },
252
249
  ]
253
250
  // Take latest n columns
@@ -263,7 +260,7 @@ export const Sorting: TableFeature = {
263
260
  if (d.id === column.id) {
264
261
  return {
265
262
  ...d,
266
- desc: hasDescDefined ? desc! : !existingSorting?.desc,
263
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
267
264
  }
268
265
  }
269
266
  return d
@@ -276,6 +273,24 @@ export const Sorting: TableFeature = {
276
273
  })
277
274
  },
278
275
 
276
+ getNextSortingOrder: () => {
277
+ const sortDescFirst =
278
+ column.columnDef.sortDescFirst ??
279
+ instance.options.sortDescFirst ??
280
+ column.getAutoSortDir() === 'desc'
281
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc'
282
+
283
+ const isSorted = column.getIsSorted()
284
+ if (!isSorted) {
285
+ return firstSortDirection
286
+ }
287
+ if (isSorted === firstSortDirection) {
288
+ return isSorted === 'desc' ? 'asc' : 'desc'
289
+ } else {
290
+ return false
291
+ }
292
+ },
293
+
279
294
  getCanSort: () => {
280
295
  return (
281
296
  (column.columnDef.enableSorting ?? true) &&
@@ -330,8 +345,6 @@ export const Sorting: TableFeature = {
330
345
  createInstance: <TGenerics extends TableGenerics>(
331
346
  instance: TableInstance<TGenerics>
332
347
  ): SortingInstance<TGenerics> => {
333
- let registered = false
334
-
335
348
  return {
336
349
  setSorting: updater => instance.options.onSortingChange?.(updater),
337
350
  resetSorting: defaultState => {
@@ -339,7 +352,7 @@ export const Sorting: TableFeature = {
339
352
  defaultState ? [] : instance.initialState?.sorting ?? []
340
353
  )
341
354
  },
342
- getPreSortedRowModel: () => instance.getFilteredRowModel(),
355
+ getPreSortedRowModel: () => instance.getGroupedRowModel(),
343
356
  getSortedRowModel: () => {
344
357
  if (
345
358
  !instance._getSortedRowModel &&
package/src/types.ts CHANGED
@@ -123,7 +123,7 @@ export type TableOptionsResolved<TGenerics extends TableGenerics> =
123
123
 
124
124
  export type TableOptions<TGenerics extends TableGenerics> = Omit<
125
125
  PartialKeys<TableOptionsResolved<TGenerics>, 'state' | 'onStateChange'>,
126
- 'render'
126
+ 'render' | 'renderFallbackValue'
127
127
  >
128
128
 
129
129
  export type TableState = CoreTableState &
@@ -1,4 +1,4 @@
1
- import { TableInstance, RowModel, TableGenerics } from '../types'
1
+ import { TableInstance, RowModel, TableGenerics, Row } from '../types'
2
2
  import { memo } from '../utils'
3
3
  import { expandRows } from './getExpandedRowModel'
4
4
 
@@ -23,8 +23,10 @@ export function getPaginationRowModel<TGenerics extends TableGenerics>(opts?: {
23
23
 
24
24
  rows = rows.slice(pageStart, pageEnd)
25
25
 
26
+ let paginatedRowModel: RowModel<TGenerics>
27
+
26
28
  if (!instance.options.paginateExpandedRows) {
27
- return expandRows(
29
+ paginatedRowModel = expandRows(
28
30
  {
29
31
  rows,
30
32
  flatRows,
@@ -32,13 +34,26 @@ export function getPaginationRowModel<TGenerics extends TableGenerics>(opts?: {
32
34
  },
33
35
  instance
34
36
  )
37
+ } else {
38
+ paginatedRowModel = {
39
+ rows,
40
+ flatRows,
41
+ rowsById,
42
+ }
35
43
  }
36
44
 
37
- return {
38
- rows,
39
- flatRows,
40
- rowsById,
45
+ paginatedRowModel.flatRows = []
46
+
47
+ const handleRow = (row: Row<TGenerics>) => {
48
+ paginatedRowModel.flatRows.push(row)
49
+ if (row.subRows.length) {
50
+ row.subRows.forEach(handleRow)
51
+ }
41
52
  }
53
+
54
+ paginatedRowModel.rows.forEach(handleRow)
55
+
56
+ return paginatedRowModel
42
57
  },
43
58
  {
44
59
  key: process.env.NODE_ENV === 'development' && 'getPaginationRowModel',