add warning when use getCheckboxProps (#15231)

This commit is contained in:
zombieJ 2019-03-06 16:48:14 +08:00 committed by GitHub
parent 074363b514
commit 3fbeff3fcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -153,7 +153,12 @@ export default class Table<T> extends React.Component<TableProps<T>, 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];
};

View File

@ -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', () => {