@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.
@@ -31,6 +31,7 @@ export declare type SortingColumn<TGenerics extends TableGenerics> = {
31
31
  getAutoSortingFn: () => SortingFn<TGenerics>;
32
32
  getAutoSortDir: () => SortDirection;
33
33
  getSortingFn: () => SortingFn<TGenerics>;
34
+ getNextSortingOrder: () => SortDirection | false;
34
35
  getCanSort: () => boolean;
35
36
  getCanMultiSort: () => boolean;
36
37
  getSortIndex: () => number;
@@ -2523,8 +2523,10 @@
2523
2523
  // })
2524
2524
  // return
2525
2525
  // }
2526
+ // this needs to be outside of instance.setSorting to be in sync with rerender
2527
+ const nextSortingOrder = column.getNextSortingOrder();
2526
2528
  instance.setSorting(old => {
2527
- var _ref2, _column$columnDef$sor, _instance$options$ena, _instance$options$ena2;
2529
+ var _instance$options$ena, _instance$options$ena2;
2528
2530
 
2529
2531
  // Find any existing sorting for this column
2530
2532
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
@@ -2549,30 +2551,29 @@
2549
2551
  } else {
2550
2552
  sortAction = 'replace';
2551
2553
  }
2552
- }
2554
+ } // Handle toggle states that will remove the sorting
2553
2555
 
2554
- const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc'; // Handle toggle states that will remove the sorting
2555
2556
 
2556
2557
  if (sortAction === 'toggle' && ( // Must be toggling
2557
2558
  (_instance$options$ena = instance.options.enableSortingRemoval) != null ? _instance$options$ena : true) && // If enableSortRemove, enable in general
2558
2559
  !hasDescDefined && ( // Must not be setting desc
2559
- multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && ( // If multi, don't allow if enableMultiRemove
2560
- existingSorting != null && existingSorting.desc // Finally, detect if it should indeed be removed
2561
- ? !sortDescFirst : sortDescFirst)) {
2560
+ multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && // If multi, don't allow if enableMultiRemove
2561
+ !nextSortingOrder // Finally, detect if it should indeed be removed
2562
+ ) {
2562
2563
  sortAction = 'remove';
2563
2564
  }
2564
2565
 
2565
2566
  if (sortAction === 'replace') {
2566
2567
  newSorting = [{
2567
2568
  id: column.id,
2568
- desc: hasDescDefined ? desc : !!sortDescFirst
2569
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2569
2570
  }];
2570
2571
  } else if (sortAction === 'add' && old != null && old.length) {
2571
2572
  var _instance$options$max;
2572
2573
 
2573
2574
  newSorting = [...old, {
2574
2575
  id: column.id,
2575
- desc: hasDescDefined ? desc : !!sortDescFirst
2576
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2576
2577
  }]; // Take latest n columns
2577
2578
 
2578
2579
  newSorting.splice(0, newSorting.length - ((_instance$options$max = instance.options.maxMultiSortColCount) != null ? _instance$options$max : Number.MAX_SAFE_INTEGER));
@@ -2581,7 +2582,7 @@
2581
2582
  newSorting = old.map(d => {
2582
2583
  if (d.id === column.id) {
2583
2584
  return { ...d,
2584
- desc: hasDescDefined ? desc : !(existingSorting != null && existingSorting.desc)
2585
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2585
2586
  };
2586
2587
  }
2587
2588
 
@@ -2594,6 +2595,23 @@
2594
2595
  return newSorting;
2595
2596
  });
2596
2597
  },
2598
+ getNextSortingOrder: () => {
2599
+ var _ref2, _column$columnDef$sor;
2600
+
2601
+ const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc';
2602
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2603
+ const isSorted = column.getIsSorted();
2604
+
2605
+ if (!isSorted) {
2606
+ return firstSortDirection;
2607
+ }
2608
+
2609
+ if (isSorted === firstSortDirection) {
2610
+ return isSorted === 'desc' ? 'asc' : 'desc';
2611
+ } else {
2612
+ return false;
2613
+ }
2614
+ },
2597
2615
  getCanSort: () => {
2598
2616
  var _column$columnDef$ena, _instance$options$ena3;
2599
2617