@tanstack/table-core 8.0.0-beta.1 → 8.0.0-beta.4

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 (39) 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 +2 -2
  4. package/build/cjs/core/instance.js.map +1 -1
  5. package/build/cjs/features/Expanding.js.map +1 -1
  6. package/build/cjs/features/Filters.js +1 -2
  7. package/build/cjs/features/Filters.js.map +1 -1
  8. package/build/cjs/features/Grouping.js +19 -6
  9. package/build/cjs/features/Grouping.js.map +1 -1
  10. package/build/cjs/features/Pagination.js.map +1 -1
  11. package/build/cjs/features/Sorting.js +27 -9
  12. package/build/cjs/features/Sorting.js.map +1 -1
  13. package/build/cjs/utils/getGroupedRowModel.js +0 -5
  14. package/build/cjs/utils/getGroupedRowModel.js.map +1 -1
  15. package/build/esm/index.js +56 -25
  16. package/build/esm/index.js.map +1 -1
  17. package/build/stats-html.html +1 -1
  18. package/build/stats-react.json +318 -318
  19. package/build/types/core/instance.d.ts +2 -1
  20. package/build/types/features/Expanding.d.ts +1 -1
  21. package/build/types/features/Filters.d.ts +1 -4
  22. package/build/types/features/Grouping.d.ts +1 -4
  23. package/build/types/features/Pagination.d.ts +1 -1
  24. package/build/types/features/Sorting.d.ts +2 -1
  25. package/build/types/types.d.ts +1 -1
  26. package/build/umd/index.development.js +56 -25
  27. package/build/umd/index.development.js.map +1 -1
  28. package/build/umd/index.production.js +1 -1
  29. package/build/umd/index.production.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/core/cell.ts +4 -1
  32. package/src/core/instance.ts +3 -4
  33. package/src/features/Expanding.ts +1 -3
  34. package/src/features/Filters.ts +6 -10
  35. package/src/features/Grouping.ts +20 -13
  36. package/src/features/Pagination.ts +1 -3
  37. package/src/features/Sorting.ts +28 -15
  38. package/src/types.ts +1 -1
  39. package/src/utils/getGroupedRowModel.ts +0 -7
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.1",
4
+ "version": "8.0.0-beta.4",
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
  },
@@ -73,9 +73,7 @@ export type CoreOptions<TGenerics extends TableGenerics> = {
73
73
  autoResetAll?: boolean
74
74
  mergeOptions?: <T>(defaultOptions: T, options: Partial<T>) => T
75
75
  meta?: TGenerics['TableMeta']
76
- getCoreRowModel: (
77
- instance: TableInstance<TGenerics>
78
- ) => () => RowModel<TGenerics>
76
+ getCoreRowModel: (instance: TableInstance<any>) => () => RowModel<any>
79
77
  getSubRows?: (
80
78
  originalRow: TGenerics['Row'],
81
79
  index: number
@@ -87,6 +85,7 @@ export type CoreOptions<TGenerics extends TableGenerics> = {
87
85
  ) => string
88
86
  columns: ColumnDef<TGenerics>[]
89
87
  defaultColumn?: Partial<ColumnDef<TGenerics>>
88
+ renderFallbackValue: any
90
89
  }
91
90
 
92
91
  export type CoreInstance<TGenerics extends TableGenerics> = {
@@ -256,7 +255,7 @@ export function createTableInstance<TGenerics extends TableGenerics>(
256
255
  return {
257
256
  header: props => props.header.column.id,
258
257
  footer: props => props.header.column.id,
259
- cell: props => props.getValue().toString?.() ?? null,
258
+ cell: props => props.getValue()?.toString?.() ?? null,
260
259
  ...instance._features.reduce((obj, feature) => {
261
260
  return Object.assign(obj, feature.getDefaultColumnDef?.())
262
261
  }, {}),
@@ -27,9 +27,7 @@ export type ExpandedOptions<TGenerics extends TableGenerics> = {
27
27
  onExpandedChange?: OnChangeFn<ExpandedState>
28
28
  autoResetExpanded?: boolean
29
29
  enableExpanding?: boolean
30
- getExpandedRowModel?: (
31
- instance: TableInstance<TGenerics>
32
- ) => () => RowModel<TGenerics>
30
+ getExpandedRowModel?: (instance: TableInstance<any>) => () => RowModel<any>
33
31
  getIsRowExpanded?: (row: Row<TGenerics>) => boolean
34
32
  getRowCanExpand?: (row: Row<TGenerics>) => boolean
35
33
  paginateExpandedRows?: boolean
@@ -75,7 +75,6 @@ export type FiltersColumnDef<TGenerics extends TableGenerics> = {
75
75
  }
76
76
 
77
77
  export type FiltersColumn<TGenerics extends TableGenerics> = {
78
- filterFn: FilterFnOption<Overwrite<TGenerics, { Value: any }>>
79
78
  getAutoFilterFn: () => FilterFn<TGenerics> | undefined
80
79
  getFilterFn: () => FilterFn<TGenerics> | undefined
81
80
  setFilterValue: (updater: Updater<any>) => void
@@ -102,9 +101,7 @@ export type FiltersOptions<TGenerics extends TableGenerics> = {
102
101
  manualFiltering?: boolean
103
102
  filterFromLeafRows?: boolean
104
103
  filterFns?: TGenerics['FilterFns']
105
- getFilteredRowModel?: (
106
- instance: TableInstance<TGenerics>
107
- ) => () => RowModel<TGenerics>
104
+ getFilteredRowModel?: (instance: TableInstance<any>) => () => RowModel<any>
108
105
 
109
106
  // Column
110
107
  onColumnFiltersChange?: OnChangeFn<ColumnFiltersState>
@@ -199,7 +196,6 @@ export const Filters: TableFeature = {
199
196
  instance: TableInstance<TGenerics>
200
197
  ): FiltersColumn<TGenerics> => {
201
198
  return {
202
- filterFn: column.filterFn,
203
199
  getAutoFilterFn: () => {
204
200
  const firstRow = instance.getCoreRowModel().flatRows[0]
205
201
 
@@ -230,15 +226,15 @@ export const Filters: TableFeature = {
230
226
  getFilterFn: () => {
231
227
  const userFilterFns = instance.options.filterFns
232
228
 
233
- return isFunction(column.filterFn)
234
- ? column.filterFn
235
- : column.filterFn === 'auto'
229
+ return isFunction(column.columnDef.filterFn)
230
+ ? column.columnDef.filterFn
231
+ : column.columnDef.filterFn === 'auto'
236
232
  ? column.getAutoFilterFn()
237
233
  : (userFilterFns as Record<string, any>)?.[
238
- column.filterFn as string
234
+ column.columnDef.filterFn as string
239
235
  ] ??
240
236
  (filterFns[
241
- column.filterFn as BuiltInFilterFn
237
+ column.columnDef.filterFn as BuiltInFilterFn
242
238
  ] as FilterFn<TGenerics>)
243
239
  },
244
240
  getCanFilter: () => {
@@ -52,7 +52,6 @@ export type GroupingColumnDef<TGenerics extends TableGenerics> = {
52
52
  }
53
53
 
54
54
  export type GroupingColumn<TGenerics extends TableGenerics> = {
55
- aggregationFn?: AggregationFnOption<Overwrite<TGenerics, { Value: any }>>
56
55
  getCanGroup: () => boolean
57
56
  getIsGrouped: () => boolean
58
57
  getGroupedIndex: () => number
@@ -87,9 +86,7 @@ export type GroupingOptions<TGenerics extends TableGenerics> = {
87
86
  aggregationFns?: TGenerics['AggregationFns']
88
87
  onGroupingChange?: OnChangeFn<GroupingState>
89
88
  enableGrouping?: boolean
90
- getGroupedRowModel?: (
91
- instance: TableInstance<TGenerics>
92
- ) => () => RowModel<TGenerics>
89
+ getGroupedRowModel?: (instance: TableInstance<any>) => () => RowModel<any>
93
90
  groupedColumnMode?: false | 'reorder' | 'remove'
94
91
  }
95
92
 
@@ -110,6 +107,7 @@ export const Grouping: TableFeature = {
110
107
  TGenerics extends TableGenerics
111
108
  >(): GroupingColumnDef<TGenerics> => {
112
109
  return {
110
+ aggregatedCell: props => props.getValue()?.toString?.() ?? null,
113
111
  aggregationFn: 'auto',
114
112
  }
115
113
  },
@@ -182,8 +180,6 @@ export const Grouping: TableFeature = {
182
180
  if (Object.prototype.toString.call(value) === '[object Date]') {
183
181
  return aggregationFns.extent
184
182
  }
185
-
186
- return aggregationFns.count
187
183
  },
188
184
  getAggregationFn: () => {
189
185
  const userAggregationFns = instance.options.aggregationFns
@@ -192,15 +188,15 @@ export const Grouping: TableFeature = {
192
188
  throw new Error()
193
189
  }
194
190
 
195
- return isFunction(column.aggregationFn)
196
- ? column.aggregationFn
197
- : column.aggregationFn === 'auto'
191
+ return isFunction(column.columnDef.aggregationFn)
192
+ ? column.columnDef.aggregationFn
193
+ : column.columnDef.aggregationFn === 'auto'
198
194
  ? column.getAutoAggregationFn()
199
195
  : (userAggregationFns as Record<string, any>)?.[
200
- column.aggregationFn as string
196
+ column.columnDef.aggregationFn as string
201
197
  ] ??
202
198
  (aggregationFns[
203
- column.aggregationFn as BuiltInAggregationFn
199
+ column.columnDef.aggregationFn as BuiltInAggregationFn
204
200
  ] as AggregationFn<TGenerics>)
205
201
  },
206
202
  }
@@ -252,6 +248,9 @@ export const Grouping: TableFeature = {
252
248
  row: Row<TGenerics>,
253
249
  instance: TableInstance<TGenerics>
254
250
  ): GroupingCell<TGenerics> => {
251
+ const getRenderValue = () =>
252
+ cell.getValue() ?? instance.options.renderFallbackValue
253
+
255
254
  return {
256
255
  getIsGrouped: () =>
257
256
  column.getIsGrouped() && column.id === row.groupingColumnId,
@@ -261,8 +260,16 @@ export const Grouping: TableFeature = {
261
260
  !cell.getIsPlaceholder() &&
262
261
  row.subRows?.length > 1,
263
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
+
264
271
  const template =
265
- column.columnDef.aggregatedCell ?? column.columnDef.cell
272
+ column.columnDef.aggregatedCell || column.columnDef.cell
266
273
 
267
274
  return template
268
275
  ? instance._render(template, {
@@ -270,7 +277,7 @@ export const Grouping: TableFeature = {
270
277
  column,
271
278
  row,
272
279
  cell,
273
- getValue: cell.getValue,
280
+ getValue: getRenderValue,
274
281
  })
275
282
  : null
276
283
  },
@@ -26,9 +26,7 @@ export type PaginationOptions<TGenerics extends TableGenerics> = {
26
26
  manualPagination?: boolean
27
27
  onPaginationChange?: OnChangeFn<PaginationState>
28
28
  autoResetPageIndex?: boolean
29
- getPaginationRowModel?: (
30
- instance: TableInstance<TGenerics>
31
- ) => () => RowModel<TGenerics>
29
+ getPaginationRowModel?: (instance: TableInstance<any>) => () => RowModel<any>
32
30
  }
33
31
 
34
32
  export type PaginationDefaultOptions = {
@@ -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
@@ -76,9 +77,7 @@ export type SortingOptions<TGenerics extends TableGenerics> = {
76
77
  enableMultiRemove?: boolean
77
78
  enableMultiSort?: boolean
78
79
  sortDescFirst?: boolean
79
- getSortedRowModel?: (
80
- instance: TableInstance<TGenerics>
81
- ) => () => RowModel<TGenerics>
80
+ getSortedRowModel?: (instance: TableInstance<any>) => () => RowModel<any>
82
81
  maxMultiSortColCount?: number
83
82
  isMultiSortEvent?: (e: unknown) => boolean
84
83
  }
@@ -191,6 +190,9 @@ export const Sorting: TableFeature = {
191
190
  // return
192
191
  // }
193
192
 
193
+ // this needs to be outside of instance.setSorting to be in sync with rerender
194
+ const nextSortingOrder = column.getNextSortingOrder()
195
+
194
196
  instance.setSorting(old => {
195
197
  // Find any existing sorting for this column
196
198
  const existingSorting = old?.find(d => d.id === column.id)
@@ -200,7 +202,7 @@ export const Sorting: TableFeature = {
200
202
  let newSorting: SortingState = []
201
203
 
202
204
  // What should we do with this sort action?
203
- let sortAction
205
+ let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
204
206
 
205
207
  if (column.getCanMultiSort() && multi) {
206
208
  if (existingSorting) {
@@ -219,20 +221,13 @@ export const Sorting: TableFeature = {
219
221
  }
220
222
  }
221
223
 
222
- const sortDescFirst =
223
- column.columnDef.sortDescFirst ??
224
- instance.options.sortDescFirst ??
225
- column.getAutoSortDir() === 'desc'
226
-
227
224
  // Handle toggle states that will remove the sorting
228
225
  if (
229
226
  sortAction === 'toggle' && // Must be toggling
230
227
  (instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
231
228
  !hasDescDefined && // Must not be setting desc
232
229
  (multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
233
- (existingSorting?.desc // Finally, detect if it should indeed be removed
234
- ? !sortDescFirst
235
- : sortDescFirst)
230
+ !nextSortingOrder // Finally, detect if it should indeed be removed
236
231
  ) {
237
232
  sortAction = 'remove'
238
233
  }
@@ -241,7 +236,7 @@ export const Sorting: TableFeature = {
241
236
  newSorting = [
242
237
  {
243
238
  id: column.id,
244
- desc: hasDescDefined ? desc! : !!sortDescFirst,
239
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
245
240
  },
246
241
  ]
247
242
  } else if (sortAction === 'add' && old?.length) {
@@ -249,7 +244,7 @@ export const Sorting: TableFeature = {
249
244
  ...old,
250
245
  {
251
246
  id: column.id,
252
- desc: hasDescDefined ? desc! : !!sortDescFirst,
247
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
253
248
  },
254
249
  ]
255
250
  // Take latest n columns
@@ -265,7 +260,7 @@ export const Sorting: TableFeature = {
265
260
  if (d.id === column.id) {
266
261
  return {
267
262
  ...d,
268
- desc: hasDescDefined ? desc! : !existingSorting?.desc,
263
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
269
264
  }
270
265
  }
271
266
  return d
@@ -278,6 +273,24 @@ export const Sorting: TableFeature = {
278
273
  })
279
274
  },
280
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
+
281
294
  getCanSort: () => {
282
295
  return (
283
296
  (column.columnDef.enableSorting ?? true) &&
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 &
@@ -93,13 +93,6 @@ export function getGroupedRowModel<TGenerics extends TableGenerics>(): (
93
93
  )
94
94
 
95
95
  return row._groupingValuesCache[columnId]
96
- } else if (column.aggregationFn) {
97
- console.info({ column })
98
- throw new Error(
99
- process.env.NODE_ENV !== 'production'
100
- ? `Table: Invalid column.aggregateType option for column listed above`
101
- : ''
102
- )
103
96
  }
104
97
  },
105
98
  })