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
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Comment on lines +30 to +31

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use batchfrom reactivity here I think, or move the batch invocation into onAfterUpdate 🤔

},
})
}
}
Expand Down
26 changes: 25 additions & 1 deletion packages/table-core/tests/implementation/core/autoReset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const features = testFeatures({
sortFns,
})

const expandingOnlyFeatures = testFeatures({
rowExpandingFeature,
})

const columns: Array<ColumnDef<typeof features, Person, any>> = [
{ accessorKey: 'name', id: 'name' },
{ accessorKey: 'age', id: 'age' },
Expand Down Expand Up @@ -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<typeof makeTable>) {
async function primeTable(table: { getRowModel: () => unknown }) {
table.getRowModel()
await flushMicrotasks()
await flushMicrotasks()
Expand Down Expand Up @@ -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<typeof expandingOnlyFeatures, Person>({
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)
Expand Down
Loading