@tanstack/table-core 8.0.0-beta.3 → 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.
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.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
  },
@@ -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> = {
@@ -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,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) &&
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 &