Skip to content
Merged
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
33 changes: 33 additions & 0 deletions src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ describe('Table', () => {
expect(screen.getByRole('cell', {name: 'Bob'})).toBeInTheDocument();
});

it('clears a cell when its field is removed from the row', () => {
interface OptionalRoleRow extends Record<string, unknown> {
id: string;
name: string;
role?: string;
}

const optionalRoleColumns: TableColumn<OptionalRoleRow>[] = [
{key: 'name', header: 'Name'},
{key: 'role', header: 'Role'},
];
const {rerender} = render(
<Table
columns={optionalRoleColumns}
data={[{id: '1', name: 'Alice', role: 'Admin'}]}
idKey="id"
/>,
);

expect(screen.getByRole('cell', {name: 'Admin'})).toBeInTheDocument();

rerender(
<Table
columns={optionalRoleColumns}
data={[{id: '1', name: 'Alice'}]}
idKey="id"
/>,
);

const dataRow = screen.getAllByRole('row')[1];
expect(within(dataRow).getAllByRole('cell')[1]).toBeEmptyDOMElement();
});

it('applies visual context props through row and cell classes', () => {
const {rerender} = render(
<Table
Expand Down
9 changes: 2 additions & 7 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
import {useBaseTablePlugins} from 'components/Table/useBaseTablePlugins';
import {Text} from 'components/Text';
import isNonEmptyReactNode from 'internal/isNonEmptyReactNode';
import {shallowEqual} from 'internal/shallowEqual';
import useShallowEqualMemo from 'internal/useShallowEqualMemo';
import {cx} from 'utils/cx';

Expand Down Expand Up @@ -253,13 +254,7 @@ function areRowPropsEqual<T extends Record<string, unknown>>(
return false;
}

if (previous.item === next.item) {
return true;
}

return Object.keys(next.item).every(
key => previous.item[key] === next.item[key],
);
return shallowEqual(previous.item, next.item);
}

const MemoizedDataRow = memo(DataRowInner, areRowPropsEqual) as <
Expand Down