@tanstack/table-core 8.2.0 → 8.2.3

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.2.0",
4
+ "version": "8.2.3",
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",
@@ -105,7 +105,7 @@ export function createColumn<TData extends RowData, TValue>(
105
105
  if (resolvedColumnDef.accessorFn) {
106
106
  accessorFn = resolvedColumnDef.accessorFn
107
107
  } else if (resolvedColumnDef.accessorKey) {
108
- accessorFn = (originalRow?: TData) =>
108
+ accessorFn = (originalRow: TData) =>
109
109
  (originalRow as any)[resolvedColumnDef.accessorKey]
110
110
  }
111
111
 
package/src/core/row.ts CHANGED
@@ -5,7 +5,7 @@ import { createCell } from './cell'
5
5
  export type CoreRow<TData extends RowData> = {
6
6
  id: string
7
7
  index: number
8
- original?: TData
8
+ original: TData
9
9
  depth: number
10
10
  _valuesCache: Record<string, unknown>
11
11
  getValue: <TValue>(columnId: string) => TValue
@@ -20,7 +20,7 @@ export type CoreRow<TData extends RowData> = {
20
20
  export const createRow = <TData extends RowData>(
21
21
  table: Table<TData>,
22
22
  id: string,
23
- original: TData | undefined,
23
+ original: TData,
24
24
  rowIndex: number,
25
25
  depth: number,
26
26
  subRows?: Row<TData>[]
@@ -108,6 +108,9 @@ export const RowSelection: TableFeature = {
108
108
  // All of the rows are flat already, so it wouldn't be worth it
109
109
  if (value) {
110
110
  preGroupedFlatRows.forEach(row => {
111
+ if (!row.getCanSelect()) {
112
+ return
113
+ }
111
114
  rowSelection[row.id] = true
112
115
  })
113
116
  } else {
@@ -430,7 +433,9 @@ const mutateRowIsSelected = <TData extends RowData>(
430
433
  if (!row.getCanMultiSelect()) {
431
434
  Object.keys(selectedRowIds).forEach(key => delete selectedRowIds[key])
432
435
  }
433
- selectedRowIds[id] = true
436
+ if (row.getCanSelect()) {
437
+ selectedRowIds[id] = true
438
+ }
434
439
  } else {
435
440
  delete selectedRowIds[id]
436
441
  }
@@ -58,6 +58,7 @@ export type SortingColumn<TData extends RowData> = {
58
58
  getAutoSortingFn: () => SortingFn<TData>
59
59
  getAutoSortDir: () => SortDirection
60
60
  getSortingFn: () => SortingFn<TData>
61
+ getFirstSortDir: () => SortDirection
61
62
  getNextSortingOrder: () => SortDirection | false
62
63
  getCanSort: () => boolean
63
64
  getCanMultiSort: () => boolean
@@ -184,19 +185,21 @@ export const Sorting: TableFeature = {
184
185
 
185
186
  // this needs to be outside of table.setSorting to be in sync with rerender
186
187
  const nextSortingOrder = column.getNextSortingOrder()
188
+ const hasManualValue = typeof desc !== 'undefined' && desc !== null
187
189
 
188
190
  table.setSorting(old => {
189
191
  // Find any existing sorting for this column
190
192
  const existingSorting = old?.find(d => d.id === column.id)
191
193
  const existingIndex = old?.findIndex(d => d.id === column.id)
192
- const hasDescDefined = typeof desc !== 'undefined' && desc !== null
193
194
 
194
195
  let newSorting: SortingState = []
195
196
 
196
197
  // What should we do with this sort action?
197
198
  let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
199
+ let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'
198
200
 
199
- if (column.getCanMultiSort() && multi) {
201
+ // Multi-mode
202
+ if (old?.length && column.getCanMultiSort() && multi) {
200
203
  if (existingSorting) {
201
204
  sortAction = 'toggle'
202
205
  } else {
@@ -214,29 +217,22 @@ export const Sorting: TableFeature = {
214
217
  }
215
218
 
216
219
  // Handle toggle states that will remove the sorting
217
- if (
218
- sortAction === 'toggle' && // Must be toggling
219
- (table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
220
- !hasDescDefined && // Must not be setting desc
221
- (multi ? table.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
222
- !nextSortingOrder // Finally, detect if it should indeed be removed
223
- ) {
224
- sortAction = 'remove'
220
+ if (sortAction === 'toggle') {
221
+ // If we are "actually" toggling (not a manual set value), should we remove the sorting?
222
+ if (!hasManualValue) {
223
+ // Is our intention to remove?
224
+ if (!nextSortingOrder) {
225
+ sortAction = 'remove'
226
+ }
227
+ }
225
228
  }
226
229
 
227
- if (sortAction === 'replace') {
228
- newSorting = [
229
- {
230
- id: column.id,
231
- desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
232
- },
233
- ]
234
- } else if (sortAction === 'add' && old?.length) {
230
+ if (sortAction === 'add') {
235
231
  newSorting = [
236
232
  ...old,
237
233
  {
238
234
  id: column.id,
239
- desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
235
+ desc: nextDesc,
240
236
  },
241
237
  ]
242
238
  // Take latest n columns
@@ -245,41 +241,56 @@ export const Sorting: TableFeature = {
245
241
  newSorting.length -
246
242
  (table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
247
243
  )
248
- } else if (sortAction === 'toggle' && old?.length) {
244
+ } else if (sortAction === 'toggle') {
249
245
  // This flips (or sets) the
250
246
  newSorting = old.map(d => {
251
247
  if (d.id === column.id) {
252
248
  return {
253
249
  ...d,
254
- desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
250
+ desc: nextDesc,
255
251
  }
256
252
  }
257
253
  return d
258
254
  })
259
- } else if (sortAction === 'remove' && old?.length) {
255
+ } else if (sortAction === 'remove') {
260
256
  newSorting = old.filter(d => d.id !== column.id)
257
+ } else {
258
+ newSorting = [
259
+ {
260
+ id: column.id,
261
+ desc: nextDesc,
262
+ },
263
+ ]
261
264
  }
262
265
 
263
266
  return newSorting
264
267
  })
265
268
  },
266
269
 
267
- getNextSortingOrder: () => {
270
+ getFirstSortDir: () => {
268
271
  const sortDescFirst =
269
272
  column.columnDef.sortDescFirst ??
270
273
  table.options.sortDescFirst ??
271
274
  column.getAutoSortDir() === 'desc'
272
- const firstSortDirection = sortDescFirst ? 'desc' : 'asc'
275
+ return sortDescFirst ? 'desc' : 'asc'
276
+ },
273
277
 
278
+ getNextSortingOrder: (multi?: boolean) => {
279
+ const firstSortDirection = column.getFirstSortDir()
274
280
  const isSorted = column.getIsSorted()
281
+
275
282
  if (!isSorted) {
276
283
  return firstSortDirection
277
284
  }
278
- if (isSorted === firstSortDirection) {
279
- return isSorted === 'desc' ? 'asc' : 'desc'
280
- } else {
285
+
286
+ if (
287
+ isSorted !== firstSortDirection &&
288
+ (table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
289
+ (multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
290
+ ) {
281
291
  return false
282
292
  }
293
+ return isSorted === 'desc' ? 'asc' : 'desc'
283
294
  },
284
295
 
285
296
  getCanSort: () => {
package/src/sortingFns.ts CHANGED
@@ -35,10 +35,13 @@ const textCaseSensitive: SortingFn<any> = (rowA, rowB, columnId) => {
35
35
  }
36
36
 
37
37
  const datetime: SortingFn<any> = (rowA, rowB, columnId) => {
38
- return compareBasic(
39
- (rowA.getValue(columnId) as Date).getTime(),
40
- (rowB.getValue(columnId) as Date).getTime()
41
- )
38
+ const a = rowA.getValue<Date>(columnId)
39
+ const b = rowB.getValue<Date>(columnId)
40
+
41
+ // Can handle nullish values
42
+ // Use > and < because == (and ===) doesn't work with
43
+ // Date objects (would require calling getTime()).
44
+ return a > b ? 1 : a < b ? -1 : 0
42
45
  }
43
46
 
44
47
  const basic: SortingFn<any> = (rowA, rowB, columnId) => {
@@ -40,7 +40,7 @@ export function getCoreRowModel<TData extends RowData>(): (
40
40
  const row = createRow(
41
41
  table,
42
42
  table._getRowId(originalRows[i]!, i, parent),
43
- originalRows[i],
43
+ originalRows[i]!,
44
44
  i,
45
45
  depth
46
46
  )
@@ -55,7 +55,13 @@ export function getGroupedRowModel<TData extends RowData>(): (
55
55
  ? flattenBy(groupedRows, row => row.subRows)
56
56
  : groupedRows
57
57
 
58
- const row = createRow(table, id, undefined, index, depth)
58
+ const row = createRow(
59
+ table,
60
+ id,
61
+ leafRows[0]!.original,
62
+ index,
63
+ depth
64
+ )
59
65
 
60
66
  Object.assign(row, {
61
67
  groupingColumnId: columnId,