@tanstack/table-core 8.2.1 → 8.2.4

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;
@@ -1983,6 +1983,10 @@
1983
1983
 
1984
1984
  if (value) {
1985
1985
  preGroupedFlatRows.forEach(row => {
1986
+ if (!row.getCanSelect()) {
1987
+ return;
1988
+ }
1989
+
1986
1990
  rowSelection[row.id] = true;
1987
1991
  });
1988
1992
  } else {
@@ -2246,8 +2250,8 @@
2246
2250
  const mutateRowIsSelected = (selectedRowIds, id, value, table) => {
2247
2251
  var _row$subRows;
2248
2252
 
2249
- const row = table.getRow(id);
2250
- row.getIsGrouped(); // if ( // TODO: enforce grouping row selection rules
2253
+ const row = table.getRow(id); // const isGrouped = row.getIsGrouped()
2254
+ // if ( // TODO: enforce grouping row selection rules
2251
2255
  // !isGrouped ||
2252
2256
  // (isGrouped && table.options.enableGroupingRowSelection)
2253
2257
  // ) {
@@ -2257,7 +2261,9 @@
2257
2261
  Object.keys(selectedRowIds).forEach(key => delete selectedRowIds[key]);
2258
2262
  }
2259
2263
 
2260
- selectedRowIds[id] = true;
2264
+ if (row.getCanSelect()) {
2265
+ selectedRowIds[id] = true;
2266
+ }
2261
2267
  } else {
2262
2268
  delete selectedRowIds[id];
2263
2269
  } // }
@@ -2353,7 +2359,12 @@
2353
2359
  };
2354
2360
 
2355
2361
  const datetime = (rowA, rowB, columnId) => {
2356
- return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2362
+ const a = rowA.getValue(columnId);
2363
+ const b = rowB.getValue(columnId); // Can handle nullish values
2364
+ // Use > and < because == (and ===) doesn't work with
2365
+ // Date objects (would require calling getTime()).
2366
+
2367
+ return a > b ? 1 : a < b ? -1 : 0;
2357
2368
  };
2358
2369
 
2359
2370
  const basic = (rowA, rowB, columnId) => {
@@ -2514,18 +2525,17 @@
2514
2525
  // }
2515
2526
  // this needs to be outside of table.setSorting to be in sync with rerender
2516
2527
  const nextSortingOrder = column.getNextSortingOrder();
2528
+ const hasManualValue = typeof desc !== 'undefined' && desc !== null;
2517
2529
  table.setSorting(old => {
2518
- var _table$options$enable, _table$options$enable2;
2519
-
2520
2530
  // Find any existing sorting for this column
2521
2531
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
2522
2532
  const existingIndex = old == null ? void 0 : old.findIndex(d => d.id === column.id);
2523
- const hasDescDefined = typeof desc !== 'undefined' && desc !== null;
2524
2533
  let newSorting = []; // What should we do with this sort action?
2525
2534
 
2526
2535
  let sortAction;
2536
+ let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'; // Multi-mode
2527
2537
 
2528
- if (column.getCanMultiSort() && multi) {
2538
+ if (old != null && old.length && column.getCanMultiSort() && multi) {
2529
2539
  if (existingSorting) {
2530
2540
  sortAction = 'toggle';
2531
2541
  } else {
@@ -2543,63 +2553,71 @@
2543
2553
  } // Handle toggle states that will remove the sorting
2544
2554
 
2545
2555
 
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';
2556
+ if (sortAction === 'toggle') {
2557
+ // If we are "actually" toggling (not a manual set value), should we remove the sorting?
2558
+ if (!hasManualValue) {
2559
+ // Is our intention to remove?
2560
+ if (!nextSortingOrder) {
2561
+ sortAction = 'remove';
2562
+ }
2563
+ }
2553
2564
  }
2554
2565
 
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) {
2566
+ if (sortAction === 'add') {
2561
2567
  var _table$options$maxMul;
2562
2568
 
2563
2569
  newSorting = [...old, {
2564
2570
  id: column.id,
2565
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2571
+ desc: nextDesc
2566
2572
  }]; // Take latest n columns
2567
2573
 
2568
2574
  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) {
2575
+ } else if (sortAction === 'toggle') {
2570
2576
  // This flips (or sets) the
2571
2577
  newSorting = old.map(d => {
2572
2578
  if (d.id === column.id) {
2573
2579
  return { ...d,
2574
- desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2580
+ desc: nextDesc
2575
2581
  };
2576
2582
  }
2577
2583
 
2578
2584
  return d;
2579
2585
  });
2580
- } else if (sortAction === 'remove' && old != null && old.length) {
2586
+ } else if (sortAction === 'remove') {
2581
2587
  newSorting = old.filter(d => d.id !== column.id);
2588
+ } else {
2589
+ newSorting = [{
2590
+ id: column.id,
2591
+ desc: nextDesc
2592
+ }];
2582
2593
  }
2583
2594
 
2584
2595
  return newSorting;
2585
2596
  });
2586
2597
  },
2587
- getNextSortingOrder: () => {
2598
+ getFirstSortDir: () => {
2588
2599
  var _ref, _column$columnDef$sor;
2589
2600
 
2590
2601
  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';
2602
+ return sortDescFirst ? 'desc' : 'asc';
2603
+ },
2604
+ getNextSortingOrder: multi => {
2605
+ var _table$options$enable, _table$options$enable2;
2606
+
2607
+ const firstSortDirection = column.getFirstSortDir();
2592
2608
  const isSorted = column.getIsSorted();
2593
2609
 
2594
2610
  if (!isSorted) {
2595
2611
  return firstSortDirection;
2596
2612
  }
2597
2613
 
2598
- if (isSorted === firstSortDirection) {
2599
- return isSorted === 'desc' ? 'asc' : 'desc';
2600
- } else {
2614
+ if (isSorted !== firstSortDirection && ((_table$options$enable = table.options.enableSortingRemoval) != null ? _table$options$enable : true) && ( // If enableSortRemove, enable in general
2615
+ multi ? (_table$options$enable2 = table.options.enableMultiRemove) != null ? _table$options$enable2 : true : true) // If multi, don't allow if enableMultiRemove))
2616
+ ) {
2601
2617
  return false;
2602
2618
  }
2619
+
2620
+ return isSorted === 'desc' ? 'asc' : 'desc';
2603
2621
  },
2604
2622
  getCanSort: () => {
2605
2623
  var _column$columnDef$ena, _table$options$enable3;
@@ -3546,9 +3564,20 @@
3546
3564
  depth = 0;
3547
3565
  }
3548
3566
 
3549
- // This is the last level, just return the rows
3550
- if (depth === existingGrouping.length) {
3551
- return rows;
3567
+ // Grouping depth has been been met
3568
+ // Stop grouping and simply rewrite thd depth and row relationships
3569
+ if (depth >= existingGrouping.length) {
3570
+ return rows.map(row => {
3571
+ row.depth = depth;
3572
+ groupedFlatRows.push(row);
3573
+ groupedRowsById[row.id] = row;
3574
+
3575
+ if (row.subRows) {
3576
+ row.subRows = groupUpRecursively(row.subRows, depth + 1);
3577
+ }
3578
+
3579
+ return row;
3580
+ });
3552
3581
  }
3553
3582
 
3554
3583
  const columnId = existingGrouping[depth]; // Group the rows together for this level