From 69d8ae4e456b647cc700d870f1cd1937962a6c68 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Thu, 23 Mar 2023 16:00:34 -0600 Subject: [PATCH] feat: provide row.parentRow where possible --- docs/api/core/row.md | 8 +++++++ packages/table-core/src/core/row.ts | 5 +++- .../table-core/src/utils/filterRowsUtils.ts | 24 ++++++++++++++----- .../table-core/src/utils/getCoreRowModel.ts | 8 ++++--- .../src/utils/getGroupedRowModel.ts | 16 +++++++++---- 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/docs/api/core/row.md b/docs/api/core/row.md index acd365365e..a0a9b6c261 100644 --- a/docs/api/core/row.md +++ b/docs/api/core/row.md @@ -42,6 +42,14 @@ The original row object provided to the table. > 🧠 If the row is a grouped row, the original row object will be the first original in the group. +### `parentRow` + +```tsx +parentRow?: Row +``` + +If nested, this row's parent row. + ### `getValue` ```tsx diff --git a/packages/table-core/src/core/row.ts b/packages/table-core/src/core/row.ts index 99c7384b65..3883bd983b 100644 --- a/packages/table-core/src/core/row.ts +++ b/packages/table-core/src/core/row.ts @@ -17,6 +17,7 @@ export interface CoreRow { originalSubRows?: TData[] getAllCells: () => Cell[] _getAllCellsByColumnId: () => Record> + parentRow?: Row } export const createRow = ( @@ -25,13 +26,15 @@ export const createRow = ( original: TData, rowIndex: number, depth: number, - subRows?: Row[] + subRows?: Row[], + parentRow?: Row ): Row => { let row: CoreRow = { id, index: rowIndex, original, depth, + parentRow, _valuesCache: {}, _uniqueValuesCache: {}, getValue: columnId => { diff --git a/packages/table-core/src/utils/filterRowsUtils.ts b/packages/table-core/src/utils/filterRowsUtils.ts index 2f0f2c8f93..d99383b5c1 100644 --- a/packages/table-core/src/utils/filterRowsUtils.ts +++ b/packages/table-core/src/utils/filterRowsUtils.ts @@ -22,7 +22,11 @@ export function filterRowModelFromLeafs( const newFilteredRowsById: Record> = {} const maxDepth = table.options.maxLeafRowFilterDepth ?? 100 - const recurseFilterRows = (rowsToFilter: Row[], depth = 0) => { + const recurseFilterRows = ( + rowsToFilter: Row[], + depth = 0, + parentRow?: Row + ) => { const rows: Row[] = [] // Filter from children up first @@ -34,12 +38,14 @@ export function filterRowModelFromLeafs( row.id, row.original, row.index, - row.depth + row.depth, + undefined, + parentRow ) newRow.columnFilters = row.columnFilters if (row.subRows?.length && depth < maxDepth) { - newRow.subRows = recurseFilterRows(row.subRows, depth + 1) + newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow) row = newRow if (filterRow(row) && !newRow.subRows.length) { @@ -85,7 +91,11 @@ export function filterRowModelFromRoot( const maxDepth = table.options.maxLeafRowFilterDepth ?? 100 // Filters top level and nested rows - const recurseFilterRows = (rowsToFilter: Row[], depth = 0) => { + const recurseFilterRows = ( + rowsToFilter: Row[], + depth = 0, + parentRow?: Row + ) => { // Filter from parents downward first const rows: Row[] = [] @@ -103,9 +113,11 @@ export function filterRowModelFromRoot( row.id, row.original, row.index, - row.depth + row.depth, + undefined, + parentRow ) - newRow.subRows = recurseFilterRows(row.subRows, depth + 1) + newRow.subRows = recurseFilterRows(row.subRows, depth + 1, newRow) row = newRow } diff --git a/packages/table-core/src/utils/getCoreRowModel.ts b/packages/table-core/src/utils/getCoreRowModel.ts index 2f6179eb14..f11330e66e 100644 --- a/packages/table-core/src/utils/getCoreRowModel.ts +++ b/packages/table-core/src/utils/getCoreRowModel.ts @@ -24,7 +24,7 @@ export function getCoreRowModel(): ( const accessRows = ( originalRows: TData[], depth = 0, - parent?: Row + parentRow?: Row ): Row[] => { const rows = [] as Row[] @@ -39,10 +39,12 @@ export function getCoreRowModel(): ( // Make the row const row = createRow( table, - table._getRowId(originalRows[i]!, i, parent), + table._getRowId(originalRows[i]!, i, parentRow), originalRows[i]!, i, - depth + depth, + undefined, + parentRow ) // Keep track of every row in a flat array diff --git a/packages/table-core/src/utils/getGroupedRowModel.ts b/packages/table-core/src/utils/getGroupedRowModel.ts index 3a3bc051cc..682194fe22 100644 --- a/packages/table-core/src/utils/getGroupedRowModel.ts +++ b/packages/table-core/src/utils/getGroupedRowModel.ts @@ -29,6 +29,7 @@ export function getGroupedRowModel(): ( const groupUpRecursively = ( rows: Row[], depth = 0, + parentRow?: Row, parentId?: string ) => { // Grouping depth has been been met @@ -41,7 +42,7 @@ export function getGroupedRowModel(): ( groupedRowsById[row.id] = row if (row.subRows) { - row.subRows = groupUpRecursively(row.subRows, depth + 1) + row.subRows = groupUpRecursively(row.subRows, depth + 1, row) } return row @@ -60,7 +61,12 @@ export function getGroupedRowModel(): ( id = parentId ? `${parentId}>${id}` : id // First, Recurse to group sub rows before aggregation - const subRows = groupUpRecursively(groupedRows, depth + 1, id) + const subRows = groupUpRecursively( + groupedRows, + depth + 1, + parentRow, + id + ) // Flatten the leaf rows of the rows in this group const leafRows = depth @@ -72,7 +78,9 @@ export function getGroupedRowModel(): ( id, leafRows[0]!.original, index, - depth + depth, + undefined, + parentRow ) Object.assign(row, { @@ -134,7 +142,7 @@ export function getGroupedRowModel(): ( return aggregatedGroupedRows } - const groupedRows = groupUpRecursively(rowModel.rows, 0, '') + const groupedRows = groupUpRecursively(rowModel.rows, 0, undefined, '') groupedRows.forEach(subRow => { groupedFlatRows.push(subRow)