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