diff --git a/packages/table-core/src/core/row-models/createCoreRowModel.ts b/packages/table-core/src/core/row-models/createCoreRowModel.ts index 1876b066d5..254e6dc390 100644 --- a/packages/table-core/src/core/row-models/createCoreRowModel.ts +++ b/packages/table-core/src/core/row-models/createCoreRowModel.ts @@ -1,5 +1,6 @@ import { constructRow } from '../rows/constructRow' import { makeObjectMap, tableMemo } from '../../utils' +import { table_autoResetExpanded } from '../../features/row-expanding/rowExpandingFeature.utils' import { table_autoResetPageIndex } from '../../features/row-pagination/rowPaginationFeature.utils' import type { Table_Internal } from '../../types/Table' import type { RowModel } from './coreRowModelsFeature.types' @@ -25,7 +26,10 @@ export function createCoreRowModel< fnName: 'table.getCoreRowModel', memoDeps: () => [table.options.data], fn: () => _createCoreRowModel(table, table.options.data), - onAfterUpdate: () => table_autoResetPageIndex(table), + onAfterUpdate: () => { + table_autoResetExpanded(table) + table_autoResetPageIndex(table) + }, }) } } diff --git a/packages/table-core/tests/implementation/core/autoReset.test.ts b/packages/table-core/tests/implementation/core/autoReset.test.ts index 4bd28fe8c4..8eedffeca0 100644 --- a/packages/table-core/tests/implementation/core/autoReset.test.ts +++ b/packages/table-core/tests/implementation/core/autoReset.test.ts @@ -52,6 +52,10 @@ const features = testFeatures({ sortFns, }) +const expandingOnlyFeatures = testFeatures({ + rowExpandingFeature, +}) + const columns: Array> = [ { accessorKey: 'name', id: 'name' }, { accessorKey: 'age', id: 'age' }, @@ -91,7 +95,7 @@ function makeTable( // Pull the row model once and flush so the initial memo runs (which itself // schedules autoReset callbacks) do not interfere with the test assertions. -async function primeTable(table: ReturnType) { +async function primeTable(table: { getRowModel: () => unknown }) { table.getRowModel() await flushMicrotasks() await flushMicrotasks() @@ -241,6 +245,26 @@ describe('autoResetPageIndex end-to-end wiring', () => { }) describe('autoResetExpanded end-to-end wiring', () => { + it('should reset expanded when data changes without the grouping feature', async () => { + const table = constructTable({ + features: expandingOnlyFeatures, + columns: [{ accessorKey: 'name', id: 'name' }], + data: makeData(), + getSubRows: (row) => row.subRows, + }) + await primeTable(table) + + table.getRow('1').toggleExpanded(true) + expect(table.atoms.expanded.get()).toEqual({ '1': true }) + + table.setOptions((old) => ({ ...old, data: makeData() })) + table.getRowModel() + await flushMicrotasks() + await flushMicrotasks() + + expect(table.atoms.expanded.get()).toEqual({}) + }) + it('should reset expanded to an empty map when grouping changes', async () => { const table = makeTable() await primeTable(table)