fix: render correctly when set childrenColumnName

This commit is contained in:
zy410419243 2018-12-20 14:44:45 +08:00 committed by 偏右
parent 6106e459a3
commit 2bfc23b6f1
2 changed files with 42 additions and 6 deletions

View File

@ -489,7 +489,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection); let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
const key = this.getRecordKey(record, rowIndex); const key = this.getRecordKey(record, rowIndex);
const { pivot } = this.state; const { pivot } = this.state;
const rows = this.getFlatCurrentPageData(); const rows = this.getFlatCurrentPageData(this.props.childrenColumnName);
let realIndex = rowIndex; let realIndex = rowIndex;
if (this.props.expandedRowRender) { if (this.props.expandedRowRender) {
realIndex = rows.findIndex(row => this.getRecordKey(row, rowIndex) === key); realIndex = rows.findIndex(row => this.getRecordKey(row, rowIndex) === key);
@ -571,7 +571,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}; };
handleSelectRow = (selectionKey: string, index: number, onSelectFunc: SelectionItemSelectFn) => { handleSelectRow = (selectionKey: string, index: number, onSelectFunc: SelectionItemSelectFn) => {
const data = this.getFlatCurrentPageData(); const data = this.getFlatCurrentPageData(this.props.childrenColumnName);
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection(); const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection); const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
const changeableRowKeys = data const changeableRowKeys = data
@ -723,10 +723,10 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}; };
renderRowSelection(locale: TableLocale) { renderRowSelection(locale: TableLocale) {
const { prefixCls, rowSelection } = this.props; const { prefixCls, rowSelection, childrenColumnName } = this.props;
const columns = this.columns.concat(); const columns = this.columns.concat();
if (rowSelection) { if (rowSelection) {
const data = this.getFlatCurrentPageData().filter((item, index) => { const data = this.getFlatCurrentPageData(childrenColumnName).filter((item, index) => {
if (rowSelection.getCheckboxProps) { if (rowSelection.getCheckboxProps) {
return !this.getCheckboxPropsByItem(item, index).disabled; return !this.getCheckboxPropsByItem(item, index).disabled;
} }
@ -1025,8 +1025,8 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
return flatArray(this.getLocalData(null, false)); return flatArray(this.getLocalData(null, false));
} }
getFlatCurrentPageData() { getFlatCurrentPageData(childrenColumnName: string | undefined) {
return flatArray(this.getCurrentPageData()); return flatArray(this.getCurrentPageData(), childrenColumnName);
} }
recursiveSort(data: T[], sorterFn: (a: any, b: any) => number): T[] { recursiveSort(data: T[], sorterFn: (a: any, b: any) => number): T[] {

View File

@ -615,4 +615,40 @@ describe('Table.rowSelection', () => {
expect(onChange.mock.calls[1][0].length).toBe(2); expect(onChange.mock.calls[1][0].length).toBe(2);
expect(onChange.mock.calls[1][1].length).toBe(2); expect(onChange.mock.calls[1][1].length).toBe(2);
}); });
it('render correctly when set childrenColumnName', () => {
const newDatas = [
{
key: 1,
name: 'Jack',
children: [
{
key: 11,
name: 'John Brown',
},
],
},
{
key: 2,
name: 'Lucy',
children: [
{
key: 21,
name: 'Lucy Brown',
},
],
},
];
const wrapper = mount(
<Table columns={columns} dataSource={newDatas} childrenColumnName="test" rowSelection={{}} />,
);
const checkboxes = wrapper.find('input');
const checkboxAll = wrapper.find('SelectionCheckboxAll');
checkboxes.at(1).simulate('change', { target: { checked: true } });
expect(checkboxAll.instance().state).toEqual({ indeterminate: true, checked: false });
checkboxes.at(2).simulate('change', { target: { checked: true } });
expect(checkboxAll.instance().state).toEqual({ indeterminate: false, checked: true });
});
}); });