@tanstack/react-table 8.0.0-beta.3 → 8.0.0-beta.6

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.
@@ -826,7 +826,6 @@ const Expanding = {
826
826
  getDefaultOptions: instance => {
827
827
  return {
828
828
  onExpandedChange: makeStateUpdater('expanded', instance),
829
- autoResetExpanded: true,
830
829
  paginateExpandedRows: true
831
830
  };
832
831
  },
@@ -835,6 +834,8 @@ const Expanding = {
835
834
  let queued = false;
836
835
  return {
837
836
  _autoResetExpanded: () => {
837
+ var _ref, _instance$options$aut;
838
+
838
839
  if (!registered) {
839
840
  instance._queue(() => {
840
841
  registered = true;
@@ -843,11 +844,7 @@ const Expanding = {
843
844
  return;
844
845
  }
845
846
 
846
- if (instance.options.autoResetAll === false) {
847
- return;
848
- }
849
-
850
- if (instance.options.autoResetAll === true || instance.options.autoResetExpanded) {
847
+ if ((_ref = (_instance$options$aut = instance.options.autoResetAll) != null ? _instance$options$aut : instance.options.autoResetExpanded) != null ? _ref : !instance.options.manualExpanding) {
851
848
  if (queued) return;
852
849
  queued = true;
853
850
 
@@ -911,7 +908,7 @@ const Expanding = {
911
908
  });
912
909
  return maxDepth;
913
910
  },
914
- getPreExpandedRowModel: () => instance.getGroupedRowModel(),
911
+ getPreExpandedRowModel: () => instance.getSortedRowModel(),
915
912
  getExpandedRowModel: () => {
916
913
  if (!instance._getExpandedRowModel && instance.options.getExpandedRowModel) {
917
914
  instance._getExpandedRowModel = instance.options.getExpandedRowModel(instance);
@@ -1512,7 +1509,7 @@ const Grouping = {
1512
1509
 
1513
1510
  instance.setGrouping(defaultState ? [] : (_instance$initialStat = (_instance$initialStat2 = instance.initialState) == null ? void 0 : _instance$initialStat2.grouping) != null ? _instance$initialStat : []);
1514
1511
  },
1515
- getPreGroupedRowModel: () => instance.getSortedRowModel(),
1512
+ getPreGroupedRowModel: () => instance.getFilteredRowModel(),
1516
1513
  getGroupedRowModel: () => {
1517
1514
  if (!instance._getGroupedRowModel && instance.options.getGroupedRowModel) {
1518
1515
  instance._getGroupedRowModel = instance.options.getGroupedRowModel(instance);
@@ -1533,6 +1530,12 @@ const Grouping = {
1533
1530
  };
1534
1531
  },
1535
1532
  createCell: (cell, column, row, instance) => {
1533
+ const getRenderValue = () => {
1534
+ var _cell$getValue;
1535
+
1536
+ return (_cell$getValue = cell.getValue()) != null ? _cell$getValue : instance.options.renderFallbackValue;
1537
+ };
1538
+
1536
1539
  return {
1537
1540
  getIsGrouped: () => column.getIsGrouped() && column.id === row.groupingColumnId,
1538
1541
  getIsPlaceholder: () => !cell.getIsGrouped() && column.getIsGrouped(),
@@ -1542,15 +1545,19 @@ const Grouping = {
1542
1545
  return !cell.getIsGrouped() && !cell.getIsPlaceholder() && ((_row$subRows = row.subRows) == null ? void 0 : _row$subRows.length) > 1;
1543
1546
  },
1544
1547
  renderAggregatedCell: () => {
1545
- var _column$columnDef$agg;
1548
+ if (process.env.NODE_ENV === 'development') {
1549
+ if (!column.columnDef.aggregatedCell) {
1550
+ console.warn('A columnDef.aggregatedCell template is recommended for displaying aggregated values.');
1551
+ }
1552
+ }
1546
1553
 
1547
- const template = (_column$columnDef$agg = column.columnDef.aggregatedCell) != null ? _column$columnDef$agg : column.columnDef.cell;
1554
+ const template = column.columnDef.aggregatedCell || column.columnDef.cell;
1548
1555
  return template ? instance._render(template, {
1549
1556
  instance,
1550
1557
  column,
1551
1558
  row,
1552
1559
  cell,
1553
- getValue: cell.getValue
1560
+ getValue: getRenderValue
1554
1561
  }) : null;
1555
1562
  }
1556
1563
  };
@@ -2020,11 +2027,11 @@ const RowSelection = {
2020
2027
  });
2021
2028
  },
2022
2029
  toggleAllPageRowsSelected: value => instance.setRowSelection(old => {
2023
- typeof value !== 'undefined' ? value : !instance.getIsAllPageRowsSelected();
2030
+ const resolvedValue = typeof value !== 'undefined' ? value : !instance.getIsAllPageRowsSelected();
2024
2031
  const rowSelection = { ...old
2025
2032
  };
2026
2033
  instance.getRowModel().rows.forEach(row => {
2027
- mutateRowIsSelected(rowSelection, row.id, value, instance);
2034
+ mutateRowIsSelected(rowSelection, row.id, resolvedValue, instance);
2028
2035
  });
2029
2036
  return rowSelection;
2030
2037
  }),
@@ -2115,7 +2122,7 @@ const RowSelection = {
2115
2122
  return (_instance$options$deb2 = instance.options.debugAll) != null ? _instance$options$deb2 : instance.options.debugTable;
2116
2123
  }
2117
2124
  }),
2118
- getGroupedSelectedRowModel: memo(() => [instance.getState().rowSelection, instance.getGroupedRowModel()], (rowSelection, rowModel) => {
2125
+ getGroupedSelectedRowModel: memo(() => [instance.getState().rowSelection, instance.getSortedRowModel()], (rowSelection, rowModel) => {
2119
2126
  if (!Object.keys(rowSelection).length) {
2120
2127
  return {
2121
2128
  rows: [],
@@ -2179,7 +2186,7 @@ const RowSelection = {
2179
2186
  },
2180
2187
  getIsSomePageRowsSelected: () => {
2181
2188
  const paginationFlatRows = instance.getPaginationRowModel().flatRows;
2182
- return instance.getIsAllPageRowsSelected() ? false : !!(paginationFlatRows != null && paginationFlatRows.length);
2189
+ return instance.getIsAllPageRowsSelected() ? false : paginationFlatRows.some(d => d.getIsSelected() || d.getIsSomeSelected());
2183
2190
  },
2184
2191
  getToggleAllRowsSelectedHandler: () => {
2185
2192
  return e => {
@@ -2533,8 +2540,10 @@ const Sorting = {
2533
2540
  // })
2534
2541
  // return
2535
2542
  // }
2543
+ // this needs to be outside of instance.setSorting to be in sync with rerender
2544
+ const nextSortingOrder = column.getNextSortingOrder();
2536
2545
  instance.setSorting(old => {
2537
- var _ref2, _column$columnDef$sor, _instance$options$ena, _instance$options$ena2;
2546
+ var _instance$options$ena, _instance$options$ena2;
2538
2547
 
2539
2548
  // Find any existing sorting for this column
2540
2549
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
@@ -2559,30 +2568,29 @@ const Sorting = {
2559
2568
  } else {
2560
2569
  sortAction = 'replace';
2561
2570
  }
2562
- }
2571
+ } // Handle toggle states that will remove the sorting
2563
2572
 
2564
- 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
2565
2573
 
2566
2574
  if (sortAction === 'toggle' && ( // Must be toggling
2567
2575
  (_instance$options$ena = instance.options.enableSortingRemoval) != null ? _instance$options$ena : true) && // If enableSortRemove, enable in general
2568
2576
  !hasDescDefined && ( // Must not be setting desc
2569
- multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && ( // If multi, don't allow if enableMultiRemove
2570
- existingSorting != null && existingSorting.desc // Finally, detect if it should indeed be removed
2571
- ? !sortDescFirst : sortDescFirst)) {
2577
+ multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && // If multi, don't allow if enableMultiRemove
2578
+ !nextSortingOrder // Finally, detect if it should indeed be removed
2579
+ ) {
2572
2580
  sortAction = 'remove';
2573
2581
  }
2574
2582
 
2575
2583
  if (sortAction === 'replace') {
2576
2584
  newSorting = [{
2577
2585
  id: column.id,
2578
- desc: hasDescDefined ? desc : !!sortDescFirst
2586
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2579
2587
  }];
2580
2588
  } else if (sortAction === 'add' && old != null && old.length) {
2581
2589
  var _instance$options$max;
2582
2590
 
2583
2591
  newSorting = [...old, {
2584
2592
  id: column.id,
2585
- desc: hasDescDefined ? desc : !!sortDescFirst
2593
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2586
2594
  }]; // Take latest n columns
2587
2595
 
2588
2596
  newSorting.splice(0, newSorting.length - ((_instance$options$max = instance.options.maxMultiSortColCount) != null ? _instance$options$max : Number.MAX_SAFE_INTEGER));
@@ -2591,7 +2599,7 @@ const Sorting = {
2591
2599
  newSorting = old.map(d => {
2592
2600
  if (d.id === column.id) {
2593
2601
  return { ...d,
2594
- desc: hasDescDefined ? desc : !(existingSorting != null && existingSorting.desc)
2602
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2595
2603
  };
2596
2604
  }
2597
2605
 
@@ -2604,6 +2612,23 @@ const Sorting = {
2604
2612
  return newSorting;
2605
2613
  });
2606
2614
  },
2615
+ getNextSortingOrder: () => {
2616
+ var _ref2, _column$columnDef$sor;
2617
+
2618
+ const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc';
2619
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2620
+ const isSorted = column.getIsSorted();
2621
+
2622
+ if (!isSorted) {
2623
+ return firstSortDirection;
2624
+ }
2625
+
2626
+ if (isSorted === firstSortDirection) {
2627
+ return isSorted === 'desc' ? 'asc' : 'desc';
2628
+ } else {
2629
+ return false;
2630
+ }
2631
+ },
2607
2632
  getCanSort: () => {
2608
2633
  var _column$columnDef$ena, _instance$options$ena3;
2609
2634
 
@@ -2647,7 +2672,7 @@ const Sorting = {
2647
2672
 
2648
2673
  instance.setSorting(defaultState ? [] : (_instance$initialStat = (_instance$initialStat2 = instance.initialState) == null ? void 0 : _instance$initialStat2.sorting) != null ? _instance$initialStat : []);
2649
2674
  },
2650
- getPreSortedRowModel: () => instance.getFilteredRowModel(),
2675
+ getPreSortedRowModel: () => instance.getGroupedRowModel(),
2651
2676
  getSortedRowModel: () => {
2652
2677
  if (!instance._getSortedRowModel && instance.options.getSortedRowModel) {
2653
2678
  instance._getSortedRowModel = instance.options.getSortedRowModel(instance);
@@ -3053,6 +3078,12 @@ function createTable$1(_, __, options) {
3053
3078
  }
3054
3079
 
3055
3080
  function createCell(instance, row, column, columnId) {
3081
+ const getRenderValue = () => {
3082
+ var _cell$getValue;
3083
+
3084
+ return (_cell$getValue = cell.getValue()) != null ? _cell$getValue : instance.options.renderFallbackValue;
3085
+ };
3086
+
3056
3087
  const cell = {
3057
3088
  id: row.id + "_" + column.id,
3058
3089
  row,
@@ -3064,7 +3095,7 @@ function createCell(instance, row, column, columnId) {
3064
3095
  column,
3065
3096
  row,
3066
3097
  cell: cell,
3067
- getValue: cell.getValue
3098
+ getValue: getRenderValue
3068
3099
  }) : null;
3069
3100
  }
3070
3101
  };
@@ -3793,20 +3824,34 @@ function getPaginationRowModel(opts) {
3793
3824
  const pageStart = pageSize * pageIndex;
3794
3825
  const pageEnd = pageStart + pageSize;
3795
3826
  rows = rows.slice(pageStart, pageEnd);
3827
+ let paginatedRowModel;
3796
3828
 
3797
3829
  if (!instance.options.paginateExpandedRows) {
3798
- return expandRows({
3830
+ paginatedRowModel = expandRows({
3799
3831
  rows,
3800
3832
  flatRows,
3801
3833
  rowsById
3802
3834
  });
3835
+ } else {
3836
+ paginatedRowModel = {
3837
+ rows,
3838
+ flatRows,
3839
+ rowsById
3840
+ };
3803
3841
  }
3804
3842
 
3805
- return {
3806
- rows,
3807
- flatRows,
3808
- rowsById
3843
+ paginatedRowModel.flatRows = [];
3844
+
3845
+ const handleRow = row => {
3846
+ paginatedRowModel.flatRows.push(row);
3847
+
3848
+ if (row.subRows.length) {
3849
+ row.subRows.forEach(handleRow);
3850
+ }
3809
3851
  };
3852
+
3853
+ paginatedRowModel.rows.forEach(handleRow);
3854
+ return paginatedRowModel;
3810
3855
  }, {
3811
3856
  key: process.env.NODE_ENV === 'development' && 'getPaginationRowModel',
3812
3857
  debug: () => {
@@ -3848,6 +3893,7 @@ function useTableInstance(table, options) {
3848
3893
  onStateChange: () => {},
3849
3894
  // noop
3850
3895
  render,
3896
+ renderFallbackValue: null,
3851
3897
  ...options
3852
3898
  }; // Create a new table instance and store it in state
3853
3899