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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module.exports = {
'import/no-named-as-default-member': 0,
'jsx-a11y/label-has-for': 0,
'jsx-a11y/label-has-associated-control': 0,
'jsx-a11y/control-has-associated-label': 0,
},
};
1 change: 1 addition & 0 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
index={index}
dataIndex={dataIndex}
render={render}
shouldCellUpdate={column.shouldCellUpdate}
{...fixedInfo}
appendNode={appendCellNode}
additionalProps={additionalCellProps}
Expand Down
14 changes: 12 additions & 2 deletions src/Cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface CellProps<RecordType extends DefaultRecordType> {
ellipsis?: boolean;
align?: AlignType;

shouldCellUpdate?: (record: RecordType) => boolean;

// Fixed
fixLeft?: number | false;
fixRight?: number | false;
Expand Down Expand Up @@ -197,7 +199,15 @@ function Cell<RecordType extends DefaultRecordType>(
);
}

const RefCell = React.forwardRef(Cell);
const RefCell = React.forwardRef<any, CellProps<any>>(Cell);
RefCell.displayName = 'Cell';

export default RefCell;
const MemoCell = React.memo(RefCell, (_, next: CellProps<any>) => {
if (next.shouldCellUpdate) {
return !next.shouldCellUpdate(next.record);
}

return false;
});

export default MemoCell;
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
record: RecordType,
index: number,
) => React.ReactNode | RenderedCell<RecordType>;
shouldCellUpdate?: (record: RecordType) => boolean;
rowSpan?: number;
width?: number | string;
onCell?: GetComponentProps<RecordType>;
Expand Down
44 changes: 44 additions & 0 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,4 +925,48 @@ describe('Table.Basic', () => {
);
}).not.toThrow();
});

it('shouldCellUpdate', () => {
const record = { key: 1 };
let shouldUpdate = false;
let renderTimes = 0;

const Demo = () => {
const [, forceUpdate] = React.useState({});

return (
<>
<Table
data={[record]}
columns={[
{
dataIndex: 'key',
shouldCellUpdate: () => shouldUpdate,
render() {
renderTimes += 1;
return null;
},
},
]}
/>
<button
type="button"
onClick={() => {
forceUpdate({});
}}
/>
</>
);
};

const wrapper = mount(<Demo />);
renderTimes = 0;

wrapper.find('button').simulate('click');
expect(renderTimes).toEqual(0);

shouldUpdate = true;
wrapper.find('button').simulate('click');
expect(renderTimes).toEqual(1);
});
});