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