@tanstack/table-core 8.8.0 → 8.8.1

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/table-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "8.8.0",
4
+ "version": "8.8.1",
5
5
  "description": "Headless UI for building powerful tables & datagrids for TS/JS.",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/tanstack/table#readme",
package/src/core/row.ts CHANGED
@@ -7,6 +7,7 @@ export interface CoreRow<TData extends RowData> {
7
7
  index: number
8
8
  original: TData
9
9
  depth: number
10
+ parentId?: string
10
11
  _valuesCache: Record<string, unknown>
11
12
  _uniqueValuesCache: Record<string, unknown>
12
13
  getValue: <TValue>(columnId: string) => TValue
@@ -17,7 +18,8 @@ export interface CoreRow<TData extends RowData> {
17
18
  originalSubRows?: TData[]
18
19
  getAllCells: () => Cell<TData, unknown>[]
19
20
  _getAllCellsByColumnId: () => Record<string, Cell<TData, unknown>>
20
- parentRow?: Row<TData>
21
+ getParentRow: () => Row<TData> | undefined
22
+ getParentRows: () => Row<TData>[]
21
23
  }
22
24
 
23
25
  export const createRow = <TData extends RowData>(
@@ -27,14 +29,14 @@ export const createRow = <TData extends RowData>(
27
29
  rowIndex: number,
28
30
  depth: number,
29
31
  subRows?: Row<TData>[],
30
- parentRow?: Row<TData>
32
+ parentId?: string
31
33
  ): Row<TData> => {
32
34
  let row: CoreRow<TData> = {
33
35
  id,
34
36
  index: rowIndex,
35
37
  original,
36
38
  depth,
37
- parentRow,
39
+ parentId,
38
40
  _valuesCache: {},
39
41
  _uniqueValuesCache: {},
40
42
  getValue: columnId => {
@@ -82,6 +84,18 @@ export const createRow = <TData extends RowData>(
82
84
  row.getValue(columnId) ?? table.options.renderFallbackValue,
83
85
  subRows: subRows ?? [],
84
86
  getLeafRows: () => flattenBy(row.subRows, d => d.subRows),
87
+ getParentRow: () => (row.parentId ? table.getRow(row.parentId) : undefined),
88
+ getParentRows: () => {
89
+ let parentRows: Row<TData>[] = []
90
+ let currentRow = row
91
+ while (true) {
92
+ const parentRow = currentRow.getParentRow()
93
+ if (!parentRow) break
94
+ parentRows.push(parentRow)
95
+ currentRow = parentRow
96
+ }
97
+ return parentRows.reverse()
98
+ },
85
99
  getAllCells: memo(
86
100
  () => [table.getAllLeafColumns()],
87
101
  leafColumns => {
@@ -22,11 +22,7 @@ export function filterRowModelFromLeafs<TData extends RowData>(
22
22
  const newFilteredRowsById: Record<string, Row<TData>> = {}
23
23
  const maxDepth = table.options.maxLeafRowFilterDepth ?? 100
24
24
 
25
- const recurseFilterRows = (
26
- rowsToFilter: Row<TData>[],
27
- depth = 0,
28
- parentRow?: Row<TData>
29
- ) => {
25
+ const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
30
26
  const rows: Row<TData>[] = []
31
27
 
32
28
  // Filter from children up first
@@ -40,12 +36,12 @@ export function filterRowModelFromLeafs<TData extends RowData>(
40
36
  row.index,
41
37
  row.depth,
42
38
  undefined,
43
- parentRow
39
+ row.parentId
44
40
  )
45
41
  newRow.columnFilters = row.columnFilters
46
42
 
47
43
  if (row.subRows?.length && depth < maxDepth) {
48
- newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow)
44
+ newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
49
45
  row = newRow
50
46
 
51
47
  if (filterRow(row) && !newRow.subRows.length) {
@@ -91,11 +87,7 @@ export function filterRowModelFromRoot<TData extends RowData>(
91
87
  const maxDepth = table.options.maxLeafRowFilterDepth ?? 100
92
88
 
93
89
  // Filters top level and nested rows
94
- const recurseFilterRows = (
95
- rowsToFilter: Row<TData>[],
96
- depth = 0,
97
- parentRow?: Row<TData>
98
- ) => {
90
+ const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
99
91
  // Filter from parents downward first
100
92
 
101
93
  const rows: Row<TData>[] = []
@@ -115,9 +107,9 @@ export function filterRowModelFromRoot<TData extends RowData>(
115
107
  row.index,
116
108
  row.depth,
117
109
  undefined,
118
- parentRow
110
+ row.parentId
119
111
  )
120
- newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow)
112
+ newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
121
113
  row = newRow
122
114
  }
123
115
 
@@ -44,7 +44,7 @@ export function getCoreRowModel<TData extends RowData>(): (
44
44
  i,
45
45
  depth,
46
46
  undefined,
47
- parentRow
47
+ parentRow?.id
48
48
  )
49
49
 
50
50
  // Keep track of every row in a flat array
@@ -29,7 +29,6 @@ export function getGroupedRowModel<TData extends RowData>(): (
29
29
  const groupUpRecursively = (
30
30
  rows: Row<TData>[],
31
31
  depth = 0,
32
- parentRow?: Row<TData>,
33
32
  parentId?: string
34
33
  ) => {
35
34
  // Grouping depth has been been met
@@ -42,7 +41,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
42
41
  groupedRowsById[row.id] = row
43
42
 
44
43
  if (row.subRows) {
45
- row.subRows = groupUpRecursively(row.subRows, depth + 1, row)
44
+ row.subRows = groupUpRecursively(row.subRows, depth + 1, row.id)
46
45
  }
47
46
 
48
47
  return row
@@ -61,12 +60,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
61
60
  id = parentId ? `${parentId}>${id}` : id
62
61
 
63
62
  // First, Recurse to group sub rows before aggregation
64
- const subRows = groupUpRecursively(
65
- groupedRows,
66
- depth + 1,
67
- parentRow,
68
- id
69
- )
63
+ const subRows = groupUpRecursively(groupedRows, depth + 1, id)
70
64
 
71
65
  // Flatten the leaf rows of the rows in this group
72
66
  const leafRows = depth
@@ -80,7 +74,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
80
74
  index,
81
75
  depth,
82
76
  undefined,
83
- parentRow
77
+ parentId
84
78
  )
85
79
 
86
80
  Object.assign(row, {
@@ -142,7 +136,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
142
136
  return aggregatedGroupedRows
143
137
  }
144
138
 
145
- const groupedRows = groupUpRecursively(rowModel.rows, 0, undefined, '')
139
+ const groupedRows = groupUpRecursively(rowModel.rows, 0)
146
140
 
147
141
  groupedRows.forEach(subRow => {
148
142
  groupedFlatRows.push(subRow)