@tanstack/table-core 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.
@@ -333,6 +333,7 @@ declare type SortingColumn<TData extends RowData> = {
333
333
  getAutoSortingFn: () => SortingFn<TData>;
334
334
  getAutoSortDir: () => SortDirection;
335
335
  getSortingFn: () => SortingFn<TData>;
336
+ getFirstSortDir: () => SortDirection;
336
337
  getNextSortingOrder: () => SortDirection | false;
337
338
  getCanSort: () => boolean;
338
339
  getCanMultiSort: () => boolean;
@@ -2353,7 +2353,12 @@
2353
2353
  };
2354
2354
 
2355
2355
  const datetime = (rowA, rowB, columnId) => {
2356
- return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2356
+ const a = rowA.getValue(columnId);
2357
+ const b = rowB.getValue(columnId); // Can handle nullish values
2358
+ // Use > and < because == (and ===) doesn't work with
2359
+ // Date objects (would require calling getTime()).
2360
+
2361
+ return a > b ? 1 : a < b ? -1 : 0;
2357
2362
  };
2358
2363
 
2359
2364
  const basic = (rowA, rowB, columnId) => {
@@ -2514,18 +2519,17 @@
2514
2519
  // }
2515
2520
  // this needs to be outside of table.setSorting to be in sync with rerender
2516
2521
  const nextSortingOrder = column.getNextSortingOrder();
2522
+ const hasManualValue = typeof desc !== 'undefined' && desc !== null;
2517
2523
  table.setSorting(old => {
2518
- var _table$options$enable, _table$options$enable2;
2519
-
2520
2524
  // Find any existing sorting for this column
2521
2525
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
2522
2526
  const existingIndex = old == null ? void 0 : old.findIndex(d => d.id === column.id);
2523
- const hasDescDefined = typeof desc !== 'undefined' && desc !== null;
2524
2527
  let newSorting = []; // What should we do with this sort action?
2525
2528
 
2526
2529
  let sortAction;
2530
+ let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'; // Multi-mode
2527
2531
 
2528
- if (column.getCanMultiSort() && multi) {
2532
+ if (old != null && old.length && column.getCanMultiSort() && multi) {
2529
2533
  if (existingSorting) {
2530
2534
  sortAction = 'toggle';
2531
2535
  } else {
@@ -2543,63 +2547,71 @@
2543
2547
  } // Handle toggle states that will remove the sorting
2544
2548
 
2545
2549
 
2546
- if (sortAction === 'toggle' && ( // Must be toggling
2547
- (_table$options$enable = table.options.enableSortingRemoval) != null ? _table$options$enable : true) && // If enableSortRemove, enable in general
2548
- !hasDescDefined && ( // Must not be setting desc
2549
- multi ? (_table$options$enable2 = table.options.enableMultiRemove) != null ? _table$options$enable2 : true : true) && // If multi, don't allow if enableMultiRemove
2550
- !nextSortingOrder // Finally, detect if it should indeed be removed
2551
- ) {
2552
- sortAction = 'remove';
2550
+ if (sortAction === 'toggle') {
2551
+ // If we are "actually" toggling (not a manual set value), should we remove the sorting?
2552
+ if (!hasManualValue) {
2553
+ // Is our intention to remove?
2554
+ if (!nextSortingOrder) {
2555
+ sortAction = 'remove';
2556
+ }
2557
+ }
2553
2558
  }
2554
2559
 
2555
- if (sortAction === 'replace') {
2556
- newSorting = [{
2557
- id: column.id,
2558
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2559
- }];
2560
- } else if (sortAction === 'add' && old != null && old.length) {
2560
+ if (sortAction === 'add') {
2561
2561
  var _table$options$maxMul;
2562
2562
 
2563
2563
  newSorting = [...old, {
2564
2564
  id: column.id,
2565
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2565
+ desc: nextDesc
2566
2566
  }]; // Take latest n columns
2567
2567
 
2568
2568
  newSorting.splice(0, newSorting.length - ((_table$options$maxMul = table.options.maxMultiSortColCount) != null ? _table$options$maxMul : Number.MAX_SAFE_INTEGER));
2569
- } else if (sortAction === 'toggle' && old != null && old.length) {
2569
+ } else if (sortAction === 'toggle') {
2570
2570
  // This flips (or sets) the
2571
2571
  newSorting = old.map(d => {
2572
2572
  if (d.id === column.id) {
2573
2573
  return { ...d,
2574
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2574
+ desc: nextDesc
2575
2575
  };
2576
2576
  }
2577
2577
 
2578
2578
  return d;
2579
2579
  });
2580
- } else if (sortAction === 'remove' && old != null && old.length) {
2580
+ } else if (sortAction === 'remove') {
2581
2581
  newSorting = old.filter(d => d.id !== column.id);
2582
+ } else {
2583
+ newSorting = [{
2584
+ id: column.id,
2585
+ desc: nextDesc
2586
+ }];
2582
2587
  }
2583
2588
 
2584
2589
  return newSorting;
2585
2590
  });
2586
2591
  },
2587
- getNextSortingOrder: () => {
2592
+ getFirstSortDir: () => {
2588
2593
  var _ref, _column$columnDef$sor;
2589
2594
 
2590
2595
  const sortDescFirst = (_ref = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : table.options.sortDescFirst) != null ? _ref : column.getAutoSortDir() === 'desc';
2591
- const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2596
+ return sortDescFirst ? 'desc' : 'asc';
2597
+ },
2598
+ getNextSortingOrder: multi => {
2599
+ var _table$options$enable, _table$options$enable2;
2600
+
2601
+ const firstSortDirection = column.getFirstSortDir();
2592
2602
  const isSorted = column.getIsSorted();
2593
2603
 
2594
2604
  if (!isSorted) {
2595
2605
  return firstSortDirection;
2596
2606
  }
2597
2607
 
2598
- if (isSorted === firstSortDirection) {
2599
- return isSorted === 'desc' ? 'asc' : 'desc';
2600
- } else {
2608
+ if (isSorted !== firstSortDirection && ((_table$options$enable = table.options.enableSortingRemoval) != null ? _table$options$enable : true) && ( // If enableSortRemove, enable in general
2609
+ multi ? (_table$options$enable2 = table.options.enableMultiRemove) != null ? _table$options$enable2 : true : true) // If multi, don't allow if enableMultiRemove))
2610
+ ) {
2601
2611
  return false;
2602
2612
  }
2613
+
2614
+ return isSorted === 'desc' ? 'asc' : 'desc';
2603
2615
  },
2604
2616
  getCanSort: () => {
2605
2617
  var _column$columnDef$ena, _table$options$enable3;