Skip to content

Commit

Permalink
fix(Table): fix record key when there is no key to use
Browse files Browse the repository at this point in the history
when there is no key to use, instead key with index is wrong when have disabled record, fix this.

close #67
  • Loading branch information
ZxBing0066 committed Nov 23, 2018
1 parent b2ccb7c commit 3d795f9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,14 @@ class Table extends Component {
total
};
};
handleToggleCurrentPage = (enableDataSourceOfCurrentPage, checked) => {
handleToggleCurrentPage = (dataSourceOfCurrentPage, checked) => {
const { selectedRowKeyMap } = this.state;
const { rowSelection = {} } = this.props;
const extendSelectedRowKeyMap = {};
_.each(enableDataSourceOfCurrentPage, (record, index) => {
_.each(dataSourceOfCurrentPage, (record, index) => {
if (rowSelection.getDisabledOfRow && rowSelection.getDisabledOfRow(record)) {
return;
}
const key = this.getRowKey(record, index);
extendSelectedRowKeyMap[key] = checked;
});
Expand Down Expand Up @@ -485,22 +489,31 @@ class Table extends Component {

if (rowSelection) {
let enableDataSourceOfCurrentPage = dataSourceOfCurrentPage;
let selectedEnableDataSourceOfCurrentPage;
if (rowSelection.getDisabledOfRow) {
enableDataSourceOfCurrentPage = _.filter(
dataSourceOfCurrentPage,
record => !rowSelection.getDisabledOfRow(record)
);
selectedEnableDataSourceOfCurrentPage = _.filter(
dataSourceOfCurrentPage,
(record, index) =>
!rowSelection.getDisabledOfRow(record) && selectedRowKeyMap[this.getRowKey(record, index)]
);
} else {
selectedEnableDataSourceOfCurrentPage = _.filter(
dataSourceOfCurrentPage,
(record, index) => selectedRowKeyMap[this.getRowKey(record, index)]
);
}
const selectedCountOfCurrentPage = _.filter(
enableDataSourceOfCurrentPage,
(record, index) => selectedRowKeyMap[this.getRowKey(record, index)]
).length;
const selectedEnableDataSourceOfCurrentPageCount = selectedEnableDataSourceOfCurrentPage.length;
const isAllSelected =
selectedCountOfCurrentPage === enableDataSourceOfCurrentPage.length && selectedCountOfCurrentPage > 0;
selectedEnableDataSourceOfCurrentPageCount === enableDataSourceOfCurrentPage.length &&
selectedEnableDataSourceOfCurrentPageCount > 0;
newColumns.unshift({
title: (
<Checkbox
onChange={() => this.handleToggleCurrentPage(enableDataSourceOfCurrentPage, !isAllSelected)}
onChange={() => this.handleToggleCurrentPage(dataSourceOfCurrentPage, !isAllSelected)}
checked={isAllSelected}
/>
),
Expand Down
4 changes: 4 additions & 0 deletions src/components/Table/Table.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* 这是 Table 组件

### 提示

* 为了确保数据的准确性请务必保证每条数据存在有效不重复的 key 或者使用 rowKey 来指定 key 的获取方式,表格中将会依照 key 来进行选择等操作。不传入将会使用数据在每一页中的 index 来作为 key,可能会造成 key 重复而导致错误

### 演示

* 演示
Expand Down
60 changes: 60 additions & 0 deletions src/components/Table/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { mount } from 'enzyme';

import Table from 'src/components/Table';

jest.unmock('rc-trigger');

describe('Table', () => {
test('select', () => {
const error = (global.console.error = jest.fn());
const onRowSelect = jest.fn();
let onRowSelectTimes = 0;

class Demo extends React.Component {
render() {
const dataSource = new Array(100).fill(null).map((v, i) => ({
index: `index-${i}`,
i
}));
const columns = new Array(5).fill(null).map((v, i) => ({
title: `title-${i}`,
key: `title-${i}`,
width: 200,
render: record => <span>content {record.index}</span>
}));
return (
<div>
<div className="demo-wrap">
<Table
rowSelection={{
defaultSelectedRowKeys: [1, 2, 4, 5],
onChange: onRowSelect,
getDisabledOfRow: record => record.i < 4
}}
dataSource={dataSource}
columns={columns}
{...this.props}
/>
</div>
</div>
);
}
}

const wrapper = mount(<Demo />);

expect(error).toHaveBeenCalledTimes(1);

expect(wrapper.find('.uc-fe-table-tbody i.icon__checkbox-ed').length).toBe(4);
expect(wrapper.find('.uc-fe-table-tbody i[disabled=true]').length).toBe(4);
expect(wrapper.find('.uc-fe-table-tbody i.icon__checkbox-ed[disabled=true]').length).toBe(2);

wrapper.find('.uc-fe-table-thead i.icon__checkbox').simulate('click');
expect(wrapper.find('.uc-fe-table-tbody i.icon__checkbox-ed').length).toBe(8);
expect(wrapper.find('.uc-fe-table-tbody i[disabled=true]').length).toBe(4);
expect(wrapper.find('.uc-fe-table-tbody i.icon__checkbox-ed[disabled=true]').length).toBe(2);
expect(onRowSelect).toHaveBeenCalledTimes(++onRowSelectTimes);
expect(onRowSelect).toHaveBeenLastCalledWith(['1', '2', '4', '5', '6', '7', '8', '9']);
});
});

0 comments on commit 3d795f9

Please sign in to comment.