@tanstack/table-core 8.0.0-alpha.88 → 8.0.0-alpha.89

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-alpha.88",
4
+ "version": "8.0.0-alpha.89",
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",
@@ -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
@@ -191,6 +192,9 @@ export const Sorting: TableFeature = {
191
192
  // return
192
193
  // }
193
194
 
195
+ // this needs to be outside of instance.setSorting to be in sync with rerender
196
+ const nextSortingOrder = column.getNextSortingOrder()
197
+
194
198
  instance.setSorting(old => {
195
199
  // Find any existing sorting for this column
196
200
  const existingSorting = old?.find(d => d.id === column.id)
@@ -200,7 +204,7 @@ export const Sorting: TableFeature = {
200
204
  let newSorting: SortingState = []
201
205
 
202
206
  // What should we do with this sort action?
203
- let sortAction
207
+ let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
204
208
 
205
209
  if (column.getCanMultiSort() && multi) {
206
210
  if (existingSorting) {
@@ -219,20 +223,13 @@ export const Sorting: TableFeature = {
219
223
  }
220
224
  }
221
225
 
222
- const sortDescFirst =
223
- column.columnDef.sortDescFirst ??
224
- instance.options.sortDescFirst ??
225
- column.getAutoSortDir() === 'desc'
226
-
227
226
  // Handle toggle states that will remove the sorting
228
227
  if (
229
228
  sortAction === 'toggle' && // Must be toggling
230
229
  (instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
231
230
  !hasDescDefined && // Must not be setting desc
232
231
  (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)
232
+ !nextSortingOrder // Finally, detect if it should indeed be removed
236
233
  ) {
237
234
  sortAction = 'remove'
238
235
  }
@@ -241,7 +238,7 @@ export const Sorting: TableFeature = {
241
238
  newSorting = [
242
239
  {
243
240
  id: column.id,
244
- desc: hasDescDefined ? desc! : !!sortDescFirst,
241
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
245
242
  },
246
243
  ]
247
244
  } else if (sortAction === 'add' && old?.length) {
@@ -249,7 +246,7 @@ export const Sorting: TableFeature = {
249
246
  ...old,
250
247
  {
251
248
  id: column.id,
252
- desc: hasDescDefined ? desc! : !!sortDescFirst,
249
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
253
250
  },
254
251
  ]
255
252
  // Take latest n columns
@@ -265,7 +262,7 @@ export const Sorting: TableFeature = {
265
262
  if (d.id === column.id) {
266
263
  return {
267
264
  ...d,
268
- desc: hasDescDefined ? desc! : !existingSorting?.desc,
265
+ desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
269
266
  }
270
267
  }
271
268
  return d
@@ -278,6 +275,24 @@ export const Sorting: TableFeature = {
278
275
  })
279
276
  },
280
277
 
278
+ getNextSortingOrder: () => {
279
+ const sortDescFirst =
280
+ column.columnDef.sortDescFirst ??
281
+ instance.options.sortDescFirst ??
282
+ column.getAutoSortDir() === 'desc'
283
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc'
284
+
285
+ const isSorted = column.getIsSorted()
286
+ if (!isSorted) {
287
+ return firstSortDirection
288
+ }
289
+ if (isSorted === firstSortDirection) {
290
+ return isSorted === 'desc' ? 'asc' : 'desc'
291
+ } else {
292
+ return false
293
+ }
294
+ },
295
+
281
296
  getCanSort: () => {
282
297
  return (
283
298
  (column.columnDef.enableSorting ?? true) &&