Skip to content

Commit 69d8ae4

Browse files
committed
feat: provide row.parentRow where possible
1 parent 0ab5231 commit 69d8ae4

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

docs/api/core/row.md

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ The original row object provided to the table.
4242

4343
> 🧠 If the row is a grouped row, the original row object will be the first original in the group.
4444
45+
### `parentRow`
46+
47+
```tsx
48+
parentRow?: Row<TData>
49+
```
50+
51+
If nested, this row's parent row.
52+
4553
### `getValue`
4654

4755
```tsx

packages/table-core/src/core/row.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface CoreRow<TData extends RowData> {
1717
originalSubRows?: TData[]
1818
getAllCells: () => Cell<TData, unknown>[]
1919
_getAllCellsByColumnId: () => Record<string, Cell<TData, unknown>>
20+
parentRow?: Row<TData>
2021
}
2122

2223
export const createRow = <TData extends RowData>(
@@ -25,13 +26,15 @@ export const createRow = <TData extends RowData>(
2526
original: TData,
2627
rowIndex: number,
2728
depth: number,
28-
subRows?: Row<TData>[]
29+
subRows?: Row<TData>[],
30+
parentRow?: Row<TData>
2931
): Row<TData> => {
3032
let row: CoreRow<TData> = {
3133
id,
3234
index: rowIndex,
3335
original,
3436
depth,
37+
parentRow,
3538
_valuesCache: {},
3639
_uniqueValuesCache: {},
3740
getValue: columnId => {

packages/table-core/src/utils/filterRowsUtils.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export function filterRowModelFromLeafs<TData extends RowData>(
2222
const newFilteredRowsById: Record<string, Row<TData>> = {}
2323
const maxDepth = table.options.maxLeafRowFilterDepth ?? 100
2424

25-
const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
25+
const recurseFilterRows = (
26+
rowsToFilter: Row<TData>[],
27+
depth = 0,
28+
parentRow?: Row<TData>
29+
) => {
2630
const rows: Row<TData>[] = []
2731

2832
// Filter from children up first
@@ -34,12 +38,14 @@ export function filterRowModelFromLeafs<TData extends RowData>(
3438
row.id,
3539
row.original,
3640
row.index,
37-
row.depth
41+
row.depth,
42+
undefined,
43+
parentRow
3844
)
3945
newRow.columnFilters = row.columnFilters
4046

4147
if (row.subRows?.length && depth < maxDepth) {
42-
newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
48+
newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow)
4349
row = newRow
4450

4551
if (filterRow(row) && !newRow.subRows.length) {
@@ -85,7 +91,11 @@ export function filterRowModelFromRoot<TData extends RowData>(
8591
const maxDepth = table.options.maxLeafRowFilterDepth ?? 100
8692

8793
// Filters top level and nested rows
88-
const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
94+
const recurseFilterRows = (
95+
rowsToFilter: Row<TData>[],
96+
depth = 0,
97+
parentRow?: Row<TData>
98+
) => {
8999
// Filter from parents downward first
90100

91101
const rows: Row<TData>[] = []
@@ -103,9 +113,11 @@ export function filterRowModelFromRoot<TData extends RowData>(
103113
row.id,
104114
row.original,
105115
row.index,
106-
row.depth
116+
row.depth,
117+
undefined,
118+
parentRow
107119
)
108-
newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
120+
newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow)
109121
row = newRow
110122
}
111123

packages/table-core/src/utils/getCoreRowModel.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getCoreRowModel<TData extends RowData>(): (
2424
const accessRows = (
2525
originalRows: TData[],
2626
depth = 0,
27-
parent?: Row<TData>
27+
parentRow?: Row<TData>
2828
): Row<TData>[] => {
2929
const rows = [] as Row<TData>[]
3030

@@ -39,10 +39,12 @@ export function getCoreRowModel<TData extends RowData>(): (
3939
// Make the row
4040
const row = createRow(
4141
table,
42-
table._getRowId(originalRows[i]!, i, parent),
42+
table._getRowId(originalRows[i]!, i, parentRow),
4343
originalRows[i]!,
4444
i,
45-
depth
45+
depth,
46+
undefined,
47+
parentRow
4648
)
4749

4850
// Keep track of every row in a flat array

packages/table-core/src/utils/getGroupedRowModel.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
2929
const groupUpRecursively = (
3030
rows: Row<TData>[],
3131
depth = 0,
32+
parentRow?: Row<TData>,
3233
parentId?: string
3334
) => {
3435
// Grouping depth has been been met
@@ -41,7 +42,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
4142
groupedRowsById[row.id] = row
4243

4344
if (row.subRows) {
44-
row.subRows = groupUpRecursively(row.subRows, depth + 1)
45+
row.subRows = groupUpRecursively(row.subRows, depth + 1, row)
4546
}
4647

4748
return row
@@ -60,7 +61,12 @@ export function getGroupedRowModel<TData extends RowData>(): (
6061
id = parentId ? `${parentId}>${id}` : id
6162

6263
// First, Recurse to group sub rows before aggregation
63-
const subRows = groupUpRecursively(groupedRows, depth + 1, id)
64+
const subRows = groupUpRecursively(
65+
groupedRows,
66+
depth + 1,
67+
parentRow,
68+
id
69+
)
6470

6571
// Flatten the leaf rows of the rows in this group
6672
const leafRows = depth
@@ -72,7 +78,9 @@ export function getGroupedRowModel<TData extends RowData>(): (
7278
id,
7379
leafRows[0]!.original,
7480
index,
75-
depth
81+
depth,
82+
undefined,
83+
parentRow
7684
)
7785

7886
Object.assign(row, {
@@ -134,7 +142,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
134142
return aggregatedGroupedRows
135143
}
136144

137-
const groupedRows = groupUpRecursively(rowModel.rows, 0, '')
145+
const groupedRows = groupUpRecursively(rowModel.rows, 0, undefined, '')
138146

139147
groupedRows.forEach(subRow => {
140148
groupedFlatRows.push(subRow)

0 commit comments

Comments
 (0)