diff --git a/components/table/Table.tsx b/components/table/Table.tsx index 4cb60c1420..4185d34146 100755 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -153,7 +153,12 @@ export default class Table extends React.Component, TableState< const key = this.getRecordKey(item, index); // Cache checkboxProps if (!this.CheckboxPropsCache[key]) { - this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item); + const checkboxProps = (this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item)); + warning( + !('checked' in checkboxProps) && !('defaultChecked' in checkboxProps), + 'Table', + 'Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead.', + ); } return this.CheckboxPropsCache[key]; }; diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 74dd25ebdb..e3a0560723 100644 --- a/components/table/__tests__/Table.rowSelection.test.js +++ b/components/table/__tests__/Table.rowSelection.test.js @@ -4,6 +4,16 @@ import Table from '..'; import Checkbox from '../../checkbox'; describe('Table.rowSelection', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + afterEach(() => { + errorSpy.mockReset(); + }); + + afterAll(() => { + errorSpy.mockRestore(); + }); + const columns = [ { title: 'Name', @@ -120,6 +130,10 @@ describe('Table.rowSelection', () => { checkboxs = wrapper.find('input'); expect(checkboxs.at(1).props().checked).toBe(true); expect(checkboxs.at(2).props().checked).toBe(true); + + expect(errorSpy).toBeCalledWith( + 'Warning: [antd: Table] Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead.', + ); }); it('can be controlled', () => {