🐛 Fix selectedRows missing when there is childrenColumnName in Table

close #16614
This commit is contained in:
afc163 2019-05-16 14:50:24 +08:00
parent 88364399e3
commit c4128c0d14
No known key found for this signature in database
GPG Key ID: 738F973FCE5C6B48
2 changed files with 41 additions and 6 deletions

View File

@ -97,6 +97,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
rowKey: 'key',
showHeader: true,
sortDirections: ['ascend', 'descend'],
childrenColumnName: 'children',
};
CheckboxPropsCache: {
@ -524,7 +525,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
const key = this.getRecordKey(record, rowIndex);
const { pivot } = this.state;
const rows = this.getFlatCurrentPageData(this.props.childrenColumnName);
const rows = this.getFlatCurrentPageData();
let realIndex = rowIndex;
if (this.props.expandedRowRender) {
realIndex = rows.findIndex(row => this.getRecordKey(row, rowIndex) === key);
@ -604,7 +605,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
};
handleSelectRow = (selectionKey: string, index: number, onSelectFunc: SelectionItemSelectFn) => {
const data = this.getFlatCurrentPageData(this.props.childrenColumnName);
const data = this.getFlatCurrentPageData();
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
const changeableRowKeys = data
@ -760,10 +761,10 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
};
renderRowSelection(prefixCls: string, locale: TableLocale) {
const { rowSelection, childrenColumnName } = this.props;
const { rowSelection } = this.props;
const columns = this.columns.concat();
if (rowSelection) {
const data = this.getFlatCurrentPageData(childrenColumnName).filter((item, index) => {
const data = this.getFlatCurrentPageData().filter((item, index) => {
if (rowSelection.getCheckboxProps) {
return !this.getCheckboxPropsByItem(item, index).disabled;
}
@ -1066,10 +1067,12 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}
getFlatData() {
return flatArray(this.getLocalData(null, false));
const { childrenColumnName } = this.props;
return flatArray(this.getLocalData(null, false), childrenColumnName);
}
getFlatCurrentPageData(childrenColumnName: string | undefined) {
getFlatCurrentPageData() {
const { childrenColumnName } = this.props;
return flatArray(this.getCurrentPageData(), childrenColumnName);
}

View File

@ -670,6 +670,38 @@ describe('Table.rowSelection', () => {
expect(checkboxAll.instance().state).toEqual({ indeterminate: false, checked: true });
});
// https://github.com/ant-design/ant-design/issues/16614
it('should get selectedRows correctly when set childrenColumnName', () => {
const onChange = jest.fn();
const newDatas = [
{
key: 1,
name: 'Jack',
list: [
{
key: 11,
name: 'John Brown',
},
],
},
];
const wrapper = mount(
<Table
columns={columns}
dataSource={newDatas}
childrenColumnName="list"
rowSelection={{ onChange }}
expandedRowKeys={[1]}
/>,
);
const checkboxes = wrapper.find('input');
checkboxes.at(2).simulate('change', { target: { checked: true } });
expect(onChange).toHaveBeenLastCalledWith([11], [newDatas[0].list[0]]);
checkboxes.at(1).simulate('change', { target: { checked: true } });
const item0 = { ...newDatas[0], list: undefined };
expect(onChange).toHaveBeenLastCalledWith([11, 1], [item0, newDatas[0].list[0]]);
});
it('clear selection className when remove `rowSelection`', () => {
const dataSource = [{ id: 1, name: 'Hello', age: 10 }, { id: 2, name: 'World', age: 30 }];