@tanstack/table-core 9.0.0-beta.34 → 9.0.0-beta.36

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.
Files changed (59) hide show
  1. package/dist/core/rows/coreRowsFeature.types.d.cts +2 -0
  2. package/dist/core/rows/coreRowsFeature.types.d.ts +2 -0
  3. package/dist/core/rows/coreRowsFeature.utils.cjs +11 -1
  4. package/dist/core/rows/coreRowsFeature.utils.cjs.map +1 -1
  5. package/dist/core/rows/coreRowsFeature.utils.js +11 -1
  6. package/dist/core/rows/coreRowsFeature.utils.js.map +1 -1
  7. package/dist/core/table/coreTablesFeature.types.d.cts +2 -2
  8. package/dist/core/table/coreTablesFeature.types.d.ts +2 -2
  9. package/dist/features/column-ordering/columnOrderingFeature.cjs +12 -11
  10. package/dist/features/column-ordering/columnOrderingFeature.cjs.map +1 -1
  11. package/dist/features/column-ordering/columnOrderingFeature.js +13 -12
  12. package/dist/features/column-ordering/columnOrderingFeature.js.map +1 -1
  13. package/dist/features/column-ordering/columnOrderingFeature.types.d.cts +26 -1
  14. package/dist/features/column-ordering/columnOrderingFeature.types.d.ts +26 -1
  15. package/dist/features/column-ordering/columnOrderingFeature.utils.cjs +26 -1
  16. package/dist/features/column-ordering/columnOrderingFeature.utils.cjs.map +1 -1
  17. package/dist/features/column-ordering/columnOrderingFeature.utils.d.cts +14 -2
  18. package/dist/features/column-ordering/columnOrderingFeature.utils.d.ts +14 -2
  19. package/dist/features/column-ordering/columnOrderingFeature.utils.js +27 -3
  20. package/dist/features/column-ordering/columnOrderingFeature.utils.js.map +1 -1
  21. package/dist/features/column-pinning/columnPinningFeature.utils.cjs +10 -6
  22. package/dist/features/column-pinning/columnPinningFeature.utils.cjs.map +1 -1
  23. package/dist/features/column-pinning/columnPinningFeature.utils.js +10 -6
  24. package/dist/features/column-pinning/columnPinningFeature.utils.js.map +1 -1
  25. package/dist/features/column-resizing/columnResizingFeature.utils.cjs +11 -2
  26. package/dist/features/column-resizing/columnResizingFeature.utils.cjs.map +1 -1
  27. package/dist/features/column-resizing/columnResizingFeature.utils.js +11 -2
  28. package/dist/features/column-resizing/columnResizingFeature.utils.js.map +1 -1
  29. package/dist/index.d.cts +2 -2
  30. package/dist/index.d.ts +2 -2
  31. package/dist/static-functions.cjs +1 -0
  32. package/dist/static-functions.d.cts +2 -2
  33. package/dist/static-functions.d.ts +2 -2
  34. package/dist/static-functions.js +2 -2
  35. package/dist/types/RowModel.d.cts +2 -4
  36. package/dist/types/RowModel.d.ts +2 -4
  37. package/dist/utils.cjs +3 -2
  38. package/dist/utils.cjs.map +1 -1
  39. package/dist/utils.d.cts +2 -1
  40. package/dist/utils.d.ts +2 -1
  41. package/dist/utils.js +3 -2
  42. package/dist/utils.js.map +1 -1
  43. package/dist/worker/initTableWorker.cjs.map +1 -1
  44. package/dist/worker/initTableWorker.js.map +1 -1
  45. package/dist/worker/rebuildRowModel.cjs.map +1 -1
  46. package/dist/worker/rebuildRowModel.js.map +1 -1
  47. package/package.json +2 -1
  48. package/src/core/rows/coreRowsFeature.types.ts +5 -0
  49. package/src/core/rows/coreRowsFeature.utils.ts +13 -1
  50. package/src/core/table/coreTablesFeature.types.ts +2 -2
  51. package/src/features/column-ordering/columnOrderingFeature.ts +12 -8
  52. package/src/features/column-ordering/columnOrderingFeature.types.ts +26 -0
  53. package/src/features/column-ordering/columnOrderingFeature.utils.ts +56 -5
  54. package/src/features/column-pinning/columnPinningFeature.utils.ts +24 -10
  55. package/src/features/column-resizing/columnResizingFeature.utils.ts +22 -5
  56. package/src/types/RowModel.ts +6 -10
  57. package/src/utils.ts +3 -2
  58. package/src/worker/initTableWorker.ts +1 -1
  59. package/src/worker/rebuildRowModel.ts +8 -5
@@ -1,12 +1,15 @@
1
1
  import { table_getPinnedVisibleLeafColumns } from '../column-pinning/columnPinningFeature.utils'
2
- import { cloneState } from '../../utils'
2
+ import { callMemoOrStaticFn, cloneState, makeObjectMap } from '../../utils'
3
3
  import type { GroupingState } from '../column-grouping/columnGroupingFeature.types'
4
4
  import type { CellData, RowData, Updater } from '../../types/type-utils'
5
5
  import type { TableFeatures } from '../../types/TableFeatures'
6
6
  import type { Table_Internal } from '../../types/Table'
7
- import type { Column_Internal } from '../../types/Column'
7
+ import type { Column, Column_Internal } from '../../types/Column'
8
8
  import type { ColumnPinningPosition } from '../column-pinning/columnPinningFeature.types'
9
- import type { ColumnOrderState } from './columnOrderingFeature.types'
9
+ import type {
10
+ ColumnIndexes,
11
+ ColumnOrderState,
12
+ } from './columnOrderingFeature.types'
10
13
 
11
14
  /**
12
15
  * Creates the default column order state.
@@ -23,6 +26,42 @@ export function getDefaultColumnOrderState(): ColumnOrderState {
23
26
  return []
24
27
  }
25
28
 
29
+ /**
30
+ * Builds column-id to index records for each visible pinning region.
31
+ *
32
+ * All four regions are built in one pass so a single memo entry serves every
33
+ * `column_getIndex` lookup without per-column scans.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const indexes = table_getColumnIndexes(table)
38
+ * ```
39
+ */
40
+ export function table_getColumnIndexes<
41
+ TFeatures extends TableFeatures,
42
+ TData extends RowData,
43
+ >(table: Table_Internal<TFeatures, TData>): ColumnIndexes {
44
+ const buildIndexes = (
45
+ columns: ReadonlyArray<
46
+ | Column<TFeatures, TData, unknown>
47
+ | Column_Internal<TFeatures, TData, unknown>
48
+ >,
49
+ ): Record<string, number> => {
50
+ const indexes = makeObjectMap<number>()
51
+ for (let i = 0; i < columns.length; i++) {
52
+ indexes[columns[i]!.id] = i
53
+ }
54
+ return indexes
55
+ }
56
+
57
+ return {
58
+ all: buildIndexes(table_getPinnedVisibleLeafColumns(table)),
59
+ center: buildIndexes(table_getPinnedVisibleLeafColumns(table, 'center')),
60
+ left: buildIndexes(table_getPinnedVisibleLeafColumns(table, 'left')),
61
+ right: buildIndexes(table_getPinnedVisibleLeafColumns(table, 'right')),
62
+ }
63
+ }
64
+
26
65
  /**
27
66
  * Finds this column's index within a visible pinning region.
28
67
  *
@@ -42,8 +81,20 @@ export function column_getIndex<
42
81
  column: Column_Internal<TFeatures, TData, TValue>,
43
82
  position?: ColumnPinningPosition | 'center',
44
83
  ) {
45
- const columns = table_getPinnedVisibleLeafColumns(column.table, position)
46
- return columns.findIndex((d) => d.id === column.id)
84
+ const indexes = callMemoOrStaticFn(
85
+ column.table,
86
+ 'getColumnIndexes',
87
+ table_getColumnIndexes,
88
+ )
89
+ const key =
90
+ position === 'left'
91
+ ? 'left'
92
+ : position === 'right'
93
+ ? 'right'
94
+ : position === 'center'
95
+ ? 'center'
96
+ : 'all'
97
+ return indexes[key][column.id] ?? -1
47
98
  }
48
99
 
49
100
  /**
@@ -139,15 +139,22 @@ export function column_getIsPinned<
139
139
  >(
140
140
  column: Column_Internal<TFeatures, TData, TValue>,
141
141
  ): ColumnPinningPosition | false {
142
- const leafColumnIds = column.getLeafColumns().map((d) => d.id)
142
+ const leafColumns = column.getLeafColumns()
143
143
 
144
144
  const { left, right } =
145
145
  column.table.atoms.columnPinning?.get() ?? getDefaultColumnPinningState()
146
146
 
147
- const isLeft = leafColumnIds.some((d) => left.includes(d))
148
- const isRight = leafColumnIds.some((d) => right.includes(d))
149
-
150
- return isLeft ? 'left' : isRight ? 'right' : false
147
+ for (let i = 0; i < leafColumns.length; i++) {
148
+ if (left.includes(leafColumns[i]!.id)) {
149
+ return 'left'
150
+ }
151
+ }
152
+ for (let i = 0; i < leafColumns.length; i++) {
153
+ if (right.includes(leafColumns[i]!.id)) {
154
+ return 'right'
155
+ }
156
+ }
157
+ return false
151
158
  }
152
159
 
153
160
  /**
@@ -197,6 +204,9 @@ export function row_getCenterVisibleCells<
197
204
  )
198
205
  const { left, right } =
199
206
  row.table.atoms.columnPinning?.get() ?? getDefaultColumnPinningState()
207
+ if (!left.length && !right.length) {
208
+ return allCells
209
+ }
200
210
  const leftAndRight: Array<string> = [...left, ...right]
201
211
  return allCells.filter((d) => !leftAndRight.includes(d.column.id))
202
212
  }
@@ -441,11 +451,12 @@ export function table_getCenterHeaderGroups<
441
451
  )
442
452
  const { left, right } =
443
453
  table.atoms.columnPinning?.get() ?? getDefaultColumnPinningState()
444
- const leftAndRight: Array<string> = [...left, ...right]
445
-
446
- leafColumns = leafColumns.filter(
447
- (column) => !leftAndRight.includes(column.id),
448
- )
454
+ if (left.length || right.length) {
455
+ const leftAndRight: Array<string> = [...left, ...right]
456
+ leafColumns = leafColumns.filter(
457
+ (column) => !leftAndRight.includes(column.id),
458
+ )
459
+ }
449
460
  return buildHeaderGroups(allColumns, leafColumns, table, 'center')
450
461
  }
451
462
 
@@ -741,6 +752,9 @@ export function table_getCenterLeafColumns<
741
752
  >(table: Table_Internal<TFeatures, TData>) {
742
753
  const { left, right } =
743
754
  table.atoms.columnPinning?.get() ?? getDefaultColumnPinningState()
755
+ if (!left.length && !right.length) {
756
+ return table.getAllLeafColumns()
757
+ }
744
758
  const leftAndRight: Array<string> = [...left, ...right]
745
759
  return table.getAllLeafColumns().filter((d) => !leftAndRight.includes(d.id))
746
760
  }
@@ -263,17 +263,29 @@ export function header_getResizeHandler<
263
263
  return false
264
264
  },
265
265
  upHandler: (e: TouchEvent) => {
266
- contextDocument?.removeEventListener(
267
- 'touchmove',
268
- touchEvents.moveHandler,
269
- )
270
- contextDocument?.removeEventListener('touchend', touchEvents.upHandler)
266
+ removeTouchEvents()
271
267
  if (e.cancelable) {
272
268
  e.preventDefault()
273
269
  e.stopPropagation()
274
270
  }
275
271
  onEnd(e.touches[0]?.clientX)
276
272
  },
273
+ // the browser fires touchcancel instead of touchend when it takes over
274
+ // the gesture (system gesture, scroll takeover, tab switch); without
275
+ // this the non-passive touchmove listener stays on the document forever
276
+ cancelHandler: () => {
277
+ removeTouchEvents()
278
+ onEnd()
279
+ },
280
+ }
281
+
282
+ const removeTouchEvents = () => {
283
+ contextDocument?.removeEventListener('touchmove', touchEvents.moveHandler)
284
+ contextDocument?.removeEventListener('touchend', touchEvents.upHandler)
285
+ contextDocument?.removeEventListener(
286
+ 'touchcancel',
287
+ touchEvents.cancelHandler,
288
+ )
277
289
  }
278
290
 
279
291
  const passiveIfSupported = passiveEventSupported()
@@ -291,6 +303,11 @@ export function header_getResizeHandler<
291
303
  touchEvents.upHandler,
292
304
  passiveIfSupported,
293
305
  )
306
+ contextDocument?.addEventListener(
307
+ 'touchcancel',
308
+ touchEvents.cancelHandler,
309
+ passiveIfSupported,
310
+ )
294
311
  } else {
295
312
  contextDocument?.addEventListener(
296
313
  'mousemove',
@@ -1,10 +1,7 @@
1
1
  import type { CachedRowModel_Faceted } from '../features/column-faceting/columnFacetingFeature.types'
2
2
  import type { CachedRowModel_Grouped } from '../features/column-grouping/columnGroupingFeature.types'
3
3
  import type { CachedRowModel_Filtered } from '../features/column-filtering/columnFilteringFeature.types'
4
- import type {
5
- CachedRowModel_Core,
6
- RowModel,
7
- } from '../core/row-models/coreRowModelsFeature.types'
4
+ import type { CachedRowModel_Core } from '../core/row-models/coreRowModelsFeature.types'
8
5
  import type { CachedRowModel_Expanded } from '../features/row-expanding/rowExpandingFeature.types'
9
6
  import type { CachedRowModel_Paginated } from '../features/row-pagination/rowPaginationFeature.types'
10
7
  import type { CachedRowModel_Sorted } from '../features/row-sorting/rowSortingFeature.types'
@@ -26,12 +23,11 @@ export interface CachedRowModels_FeatureMap<
26
23
  export type CachedRowModels<
27
24
  TFeatures extends TableFeatures,
28
25
  TData extends RowData,
29
- > = {
30
- CachedRowModel_Core: () => RowModel<TFeatures, TData>
31
- } & ExtractFeatureMapTypes<
32
- TFeatures,
33
- CachedRowModels_FeatureMap<TFeatures, TData>
34
- >
26
+ > = Partial<CachedRowModel_Core<TFeatures, TData>> &
27
+ ExtractFeatureMapTypes<
28
+ TFeatures,
29
+ CachedRowModels_FeatureMap<TFeatures, TData>
30
+ >
35
31
 
36
32
  export interface CachedRowModel_All<
37
33
  in out TFeatures extends TableFeatures,
package/src/utils.ts CHANGED
@@ -52,7 +52,8 @@ export function cloneState<T>(value: T): T {
52
52
 
53
53
  /**
54
54
  * Copies prototype-instance own properties without carrying over lazy memo
55
- * closures that were bound to the source instance.
55
+ * closures or the per-row cell cache, both of which are bound to the source
56
+ * instance (cached cells reference the source row).
56
57
  */
57
58
  export function copyInstancePropertiesWithoutMemos<
58
59
  TTarget extends Record<string, any>,
@@ -63,7 +64,7 @@ export function copyInstancePropertiesWithoutMemos<
63
64
 
64
65
  for (let i = 0; i < keys.length; i++) {
65
66
  const key = keys[i]!
66
- if (!key.startsWith('_memo_')) {
67
+ if (!key.startsWith('_memo_') && key !== '_cellsCache') {
67
68
  targetRecord[key] = source[key]
68
69
  }
69
70
  }
@@ -79,7 +79,7 @@ export function initTableWorker<
79
79
  ...config.features,
80
80
  },
81
81
  data: message.data,
82
- }) as unknown as Table_Internal<TFeatures, TData>
82
+ })
83
83
  // Only columns with an explicit aggregation get eagerly aggregated
84
84
  // per group. Sync tables aggregate lazily (visible cells only), so
85
85
  // auto-aggregating every column would explode on high-cardinality
@@ -2,6 +2,8 @@ import { constructRow } from '../core/rows/constructRow'
2
2
  import { hasOwn } from '../utils'
3
3
  import type { RowModel } from '../core/row-models/coreRowModelsFeature.types'
4
4
  import type { Table_Internal } from '../types/Table'
5
+ import type { TableFeatures } from '../types/TableFeatures'
6
+ import type { RowData } from '../types/type-utils'
5
7
  import type {
6
8
  TableWorkerRowNode,
7
9
  TableWorkerStagePayload,
@@ -13,8 +15,6 @@ export type TableWorkerDataPayload = Exclude<
13
15
  { kind: 'unchanged' }
14
16
  >
15
17
 
16
- type AnyTable = Table_Internal<any, any>
17
-
18
18
  // Main-thread side: payload + this table's core rows -> RowModel. Mirrors how
19
19
  // the sync row models treat rows: data rows are reused (with depth/parentId
20
20
  // rewritten, exactly like createGroupedRowModel does), synthetic group rows
@@ -32,8 +32,11 @@ function collectLeafRows(subRows: Array<any>, out: Array<any>) {
32
32
  }
33
33
  }
34
34
 
35
- export function rebuildRowModel(
36
- table: AnyTable,
35
+ export function rebuildRowModel<
36
+ TFeatures extends TableFeatures,
37
+ TData extends RowData,
38
+ >(
39
+ table: Table_Internal<TFeatures, TData>,
37
40
  payload: TableWorkerDataPayload,
38
41
  /**
39
42
  * Whether a flat payload should rewrite row depth/parentId. Mirrors core:
@@ -42,7 +45,7 @@ export function rebuildRowModel(
42
45
  * depths a grouped/sorted tree rebuild just assigned to shared row objects.
43
46
  */
44
47
  resetDepths: boolean,
45
- ): RowModel<any, any> {
48
+ ): RowModel<TFeatures, TData> {
46
49
  const core = table.getCoreRowModel()
47
50
 
48
51
  if (payload.kind === 'flat') {