@tanstack/react-table 8.0.0-alpha.86 → 8.0.0-alpha.89

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.
@@ -122,7 +122,7 @@ function createColumn(instance, columnDef, depth, parent) {
122
122
  throw new Error();
123
123
  }
124
124
 
125
- let column = { ...columnDef,
125
+ let column = {
126
126
  id: "" + id,
127
127
  accessorFn,
128
128
  parent: parent,
@@ -886,8 +886,12 @@ const Expanding = {
886
886
  getIsAllRowsExpanded: () => {
887
887
  const expanded = instance.getState().expanded; // If expanded is true, save some cycles and return true
888
888
 
889
- if (expanded === true) {
890
- return true;
889
+ if (typeof expanded === 'boolean') {
890
+ return expanded === true;
891
+ }
892
+
893
+ if (!Object.keys(expanded).length) {
894
+ return false;
891
895
  } // If any row is not expanded, return false
892
896
 
893
897
 
@@ -2342,21 +2346,54 @@ function isRowSelected(row, selection, instance) {
2342
2346
  }
2343
2347
 
2344
2348
  const reSplitAlphaNumeric = /([0-9]+)/gm;
2345
- const sortingFns = {
2346
- alphanumeric,
2347
- alphanumericCaseSensitive,
2348
- text,
2349
- textCaseSensitive,
2350
- datetime,
2351
- basic
2352
- };
2353
2349
 
2354
- function alphanumeric(rowA, rowB, columnId) {
2350
+ const alphanumeric = (rowA, rowB, columnId) => {
2355
2351
  return compareAlphanumeric(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2356
- }
2352
+ };
2357
2353
 
2358
- function alphanumericCaseSensitive(rowA, rowB, columnId) {
2354
+ const alphanumericCaseSensitive = (rowA, rowB, columnId) => {
2359
2355
  return compareAlphanumeric(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2356
+ }; // The text filter is more basic (less numeric support)
2357
+ // but is much faster
2358
+
2359
+
2360
+ const text = (rowA, rowB, columnId) => {
2361
+ return compareBasic(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2362
+ }; // The text filter is more basic (less numeric support)
2363
+ // but is much faster
2364
+
2365
+
2366
+ const textCaseSensitive = (rowA, rowB, columnId) => {
2367
+ return compareBasic(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2368
+ };
2369
+
2370
+ const datetime = (rowA, rowB, columnId) => {
2371
+ return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2372
+ };
2373
+
2374
+ const basic = (rowA, rowB, columnId) => {
2375
+ return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId));
2376
+ }; // Utils
2377
+
2378
+
2379
+ function compareBasic(a, b) {
2380
+ return a === b ? 0 : a > b ? 1 : -1;
2381
+ }
2382
+
2383
+ function toString(a) {
2384
+ if (typeof a === 'number') {
2385
+ if (isNaN(a) || a === Infinity || a === -Infinity) {
2386
+ return '';
2387
+ }
2388
+
2389
+ return String(a);
2390
+ }
2391
+
2392
+ if (typeof a === 'string') {
2393
+ return a;
2394
+ }
2395
+
2396
+ return '';
2360
2397
  } // Mixed sorting is slow, but very inclusive of many edge cases.
2361
2398
  // It handles numbers, mixed alphanumeric combinations, and even
2362
2399
  // null, undefined, and Infinity
@@ -2403,48 +2440,17 @@ function compareAlphanumeric(aStr, bStr) {
2403
2440
  }
2404
2441
 
2405
2442
  return a.length - b.length;
2406
- } // The text filter is more basic (less numeric support)
2407
- // but is much faster
2408
-
2409
-
2410
- function text(rowA, rowB, columnId) {
2411
- return compareBasic(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2412
- } // The text filter is more basic (less numeric support)
2413
- // but is much faster
2414
-
2415
-
2416
- function textCaseSensitive(rowA, rowB, columnId) {
2417
- return compareBasic(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2418
- }
2419
-
2420
- function datetime(rowA, rowB, columnId) {
2421
- return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2422
- }
2423
-
2424
- function basic(rowA, rowB, columnId) {
2425
- return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId));
2426
- } // Utils
2427
-
2428
-
2429
- function compareBasic(a, b) {
2430
- return a === b ? 0 : a > b ? 1 : -1;
2431
- }
2443
+ } // Exports
2432
2444
 
2433
- function toString(a) {
2434
- if (typeof a === 'number') {
2435
- if (isNaN(a) || a === Infinity || a === -Infinity) {
2436
- return '';
2437
- }
2438
-
2439
- return String(a);
2440
- }
2441
2445
 
2442
- if (typeof a === 'string') {
2443
- return a;
2444
- }
2445
-
2446
- return '';
2447
- }
2446
+ const sortingFns = {
2447
+ alphanumeric,
2448
+ alphanumericCaseSensitive,
2449
+ text,
2450
+ textCaseSensitive,
2451
+ datetime,
2452
+ basic
2453
+ };
2448
2454
 
2449
2455
  //
2450
2456
  const Sorting = {
@@ -2525,8 +2531,10 @@ const Sorting = {
2525
2531
  // })
2526
2532
  // return
2527
2533
  // }
2534
+ // this needs to be outside of instance.setSorting to be in sync with rerender
2535
+ const nextSortingOrder = column.getNextSortingOrder();
2528
2536
  instance.setSorting(old => {
2529
- var _ref2, _column$columnDef$sor, _instance$options$ena, _instance$options$ena2;
2537
+ var _instance$options$ena, _instance$options$ena2;
2530
2538
 
2531
2539
  // Find any existing sorting for this column
2532
2540
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
@@ -2551,30 +2559,29 @@ const Sorting = {
2551
2559
  } else {
2552
2560
  sortAction = 'replace';
2553
2561
  }
2554
- }
2562
+ } // Handle toggle states that will remove the sorting
2555
2563
 
2556
- 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
2557
2564
 
2558
2565
  if (sortAction === 'toggle' && ( // Must be toggling
2559
2566
  (_instance$options$ena = instance.options.enableSortingRemoval) != null ? _instance$options$ena : true) && // If enableSortRemove, enable in general
2560
2567
  !hasDescDefined && ( // Must not be setting desc
2561
- multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && ( // If multi, don't allow if enableMultiRemove
2562
- existingSorting != null && existingSorting.desc // Finally, detect if it should indeed be removed
2563
- ? !sortDescFirst : sortDescFirst)) {
2568
+ multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && // If multi, don't allow if enableMultiRemove
2569
+ !nextSortingOrder // Finally, detect if it should indeed be removed
2570
+ ) {
2564
2571
  sortAction = 'remove';
2565
2572
  }
2566
2573
 
2567
2574
  if (sortAction === 'replace') {
2568
2575
  newSorting = [{
2569
2576
  id: column.id,
2570
- desc: hasDescDefined ? desc : !!sortDescFirst
2577
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2571
2578
  }];
2572
2579
  } else if (sortAction === 'add' && old != null && old.length) {
2573
2580
  var _instance$options$max;
2574
2581
 
2575
2582
  newSorting = [...old, {
2576
2583
  id: column.id,
2577
- desc: hasDescDefined ? desc : !!sortDescFirst
2584
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2578
2585
  }]; // Take latest n columns
2579
2586
 
2580
2587
  newSorting.splice(0, newSorting.length - ((_instance$options$max = instance.options.maxMultiSortColCount) != null ? _instance$options$max : Number.MAX_SAFE_INTEGER));
@@ -2583,7 +2590,7 @@ const Sorting = {
2583
2590
  newSorting = old.map(d => {
2584
2591
  if (d.id === column.id) {
2585
2592
  return { ...d,
2586
- desc: hasDescDefined ? desc : !(existingSorting != null && existingSorting.desc)
2593
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2587
2594
  };
2588
2595
  }
2589
2596
 
@@ -2596,6 +2603,23 @@ const Sorting = {
2596
2603
  return newSorting;
2597
2604
  });
2598
2605
  },
2606
+ getNextSortingOrder: () => {
2607
+ var _ref2, _column$columnDef$sor;
2608
+
2609
+ const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc';
2610
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2611
+ const isSorted = column.getIsSorted();
2612
+
2613
+ if (!isSorted) {
2614
+ return firstSortDirection;
2615
+ }
2616
+
2617
+ if (isSorted === firstSortDirection) {
2618
+ return isSorted === 'desc' ? 'asc' : 'desc';
2619
+ } else {
2620
+ return false;
2621
+ }
2622
+ },
2599
2623
  getCanSort: () => {
2600
2624
  var _column$columnDef$ena, _instance$options$ena3;
2601
2625
 
@@ -2696,8 +2720,8 @@ const Visibility = {
2696
2720
  },
2697
2721
  createRow: (row, instance) => {
2698
2722
  return {
2699
- _getAllVisibleCells: memo(() => [row.getAllCells().filter(cell => cell.column.getIsVisible()).map(d => d.id).join('_')], _ => {
2700
- return row.getAllCells().filter(cell => cell.column.getIsVisible());
2723
+ _getAllVisibleCells: memo(() => [row.getAllCells(), instance.getState().columnVisibility], cells => {
2724
+ return cells.filter(cell => cell.column.getIsVisible());
2701
2725
  }, {
2702
2726
  key: process.env.NODE_ENV === 'production' && 'row._getAllVisibleCells',
2703
2727
  debug: () => {
@@ -3050,13 +3074,15 @@ function createCell(instance, row, column, columnId) {
3050
3074
  row,
3051
3075
  column,
3052
3076
  getValue: () => row.getValue(columnId),
3053
- renderCell: () => column.columnDef.cell ? instance._render(column.columnDef.cell, {
3054
- instance,
3055
- column,
3056
- row,
3057
- cell: cell,
3058
- getValue: cell.getValue
3059
- }) : null
3077
+ renderCell: () => {
3078
+ return column.columnDef.cell ? instance._render(column.columnDef.cell, {
3079
+ instance,
3080
+ column,
3081
+ row,
3082
+ cell: cell,
3083
+ getValue: cell.getValue
3084
+ }) : null;
3085
+ }
3060
3086
  };
3061
3087
 
3062
3088
  instance._features.forEach(feature => {
@@ -3131,27 +3157,23 @@ function getCoreRowModel() {
3131
3157
  flatRows: [],
3132
3158
  rowsById: {}
3133
3159
  };
3134
- let rows;
3135
- let row;
3136
- let originalRow;
3137
3160
 
3138
3161
  const accessRows = function (originalRows, depth, parent) {
3139
3162
  if (depth === void 0) {
3140
3163
  depth = 0;
3141
3164
  }
3142
3165
 
3143
- rows = [];
3166
+ const rows = [];
3144
3167
 
3145
3168
  for (let i = 0; i < originalRows.length; i++) {
3146
- originalRow = originalRows[i]; // This could be an expensive check at scale, so we should move it somewhere else, but where?
3169
+ // This could be an expensive check at scale, so we should move it somewhere else, but where?
3147
3170
  // if (!id) {
3148
3171
  // if (process.env.NODE_ENV !== 'production') {
3149
3172
  // throw new Error(`getRowId expected an ID, but got ${id}`)
3150
3173
  // }
3151
3174
  // }
3152
3175
  // Make the row
3153
-
3154
- row = createRow(instance, instance._getRowId(originalRow, i, parent), originalRow, i, depth); // Keep track of every row in a flat array
3176
+ const row = createRow(instance, instance._getRowId(originalRows[i], i, parent), originalRows[i], i, depth); // Keep track of every row in a flat array
3155
3177
 
3156
3178
  rowModel.flatRows.push(row); // Also keep track of every row by its ID
3157
3179
 
@@ -3162,7 +3184,7 @@ function getCoreRowModel() {
3162
3184
  if (instance.options.getSubRows) {
3163
3185
  var _row$originalSubRows;
3164
3186
 
3165
- row.originalSubRows = instance.options.getSubRows(originalRow, i); // Then recursively access them
3187
+ row.originalSubRows = instance.options.getSubRows(originalRows[i], i); // Then recursively access them
3166
3188
 
3167
3189
  if ((_row$originalSubRows = row.originalSubRows) != null && _row$originalSubRows.length) {
3168
3190
  row.subRows = accessRows(row.originalSubRows, depth + 1, row);