Skip to content

Commit

Permalink
🐛 Fix selectedRows missing when there is childrenColumnName in Table
Browse files Browse the repository at this point in the history
close #16614
  • Loading branch information
afc163 committed May 16, 2019
1 parent 8836439 commit c4128c0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
15 changes: 9 additions & 6 deletions components/table/Table.tsx
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
32 changes: 32 additions & 0 deletions components/table/__tests__/Table.rowSelection.test.js
Expand Up @@ -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 }];

Expand Down

0 comments on commit c4128c0

Please sign in to comment.