@tanstack/react-table 8.2.1 → 8.2.2

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.
@@ -2361,7 +2361,12 @@ const textCaseSensitive = (rowA, rowB, columnId) => {
2361
2361
  };
2362
2362
 
2363
2363
  const datetime = (rowA, rowB, columnId) => {
2364
- return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2364
+ const a = rowA.getValue(columnId);
2365
+ const b = rowB.getValue(columnId); // Can handle nullish values
2366
+ // Use > and < because == (and ===) doesn't work with
2367
+ // Date objects (would require calling getTime()).
2368
+
2369
+ return a > b ? 1 : a < b ? -1 : 0;
2365
2370
  };
2366
2371
 
2367
2372
  const basic = (rowA, rowB, columnId) => {
@@ -2522,18 +2527,17 @@ const Sorting = {
2522
2527
  // }
2523
2528
  // this needs to be outside of table.setSorting to be in sync with rerender
2524
2529
  const nextSortingOrder = column.getNextSortingOrder();
2530
+ const hasManualValue = typeof desc !== 'undefined' && desc !== null;
2525
2531
  table.setSorting(old => {
2526
- var _table$options$enable, _table$options$enable2;
2527
-
2528
2532
  // Find any existing sorting for this column
2529
2533
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
2530
2534
  const existingIndex = old == null ? void 0 : old.findIndex(d => d.id === column.id);
2531
- const hasDescDefined = typeof desc !== 'undefined' && desc !== null;
2532
2535
  let newSorting = []; // What should we do with this sort action?
2533
2536
 
2534
2537
  let sortAction;
2538
+ let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'; // Multi-mode
2535
2539
 
2536
- if (column.getCanMultiSort() && multi) {
2540
+ if (old != null && old.length && column.getCanMultiSort() && multi) {
2537
2541
  if (existingSorting) {
2538
2542
  sortAction = 'toggle';
2539
2543
  } else {
@@ -2551,63 +2555,71 @@ const Sorting = {
2551
2555
  } // Handle toggle states that will remove the sorting
2552
2556
 
2553
2557
 
2554
- if (sortAction === 'toggle' && ( // Must be toggling
2555
- (_table$options$enable = table.options.enableSortingRemoval) != null ? _table$options$enable : true) && // If enableSortRemove, enable in general
2556
- !hasDescDefined && ( // Must not be setting desc
2557
- multi ? (_table$options$enable2 = table.options.enableMultiRemove) != null ? _table$options$enable2 : true : true) && // If multi, don't allow if enableMultiRemove
2558
- !nextSortingOrder // Finally, detect if it should indeed be removed
2559
- ) {
2560
- sortAction = 'remove';
2558
+ if (sortAction === 'toggle') {
2559
+ // If we are "actually" toggling (not a manual set value), should we remove the sorting?
2560
+ if (!hasManualValue) {
2561
+ // Is our intention to remove?
2562
+ if (!nextSortingOrder) {
2563
+ sortAction = 'remove';
2564
+ }
2565
+ }
2561
2566
  }
2562
2567
 
2563
- if (sortAction === 'replace') {
2564
- newSorting = [{
2565
- id: column.id,
2566
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2567
- }];
2568
- } else if (sortAction === 'add' && old != null && old.length) {
2568
+ if (sortAction === 'add') {
2569
2569
  var _table$options$maxMul;
2570
2570
 
2571
2571
  newSorting = [...old, {
2572
2572
  id: column.id,
2573
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2573
+ desc: nextDesc
2574
2574
  }]; // Take latest n columns
2575
2575
 
2576
2576
  newSorting.splice(0, newSorting.length - ((_table$options$maxMul = table.options.maxMultiSortColCount) != null ? _table$options$maxMul : Number.MAX_SAFE_INTEGER));
2577
- } else if (sortAction === 'toggle' && old != null && old.length) {
2577
+ } else if (sortAction === 'toggle') {
2578
2578
  // This flips (or sets) the
2579
2579
  newSorting = old.map(d => {
2580
2580
  if (d.id === column.id) {
2581
2581
  return { ...d,
2582
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2582
+ desc: nextDesc
2583
2583
  };
2584
2584
  }
2585
2585
 
2586
2586
  return d;
2587
2587
  });
2588
- } else if (sortAction === 'remove' && old != null && old.length) {
2588
+ } else if (sortAction === 'remove') {
2589
2589
  newSorting = old.filter(d => d.id !== column.id);
2590
+ } else {
2591
+ newSorting = [{
2592
+ id: column.id,
2593
+ desc: nextDesc
2594
+ }];
2590
2595
  }
2591
2596
 
2592
2597
  return newSorting;
2593
2598
  });
2594
2599
  },
2595
- getNextSortingOrder: () => {
2600
+ getFirstSortDir: () => {
2596
2601
  var _ref, _column$columnDef$sor;
2597
2602
 
2598
2603
  const sortDescFirst = (_ref = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : table.options.sortDescFirst) != null ? _ref : column.getAutoSortDir() === 'desc';
2599
- const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2604
+ return sortDescFirst ? 'desc' : 'asc';
2605
+ },
2606
+ getNextSortingOrder: multi => {
2607
+ var _table$options$enable, _table$options$enable2;
2608
+
2609
+ const firstSortDirection = column.getFirstSortDir();
2600
2610
  const isSorted = column.getIsSorted();
2601
2611
 
2602
2612
  if (!isSorted) {
2603
2613
  return firstSortDirection;
2604
2614
  }
2605
2615
 
2606
- if (isSorted === firstSortDirection) {
2607
- return isSorted === 'desc' ? 'asc' : 'desc';
2608
- } else {
2616
+ if (isSorted !== firstSortDirection && ((_table$options$enable = table.options.enableSortingRemoval) != null ? _table$options$enable : true) && ( // If enableSortRemove, enable in general
2617
+ multi ? (_table$options$enable2 = table.options.enableMultiRemove) != null ? _table$options$enable2 : true : true) // If multi, don't allow if enableMultiRemove))
2618
+ ) {
2609
2619
  return false;
2610
2620
  }
2621
+
2622
+ return isSorted === 'desc' ? 'asc' : 'desc';
2611
2623
  },
2612
2624
  getCanSort: () => {
2613
2625
  var _column$columnDef$ena, _table$options$enable3;