diff --git a/.changeset/every-parks-raise.md b/.changeset/every-parks-raise.md new file mode 100644 index 0000000000..e53695ec4f --- /dev/null +++ b/.changeset/every-parks-raise.md @@ -0,0 +1,5 @@ +--- +'@tanstack/table-core': patch +--- + +Prevents non-expandable rows from being added to the state when the old state was true diff --git a/packages/table-core/src/features/RowExpanding.ts b/packages/table-core/src/features/RowExpanding.ts index 456108d4d4..2b1860e32c 100644 --- a/packages/table-core/src/features/RowExpanding.ts +++ b/packages/table-core/src/features/RowExpanding.ts @@ -217,9 +217,7 @@ export const RowExpanding: TableFeature = { } } table.resetExpanded = (defaultState) => { - table.setExpanded( - defaultState ? {} : (table.initialState?.expanded ?? {}), - ) + table.setExpanded(defaultState ? {} : table.initialState?.expanded ?? {}) } table.getCanSomeRowsExpand = () => { return table @@ -296,8 +294,11 @@ export const RowExpanding: TableFeature = { let oldExpanded: ExpandedStateList = {} if (old === true) { - Object.keys(table.getRowModel().rowsById).forEach((rowId) => { - oldExpanded[rowId] = true + const rowIds = table.getRowModel().rowsById + Object.keys(rowIds).forEach((rowId) => { + if (rowIds[rowId]?.getCanExpand()) { + oldExpanded[rowId] = true + } }) } else { oldExpanded = old