diff --git a/packages/react/src/components/DataTable/DataTable.tsx b/packages/react/src/components/DataTable/DataTable.tsx index 840d7905a364..ce9368471d64 100644 --- a/packages/react/src/components/DataTable/DataTable.tsx +++ b/packages/react/src/components/DataTable/DataTable.tsx @@ -334,6 +334,7 @@ interface DataTableState { isExpandedAll: boolean; rowIds: string[]; rowsById: Record>; + headers: DataTableHeader[]; shouldShowBatchActions: boolean; sortDirection: DataTableSortState; sortHeaderKey: string | null; @@ -379,12 +380,8 @@ export const DataTable = ( useEffect(() => { const nextRowIds = rows.map((row) => row.id); - const nextHeaders = headers.map((header) => header.key); const hasRowIdsChanged = !isEqual(nextRowIds, state.rowIds); - const currentHeaders = Array.from( - new Set(Object.keys(state.cellsById).map((id) => id.split(':')[1])) - ); - const hasHeadersChanged = !isEqual(nextHeaders, currentHeaders); + const hasHeadersChanged = !isEqual(headers, state.headers); const currentRows = state.rowIds.map((id) => { const row = state.rowsById[id]; return headers.reduce( @@ -664,7 +661,7 @@ export const DataTable = ( ? filterRows({ cellsById: state.cellsById, getCellId, - headers, + headers: state.headers, inputValue: state.filterInputValue, rowIds: state.rowIds, }) @@ -860,7 +857,7 @@ export const DataTable = ( const renderProps: RenderProps = { // Data derived from state rows: denormalize(filteredRowIds, state.rowsById, state.cellsById), - headers: headers, + headers: state.headers, selectedRows: denormalize(selectedRows, state.rowsById, state.cellsById), // Prop accessors/getters diff --git a/packages/react/src/components/DataTable/__tests__/DataTable-test.js b/packages/react/src/components/DataTable/__tests__/DataTable-test.js index bb7964de4003..05a257e42144 100644 --- a/packages/react/src/components/DataTable/__tests__/DataTable-test.js +++ b/packages/react/src/components/DataTable/__tests__/DataTable-test.js @@ -1206,6 +1206,83 @@ describe('DataTable', () => { 'Field 3:A!', ]); }); + + it('should never use mismatched headers and rows for render props while syncing new props', () => { + const childrenSpy = jest.fn(() => null); + const initialHeaders = [ + { key: 'fieldA', header: 'Field A' }, + { key: 'fieldB', header: 'Field B' }, + ]; + const initialRows = [ + { id: 'a', fieldA: 'A:1', fieldB: 'B:1' }, + { id: 'b', fieldA: 'A:2', fieldB: 'B:2' }, + ]; + const nextHeaders = [ + { key: 'fieldB', header: 'Field B' }, + { key: 'fieldC', header: 'Field C' }, + ]; + const nextRows = [ + { id: 'a', fieldB: 'B:1', fieldC: 'C:1' }, + { id: 'b', fieldB: 'B:2', fieldC: 'C:2' }, + { id: 'c', fieldB: 'B:3', fieldC: 'C:3' }, + ]; + + const { rerender } = render( + + {childrenSpy} + + ); + + // Every render must use headers and rows from the same version: + // each row has exactly one cell per header, in header order. + const expectCellsToMatchHeaders = ({ headers, rows }) => { + const headerKeys = headers.map((header) => header.key); + rows.forEach((row) => { + expect(row.cells.map((cell) => cell.info.header)).toEqual( + headerKeys + ); + }); + }; + + childrenSpy.mock.calls.forEach(([renderProps]) => + expectCellsToMatchHeaders(renderProps) + ); + childrenSpy.mockClear(); + + rerender( + + {childrenSpy} + + ); + + const calls = childrenSpy.mock.calls.map( + ([renderProps]) => renderProps + ); + + // The render that happens before the sync effect still exposes the + // previous version of both headers and rows. + expect(calls.length).toBeGreaterThan(1); + expect(calls[0].headers.map((header) => header.key)).toEqual([ + 'fieldA', + 'fieldB', + ]); + expect(calls[0].rows.map((row) => row.id)).toEqual(['a', 'b']); + + calls.forEach(expectCellsToMatchHeaders); + + // The render that happens after the sync effect still uses the + // new versions of both headers and rows. + const lastCall = calls[calls.length - 1]; + expect(lastCall.headers.map((header) => header.key)).toEqual([ + 'fieldB', + 'fieldC', + ]); + expect(lastCall.rows.map((row) => row.id)).toEqual(['a', 'b', 'c']); + expect(lastCall.rows[2].cells.map((cell) => cell.value)).toEqual([ + 'B:3', + 'C:3', + ]); + }); }); describe('row click behavior', () => { diff --git a/packages/react/src/components/DataTable/state/__tests__/getDerivedStateFromProps-test.js b/packages/react/src/components/DataTable/state/__tests__/getDerivedStateFromProps-test.js index 10dd56e94df3..0065dda05131 100644 --- a/packages/react/src/components/DataTable/state/__tests__/getDerivedStateFromProps-test.js +++ b/packages/react/src/components/DataTable/state/__tests__/getDerivedStateFromProps-test.js @@ -33,6 +33,48 @@ describe('getDerivedStateFromProps', () => { ); }); + it('stores headers from props in state', () => { + const headers = [{ key: 'name', header: 'Name' }]; + const props = { rows: [], headers }; + const state = getDerivedStateFromProps(props, {}); + expect(state.headers).toEqual(headers); + }); + + it('updates headers in state when props.headers changes', () => { + const headersA = [{ key: 'name', header: 'Name' }]; + const headersB = [ + { key: 'col1', header: 'Column 1' }, + { key: 'col2', header: 'Column 2' }, + ]; + const stateA = getDerivedStateFromProps( + { rows: [], headers: headersA }, + {} + ); + expect(stateA.headers).toEqual(headersA); + const stateB = getDerivedStateFromProps( + { rows: [], headers: headersB }, + stateA + ); + expect(stateB.headers).toEqual(headersB); + }); + + it('should copy headers so that prop mutations do not affect already-derived state', () => { + const headersA = [{ key: 'name', header: 'Name' }]; + const stateA = getDerivedStateFromProps( + { rows: [], headers: headersA }, + {} + ); + expect(stateA.headers).not.toBe(headersA); + expect(stateA.headers[0]).not.toBe(headersA[0]); + headersA[0].key = 'newKey'; + expect(stateA.headers[0].key).toEqual('name'); + const stateB = getDerivedStateFromProps( + { rows: [], headers: headersA }, + stateA + ); + expect(stateB.headers[0].key).toEqual('newKey'); + }); + describe('with previous state', () => { let mockProps; diff --git a/packages/react/src/components/DataTable/state/getDerivedStateFromProps.js b/packages/react/src/components/DataTable/state/getDerivedStateFromProps.js index b95b7c81a869..03916daefd29 100644 --- a/packages/react/src/components/DataTable/state/getDerivedStateFromProps.js +++ b/packages/react/src/components/DataTable/state/getDerivedStateFromProps.js @@ -24,6 +24,7 @@ const getDerivedStateFromProps = (props, prevState) => { ); const hasSelectedRows = rowIds.some((id) => isSelectedRow(rowsById[id])); const state = { + headers: props.headers.map((header) => ({ ...header })), rowIds, rowsById, cellsById,