Skip to content

Commit

Permalink
feat: provide row.parentRow where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Mar 23, 2023
1 parent 0ab5231 commit 69d8ae4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
8 changes: 8 additions & 0 deletions docs/api/core/row.md
Expand Up @@ -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<TData>
```

If nested, this row's parent row.

### `getValue`

```tsx
Expand Down
5 changes: 4 additions & 1 deletion packages/table-core/src/core/row.ts
Expand Up @@ -17,6 +17,7 @@ export interface CoreRow<TData extends RowData> {
originalSubRows?: TData[]
getAllCells: () => Cell<TData, unknown>[]
_getAllCellsByColumnId: () => Record<string, Cell<TData, unknown>>
parentRow?: Row<TData>
}

export const createRow = <TData extends RowData>(
Expand All @@ -25,13 +26,15 @@ export const createRow = <TData extends RowData>(
original: TData,
rowIndex: number,
depth: number,
subRows?: Row<TData>[]
subRows?: Row<TData>[],
parentRow?: Row<TData>
): Row<TData> => {
let row: CoreRow<TData> = {
id,
index: rowIndex,
original,
depth,
parentRow,
_valuesCache: {},
_uniqueValuesCache: {},
getValue: columnId => {
Expand Down
24 changes: 18 additions & 6 deletions packages/table-core/src/utils/filterRowsUtils.ts
Expand Up @@ -22,7 +22,11 @@ export function filterRowModelFromLeafs<TData extends RowData>(
const newFilteredRowsById: Record<string, Row<TData>> = {}
const maxDepth = table.options.maxLeafRowFilterDepth ?? 100

const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
const recurseFilterRows = (
rowsToFilter: Row<TData>[],
depth = 0,
parentRow?: Row<TData>
) => {
const rows: Row<TData>[] = []

// Filter from children up first
Expand All @@ -34,12 +38,14 @@ export function filterRowModelFromLeafs<TData extends RowData>(
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) {
Expand Down Expand Up @@ -85,7 +91,11 @@ export function filterRowModelFromRoot<TData extends RowData>(
const maxDepth = table.options.maxLeafRowFilterDepth ?? 100

// Filters top level and nested rows
const recurseFilterRows = (rowsToFilter: Row<TData>[], depth = 0) => {
const recurseFilterRows = (
rowsToFilter: Row<TData>[],
depth = 0,
parentRow?: Row<TData>
) => {
// Filter from parents downward first

const rows: Row<TData>[] = []
Expand All @@ -103,9 +113,11 @@ export function filterRowModelFromRoot<TData extends RowData>(
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
}

Expand Down
8 changes: 5 additions & 3 deletions packages/table-core/src/utils/getCoreRowModel.ts
Expand Up @@ -24,7 +24,7 @@ export function getCoreRowModel<TData extends RowData>(): (
const accessRows = (
originalRows: TData[],
depth = 0,
parent?: Row<TData>
parentRow?: Row<TData>
): Row<TData>[] => {
const rows = [] as Row<TData>[]

Expand All @@ -39,10 +39,12 @@ export function getCoreRowModel<TData extends RowData>(): (
// 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
Expand Down
16 changes: 12 additions & 4 deletions packages/table-core/src/utils/getGroupedRowModel.ts
Expand Up @@ -29,6 +29,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
const groupUpRecursively = (
rows: Row<TData>[],
depth = 0,
parentRow?: Row<TData>,
parentId?: string
) => {
// Grouping depth has been been met
Expand All @@ -41,7 +42,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
groupedRowsById[row.id] = row

if (row.subRows) {
row.subRows = groupUpRecursively(row.subRows, depth + 1)
row.subRows = groupUpRecursively(row.subRows, depth + 1, row)
}

return row
Expand All @@ -60,7 +61,12 @@ export function getGroupedRowModel<TData extends RowData>(): (
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
Expand All @@ -72,7 +78,9 @@ export function getGroupedRowModel<TData extends RowData>(): (
id,
leafRows[0]!.original,
index,
depth
depth,
undefined,
parentRow
)

Object.assign(row, {
Expand Down Expand Up @@ -134,7 +142,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
return aggregatedGroupedRows
}

const groupedRows = groupUpRecursively(rowModel.rows, 0, '')
const groupedRows = groupUpRecursively(rowModel.rows, 0, undefined, '')

groupedRows.forEach(subRow => {
groupedFlatRows.push(subRow)
Expand Down

0 comments on commit 69d8ae4

Please sign in to comment.