diff --git a/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts b/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts index 916a747e7f..37a6be8162 100644 --- a/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts +++ b/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts @@ -123,8 +123,10 @@ function table_getPinnedRows< if (keepPinnedRows) { // get all rows that are pinned even if they would not be otherwise // visible; account for expanded parent rows, but not pagination/filtering - const fullRow = table.getRow(rowId, true) - if (row_getIsAllParentsExpanded(fullRow)) row = fullRow + const fullRow = + table.getPrePaginatedRowModel().rowsById[rowId] ?? + table.getCoreRowModel().rowsById[rowId] + if (fullRow && row_getIsAllParentsExpanded(fullRow)) row = fullRow } else { // else get only visible rows that are pinned row = visibleRows.find((r) => r.id === rowId) diff --git a/packages/table-core/tests/implementation/features/row-pinning/rowPinningFeature.test.ts b/packages/table-core/tests/implementation/features/row-pinning/rowPinningFeature.test.ts index fd916d184a..1cac828818 100644 --- a/packages/table-core/tests/implementation/features/row-pinning/rowPinningFeature.test.ts +++ b/packages/table-core/tests/implementation/features/row-pinning/rowPinningFeature.test.ts @@ -1,6 +1,8 @@ import { describe, expect, it, vi } from 'vitest' import { + columnGroupingFeature, constructTable, + createGroupedRowModel, createPaginatedRowModel, rowPaginationFeature, rowPinningFeature, @@ -21,6 +23,12 @@ const featuresWithPagination = testFeatures({ paginatedRowModel: createPaginatedRowModel(), }) +const featuresWithGrouping = testFeatures({ + columnGroupingFeature, + rowPinningFeature, + groupedRowModel: createGroupedRowModel(), +}) + function makeTable( options?: Partial< Omit, 'data' | 'columns' | 'features'> @@ -137,6 +145,31 @@ describe('table methods', () => { }) describe('getTopRows/getBottomRows/getCenterRows', () => { + it('does not throw when a pinned grouped row disappears after ungrouping', () => { + const data = generateTestData(3) + const table = constructTable({ + features: featuresWithGrouping, + data, + columns: generateTestColumnDefs(data), + initialState: { + grouping: ['status'], + }, + }) + const groupedRow = table.getRowModel().rows[0]! + + groupedRow.pin('top') + expect(table.getTopRows()).toEqual([groupedRow]) + + table.setGrouping([]) + + expect(table.getTopRows()).toEqual([]) + expect(table.atoms.rowPinning.get().top).toEqual([groupedRow.id]) + + table.setGrouping(['status']) + + expect(table.getTopRows().map((row) => row.id)).toEqual([groupedRow.id]) + }) + it('should return correct rows for each section', () => { const table = makeTable({ initialState: {