@tanstack/table-core 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.
@@ -9,13 +9,13 @@ export declare type ColumnSort = {
9
9
  desc: boolean;
10
10
  };
11
11
  export declare type SortingState = ColumnSort[];
12
+ export declare type SortingTableState = {
13
+ sorting: SortingState;
14
+ };
12
15
  export declare type SortingFn<TGenerics extends TableGenerics> = {
13
16
  (rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): number;
14
17
  };
15
18
  export declare type CustomSortingFns<TGenerics extends TableGenerics> = Record<string, SortingFn<TGenerics>>;
16
- export declare type SortingTableState = {
17
- sorting: SortingState;
18
- };
19
19
  export declare type SortingFnOption<TGenerics extends TableGenerics> = 'auto' | BuiltInSortingFn | keyof TGenerics['SortingFns'] | SortingFn<TGenerics>;
20
20
  export declare type SortingColumnDef<TGenerics extends TableGenerics> = {
21
21
  sortingFn?: SortingFnOption<Overwrite<TGenerics, {
@@ -31,6 +31,7 @@ export declare type SortingColumn<TGenerics extends TableGenerics> = {
31
31
  getAutoSortingFn: () => SortingFn<TGenerics>;
32
32
  getAutoSortDir: () => SortDirection;
33
33
  getSortingFn: () => SortingFn<TGenerics>;
34
+ getNextSortingOrder: () => SortDirection | false;
34
35
  getCanSort: () => boolean;
35
36
  getCanMultiSort: () => boolean;
36
37
  getSortIndex: () => number;
@@ -5,7 +5,6 @@ export * from './core/column';
5
5
  export * from './core/headers';
6
6
  export * from './core/row';
7
7
  export * from './features/ColumnSizing';
8
- export * from './features/ColumnSizing';
9
8
  export * from './features/Expanding';
10
9
  export * from './features/Filters';
11
10
  export * from './features/Grouping';
@@ -15,7 +14,6 @@ export * from './features/Pinning';
15
14
  export * from './features/RowSelection';
16
15
  export * from './features/Sorting';
17
16
  export * from './features/Visibility';
18
- export * from './features/Expanding';
19
17
  export * from './filterFns';
20
18
  export * from './sortingFns';
21
19
  export * from './aggregationFns';
@@ -1,18 +1,11 @@
1
- import { TableGenerics, Row } from './types';
1
+ import { SortingFn } from './features/Sorting';
2
2
  export declare const reSplitAlphaNumeric: RegExp;
3
3
  export declare const sortingFns: {
4
- alphanumeric: typeof alphanumeric;
5
- alphanumericCaseSensitive: typeof alphanumericCaseSensitive;
6
- text: typeof text;
7
- textCaseSensitive: typeof textCaseSensitive;
8
- datetime: typeof datetime;
9
- basic: typeof basic;
4
+ alphanumeric: SortingFn<any>;
5
+ alphanumericCaseSensitive: SortingFn<any>;
6
+ text: SortingFn<any>;
7
+ textCaseSensitive: SortingFn<any>;
8
+ datetime: SortingFn<any>;
9
+ basic: SortingFn<any>;
10
10
  };
11
11
  export declare type BuiltInSortingFn = keyof typeof sortingFns;
12
- declare function alphanumeric<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): number;
13
- declare function alphanumericCaseSensitive<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): number;
14
- declare function text<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): 1 | -1 | 0;
15
- declare function textCaseSensitive<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): 1 | -1 | 0;
16
- declare function datetime<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): 1 | -1 | 0;
17
- declare function basic<TGenerics extends TableGenerics>(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): 1 | -1 | 0;
18
- export {};
@@ -114,7 +114,7 @@
114
114
  }
115
115
  }
116
116
 
117
- let column = { ...columnDef,
117
+ let column = {
118
118
  id: "" + id,
119
119
  accessorFn,
120
120
  parent: parent,
@@ -878,8 +878,12 @@
878
878
  getIsAllRowsExpanded: () => {
879
879
  const expanded = instance.getState().expanded; // If expanded is true, save some cycles and return true
880
880
 
881
- if (expanded === true) {
882
- return true;
881
+ if (typeof expanded === 'boolean') {
882
+ return expanded === true;
883
+ }
884
+
885
+ if (!Object.keys(expanded).length) {
886
+ return false;
883
887
  } // If any row is not expanded, return false
884
888
 
885
889
 
@@ -2334,21 +2338,54 @@
2334
2338
  }
2335
2339
 
2336
2340
  const reSplitAlphaNumeric = /([0-9]+)/gm;
2337
- const sortingFns = {
2338
- alphanumeric,
2339
- alphanumericCaseSensitive,
2340
- text,
2341
- textCaseSensitive,
2342
- datetime,
2343
- basic
2344
- };
2345
2341
 
2346
- function alphanumeric(rowA, rowB, columnId) {
2342
+ const alphanumeric = (rowA, rowB, columnId) => {
2347
2343
  return compareAlphanumeric(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2348
- }
2344
+ };
2349
2345
 
2350
- function alphanumericCaseSensitive(rowA, rowB, columnId) {
2346
+ const alphanumericCaseSensitive = (rowA, rowB, columnId) => {
2351
2347
  return compareAlphanumeric(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2348
+ }; // The text filter is more basic (less numeric support)
2349
+ // but is much faster
2350
+
2351
+
2352
+ const text = (rowA, rowB, columnId) => {
2353
+ return compareBasic(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2354
+ }; // The text filter is more basic (less numeric support)
2355
+ // but is much faster
2356
+
2357
+
2358
+ const textCaseSensitive = (rowA, rowB, columnId) => {
2359
+ return compareBasic(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2360
+ };
2361
+
2362
+ const datetime = (rowA, rowB, columnId) => {
2363
+ return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2364
+ };
2365
+
2366
+ const basic = (rowA, rowB, columnId) => {
2367
+ return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId));
2368
+ }; // Utils
2369
+
2370
+
2371
+ function compareBasic(a, b) {
2372
+ return a === b ? 0 : a > b ? 1 : -1;
2373
+ }
2374
+
2375
+ function toString(a) {
2376
+ if (typeof a === 'number') {
2377
+ if (isNaN(a) || a === Infinity || a === -Infinity) {
2378
+ return '';
2379
+ }
2380
+
2381
+ return String(a);
2382
+ }
2383
+
2384
+ if (typeof a === 'string') {
2385
+ return a;
2386
+ }
2387
+
2388
+ return '';
2352
2389
  } // Mixed sorting is slow, but very inclusive of many edge cases.
2353
2390
  // It handles numbers, mixed alphanumeric combinations, and even
2354
2391
  // null, undefined, and Infinity
@@ -2395,48 +2432,17 @@
2395
2432
  }
2396
2433
 
2397
2434
  return a.length - b.length;
2398
- } // The text filter is more basic (less numeric support)
2399
- // but is much faster
2400
-
2401
-
2402
- function text(rowA, rowB, columnId) {
2403
- return compareBasic(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
2404
- } // The text filter is more basic (less numeric support)
2405
- // but is much faster
2406
-
2407
-
2408
- function textCaseSensitive(rowA, rowB, columnId) {
2409
- return compareBasic(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
2410
- }
2411
-
2412
- function datetime(rowA, rowB, columnId) {
2413
- return compareBasic(rowA.getValue(columnId).getTime(), rowB.getValue(columnId).getTime());
2414
- }
2415
-
2416
- function basic(rowA, rowB, columnId) {
2417
- return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId));
2418
- } // Utils
2419
-
2420
-
2421
- function compareBasic(a, b) {
2422
- return a === b ? 0 : a > b ? 1 : -1;
2423
- }
2435
+ } // Exports
2424
2436
 
2425
- function toString(a) {
2426
- if (typeof a === 'number') {
2427
- if (isNaN(a) || a === Infinity || a === -Infinity) {
2428
- return '';
2429
- }
2430
-
2431
- return String(a);
2432
- }
2433
2437
 
2434
- if (typeof a === 'string') {
2435
- return a;
2436
- }
2437
-
2438
- return '';
2439
- }
2438
+ const sortingFns = {
2439
+ alphanumeric,
2440
+ alphanumericCaseSensitive,
2441
+ text,
2442
+ textCaseSensitive,
2443
+ datetime,
2444
+ basic
2445
+ };
2440
2446
 
2441
2447
  //
2442
2448
  const Sorting = {
@@ -2517,8 +2523,10 @@
2517
2523
  // })
2518
2524
  // return
2519
2525
  // }
2526
+ // this needs to be outside of instance.setSorting to be in sync with rerender
2527
+ const nextSortingOrder = column.getNextSortingOrder();
2520
2528
  instance.setSorting(old => {
2521
- var _ref2, _column$columnDef$sor, _instance$options$ena, _instance$options$ena2;
2529
+ var _instance$options$ena, _instance$options$ena2;
2522
2530
 
2523
2531
  // Find any existing sorting for this column
2524
2532
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
@@ -2543,30 +2551,29 @@
2543
2551
  } else {
2544
2552
  sortAction = 'replace';
2545
2553
  }
2546
- }
2554
+ } // Handle toggle states that will remove the sorting
2547
2555
 
2548
- 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
2549
2556
 
2550
2557
  if (sortAction === 'toggle' && ( // Must be toggling
2551
2558
  (_instance$options$ena = instance.options.enableSortingRemoval) != null ? _instance$options$ena : true) && // If enableSortRemove, enable in general
2552
2559
  !hasDescDefined && ( // Must not be setting desc
2553
- multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && ( // If multi, don't allow if enableMultiRemove
2554
- existingSorting != null && existingSorting.desc // Finally, detect if it should indeed be removed
2555
- ? !sortDescFirst : sortDescFirst)) {
2560
+ multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && // If multi, don't allow if enableMultiRemove
2561
+ !nextSortingOrder // Finally, detect if it should indeed be removed
2562
+ ) {
2556
2563
  sortAction = 'remove';
2557
2564
  }
2558
2565
 
2559
2566
  if (sortAction === 'replace') {
2560
2567
  newSorting = [{
2561
2568
  id: column.id,
2562
- desc: hasDescDefined ? desc : !!sortDescFirst
2569
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2563
2570
  }];
2564
2571
  } else if (sortAction === 'add' && old != null && old.length) {
2565
2572
  var _instance$options$max;
2566
2573
 
2567
2574
  newSorting = [...old, {
2568
2575
  id: column.id,
2569
- desc: hasDescDefined ? desc : !!sortDescFirst
2576
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2570
2577
  }]; // Take latest n columns
2571
2578
 
2572
2579
  newSorting.splice(0, newSorting.length - ((_instance$options$max = instance.options.maxMultiSortColCount) != null ? _instance$options$max : Number.MAX_SAFE_INTEGER));
@@ -2575,7 +2582,7 @@
2575
2582
  newSorting = old.map(d => {
2576
2583
  if (d.id === column.id) {
2577
2584
  return { ...d,
2578
- desc: hasDescDefined ? desc : !(existingSorting != null && existingSorting.desc)
2585
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2579
2586
  };
2580
2587
  }
2581
2588
 
@@ -2588,6 +2595,23 @@
2588
2595
  return newSorting;
2589
2596
  });
2590
2597
  },
2598
+ getNextSortingOrder: () => {
2599
+ var _ref2, _column$columnDef$sor;
2600
+
2601
+ const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc';
2602
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2603
+ const isSorted = column.getIsSorted();
2604
+
2605
+ if (!isSorted) {
2606
+ return firstSortDirection;
2607
+ }
2608
+
2609
+ if (isSorted === firstSortDirection) {
2610
+ return isSorted === 'desc' ? 'asc' : 'desc';
2611
+ } else {
2612
+ return false;
2613
+ }
2614
+ },
2591
2615
  getCanSort: () => {
2592
2616
  var _column$columnDef$ena, _instance$options$ena3;
2593
2617
 
@@ -2688,8 +2712,8 @@
2688
2712
  },
2689
2713
  createRow: (row, instance) => {
2690
2714
  return {
2691
- _getAllVisibleCells: memo(() => [row.getAllCells().filter(cell => cell.column.getIsVisible()).map(d => d.id).join('_')], _ => {
2692
- return row.getAllCells().filter(cell => cell.column.getIsVisible());
2715
+ _getAllVisibleCells: memo(() => [row.getAllCells(), instance.getState().columnVisibility], cells => {
2716
+ return cells.filter(cell => cell.column.getIsVisible());
2693
2717
  }, {
2694
2718
  key: "development" === 'production' ,
2695
2719
  debug: () => {
@@ -3040,13 +3064,15 @@
3040
3064
  row,
3041
3065
  column,
3042
3066
  getValue: () => row.getValue(columnId),
3043
- renderCell: () => column.columnDef.cell ? instance._render(column.columnDef.cell, {
3044
- instance,
3045
- column,
3046
- row,
3047
- cell: cell,
3048
- getValue: cell.getValue
3049
- }) : null
3067
+ renderCell: () => {
3068
+ return column.columnDef.cell ? instance._render(column.columnDef.cell, {
3069
+ instance,
3070
+ column,
3071
+ row,
3072
+ cell: cell,
3073
+ getValue: cell.getValue
3074
+ }) : null;
3075
+ }
3050
3076
  };
3051
3077
 
3052
3078
  instance._features.forEach(feature => {
@@ -3121,27 +3147,23 @@
3121
3147
  flatRows: [],
3122
3148
  rowsById: {}
3123
3149
  };
3124
- let rows;
3125
- let row;
3126
- let originalRow;
3127
3150
 
3128
3151
  const accessRows = function (originalRows, depth, parent) {
3129
3152
  if (depth === void 0) {
3130
3153
  depth = 0;
3131
3154
  }
3132
3155
 
3133
- rows = [];
3156
+ const rows = [];
3134
3157
 
3135
3158
  for (let i = 0; i < originalRows.length; i++) {
3136
- originalRow = originalRows[i]; // This could be an expensive check at scale, so we should move it somewhere else, but where?
3159
+ // This could be an expensive check at scale, so we should move it somewhere else, but where?
3137
3160
  // if (!id) {
3138
3161
  // if ("development" !== 'production') {
3139
3162
  // throw new Error(`getRowId expected an ID, but got ${id}`)
3140
3163
  // }
3141
3164
  // }
3142
3165
  // Make the row
3143
-
3144
- row = createRow(instance, instance._getRowId(originalRow, i, parent), originalRow, i, depth); // Keep track of every row in a flat array
3166
+ const row = createRow(instance, instance._getRowId(originalRows[i], i, parent), originalRows[i], i, depth); // Keep track of every row in a flat array
3145
3167
 
3146
3168
  rowModel.flatRows.push(row); // Also keep track of every row by its ID
3147
3169
 
@@ -3152,7 +3174,7 @@
3152
3174
  if (instance.options.getSubRows) {
3153
3175
  var _row$originalSubRows;
3154
3176
 
3155
- row.originalSubRows = instance.options.getSubRows(originalRow, i); // Then recursively access them
3177
+ row.originalSubRows = instance.options.getSubRows(originalRows[i], i); // Then recursively access them
3156
3178
 
3157
3179
  if ((_row$originalSubRows = row.originalSubRows) != null && _row$originalSubRows.length) {
3158
3180
  row.subRows = accessRows(row.originalSubRows, depth + 1, row);