Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/table-core/src/core/rows/coreRowsFeature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export function table_getMaxSubRowDepth<
/**
* Looks up this row's direct parent, if it has one.
*
* Parent lookup searches the pre-pagination row model so parent relationships
* are available even when the parent is not on the current page.
* Parent lookup prefers the core row model for structural parents, then falls
* back to the pre-pagination row model for generated parent rows.
*
* @example
* ```ts
Expand All @@ -200,7 +200,14 @@ export function row_getParentRow<
TFeatures extends TableFeatures,
TData extends RowData,
>(row: Row<TFeatures, TData>) {
return row.parentId ? row.table.getRow(row.parentId, true) : undefined
if (!row.parentId) {
return undefined
}

return (
row.table.getCoreRowModel().rowsById[row.parentId] ??
row.table.getRow(row.parentId, true)
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,55 @@ const rowNames = (rows: Array<{ original: { name: string } }>) =>
rows.map((row) => row.original.name)

describe('createFilteredRowModel', () => {
it('allows a filter function to inspect structural parent rows', () => {
const parentAwareFilter: FilterFn<typeof features, NestedRow> = (
row,
columnId,
filterValue,
) => {
const parent = row.getParentRow()
return [row, parent]
.filter((candidate) => candidate !== undefined)
.some((candidate) =>
String(candidate.getValue(columnId))
.toLowerCase()
.includes(String(filterValue).toLowerCase()),
)
}
const table = constructTable<typeof features, NestedRow>({
features,
columns: [
{
accessorKey: 'name',
id: 'name',
filterFn: parentAwareFilter,
},
],
data: [
{ name: 'parent', subRows: [{ name: 'child' }] },
{ name: 'other', subRows: [{ name: 'nested' }] },
],
getSubRows: (row) => row.subRows,
initialState: {
columnFilters: [{ id: 'name', value: 'parent' }],
},
})

expect(() => table.getFilteredRowModel()).not.toThrow()
expect(rowNames(table.getFilteredRowModel().flatRows).sort()).toEqual([
'child',
'parent',
])

table.setColumnFilters([{ id: 'name', value: 'other' }])

expect(() => table.getFilteredRowModel()).not.toThrow()
expect(rowNames(table.getFilteredRowModel().flatRows).sort()).toEqual([
'nested',
'other',
])
})

it('should assign display indexes in filtered row order', () => {
const table = constructTable<typeof features, TestRow>({
features,
Expand Down
Loading