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

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 (32) hide show
  1. package/dist/core/rows/coreRowsFeature.cjs +4 -1
  2. package/dist/core/rows/coreRowsFeature.cjs.map +1 -1
  3. package/dist/core/rows/coreRowsFeature.js +4 -1
  4. package/dist/core/rows/coreRowsFeature.js.map +1 -1
  5. package/dist/features/row-selection/rowSelectionFeature.utils.cjs +1 -1
  6. package/dist/features/row-selection/rowSelectionFeature.utils.cjs.map +1 -1
  7. package/dist/features/row-selection/rowSelectionFeature.utils.js +2 -2
  8. package/dist/features/row-selection/rowSelectionFeature.utils.js.map +1 -1
  9. package/dist/features/row-sorting/createSortedRowModel.cjs +17 -7
  10. package/dist/features/row-sorting/createSortedRowModel.cjs.map +1 -1
  11. package/dist/features/row-sorting/createSortedRowModel.js +18 -8
  12. package/dist/features/row-sorting/createSortedRowModel.js.map +1 -1
  13. package/dist/features/row-sorting/rowSortingFeature.utils.cjs +1 -1
  14. package/dist/features/row-sorting/rowSortingFeature.utils.cjs.map +1 -1
  15. package/dist/features/row-sorting/rowSortingFeature.utils.js +1 -1
  16. package/dist/features/row-sorting/rowSortingFeature.utils.js.map +1 -1
  17. package/dist/index.cjs +1 -0
  18. package/dist/index.d.cts +2 -2
  19. package/dist/index.d.ts +2 -2
  20. package/dist/index.js +2 -2
  21. package/dist/utils.cjs +14 -0
  22. package/dist/utils.cjs.map +1 -1
  23. package/dist/utils.d.cts +6 -1
  24. package/dist/utils.d.ts +6 -1
  25. package/dist/utils.js +14 -1
  26. package/dist/utils.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/core/rows/coreRowsFeature.ts +1 -0
  29. package/src/features/row-selection/rowSelectionFeature.utils.ts +2 -1
  30. package/src/features/row-sorting/createSortedRowModel.ts +31 -10
  31. package/src/features/row-sorting/rowSortingFeature.utils.ts +2 -10
  32. package/src/utils.ts +21 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/table-core",
3
- "version": "9.0.0-beta.33",
3
+ "version": "9.0.0-beta.34",
4
4
  "description": "Headless UI for building powerful tables & datagrids for TS/JS.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -29,6 +29,7 @@ export const coreRowsFeature: TableFeature = {
29
29
  },
30
30
  row_getLeafRows: {
31
31
  fn: (row) => row_getLeafRows(row),
32
+ memoDeps: (row) => [row.subRows],
32
33
  },
33
34
  row_getParentRow: {
34
35
  fn: (row) => row_getParentRow(row),
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  callMemoOrStaticFn,
3
3
  cloneState,
4
+ copyInstancePropertiesWithoutMemos,
4
5
  hasOwn,
5
6
  makeObjectMap,
6
7
  } from '../../utils'
@@ -738,7 +739,7 @@ export function selectRowsFn<
738
739
  if (isSelected) {
739
740
  // Preserve prototype chain so methods like getValue() remain accessible
740
741
  const cloned = Object.create(Object.getPrototypeOf(row))
741
- Object.assign(cloned, row)
742
+ copyInstancePropertiesWithoutMemos(cloned, row)
742
743
  cloned.subRows = newSubRows
743
744
  result.push(cloned)
744
745
  }
@@ -1,4 +1,4 @@
1
- import { tableMemo } from '../../utils'
1
+ import { copyInstancePropertiesWithoutMemos, tableMemo } from '../../utils'
2
2
  import { table_autoResetPageIndex } from '../row-pagination/rowPaginationFeature.utils'
3
3
  import { column_getCanSort, column_getSortFn } from './rowSortingFeature.utils'
4
4
  import type { Column_Internal } from '../../types/Column'
@@ -56,6 +56,10 @@ function _createSortedRowModel<
56
56
  return column ? column_getCanSort(column) : false
57
57
  })
58
58
 
59
+ if (!availableSorting.length) {
60
+ return preSortedRowModel
61
+ }
62
+
59
63
  const resolvedSorting: Array<{
60
64
  id: string
61
65
  desc?: boolean
@@ -132,32 +136,49 @@ function _createSortedRowModel<
132
136
  return rowA.index - rowB.index
133
137
  }
134
138
 
135
- const sortData = (rows: Array<Row<TFeatures, TData>>) => {
139
+ const sortData = (
140
+ rows: Array<Row<TFeatures, TData>>,
141
+ ): {
142
+ rows: Array<Row<TFeatures, TData>>
143
+ changed: boolean
144
+ } => {
136
145
  const sortedData = rows.slice()
137
146
 
138
147
  sortedData.sort(compareRows)
148
+ let changed = false
139
149
 
140
150
  // If there are sub-rows, sort them. Clone only rows that need mutation
141
151
  // (i.e. have subRows) so we don't corrupt the source row model.
142
152
  for (let i = 0; i < sortedData.length; i++) {
143
153
  const row = sortedData[i]!
154
+ if (row !== rows[i]) {
155
+ changed = true
156
+ }
157
+
144
158
  if (row.subRows.length) {
145
- // Preserve prototype chain so methods like getValue() remain accessible
146
- const cloned = Object.create(Object.getPrototypeOf(row))
147
- Object.assign(cloned, row)
148
- cloned.subRows = sortData(row.subRows)
149
- sortedData[i] = cloned
150
- sortedFlatRows.push(cloned)
159
+ const sortedSubRows = sortData(row.subRows)
160
+
161
+ if (sortedSubRows.changed) {
162
+ // Preserve prototype chain so methods like getValue() remain accessible
163
+ const cloned = Object.create(Object.getPrototypeOf(row))
164
+ copyInstancePropertiesWithoutMemos(cloned, row)
165
+ cloned.subRows = sortedSubRows.rows
166
+ sortedData[i] = cloned
167
+ sortedFlatRows.push(cloned)
168
+ changed = true
169
+ } else {
170
+ sortedFlatRows.push(row)
171
+ }
151
172
  } else {
152
173
  sortedFlatRows.push(row)
153
174
  }
154
175
  }
155
176
 
156
- return sortedData
177
+ return { rows: sortedData, changed }
157
178
  }
158
179
 
159
180
  return {
160
- rows: sortData(preSortedRowModel.rows),
181
+ rows: sortData(preSortedRowModel.rows).rows,
161
182
  flatRows: sortedFlatRows,
162
183
  rowsById: preSortedRowModel.rowsById,
163
184
  }
@@ -195,23 +195,15 @@ export function column_toggleSorting<
195
195
  desc?: boolean,
196
196
  multi?: boolean,
197
197
  ) {
198
- // if (column.columns.length) {
199
- // column.columns.forEach((c, i) => {
200
- // if (c.id) {
201
- // table.toggleColumnSorting(c.id, undefined, multi || !!i)
202
- // }
203
- // })
204
- // return
205
- // }
206
-
207
198
  // this needs to be outside of table.setSorting to be in sync with rerender
208
199
  const nextSortingOrder = column_getNextSortingOrder(column)
209
200
  const hasManualValue = typeof desc !== 'undefined'
210
201
 
211
202
  table_setSorting(column.table, (old) => {
212
203
  // Find any existing sorting for this column
213
- const existingSorting = old.find((d) => d.id === column.id)
214
204
  const existingIndex = old.findIndex((d) => d.id === column.id)
205
+ const existingSorting =
206
+ existingIndex === -1 ? undefined : old[existingIndex]
215
207
 
216
208
  let newSorting: SortingState = []
217
209
 
package/src/utils.ts CHANGED
@@ -50,6 +50,27 @@ export function cloneState<T>(value: T): T {
50
50
  return value
51
51
  }
52
52
 
53
+ /**
54
+ * Copies prototype-instance own properties without carrying over lazy memo
55
+ * closures that were bound to the source instance.
56
+ */
57
+ export function copyInstancePropertiesWithoutMemos<
58
+ TTarget extends Record<string, any>,
59
+ TSource extends Record<string, any>,
60
+ >(target: TTarget, source: TSource): TTarget & TSource {
61
+ const keys = Object.keys(source)
62
+ const targetRecord = target as Record<string, any>
63
+
64
+ for (let i = 0; i < keys.length; i++) {
65
+ const key = keys[i]!
66
+ if (!key.startsWith('_memo_')) {
67
+ targetRecord[key] = source[key]
68
+ }
69
+ }
70
+
71
+ return target as TTarget & TSource
72
+ }
73
+
53
74
  /**
54
75
  * Creates an object intended only for string-keyed dictionary lookups.
55
76
  *