Skip to content

Commit

Permalink
fix(Table): fix defaultExpandAllRows
Browse files Browse the repository at this point in the history
fix defaultExpandAllRows when page change or datasource update

close #154
  • Loading branch information
ZxBing0066 committed Jul 2, 2019
1 parent 8d50bcb commit eb9b767
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/components/Table/Table.jsx
Expand Up @@ -219,6 +219,17 @@ class Table extends Component {
});
}
};
getExpandedRowKeys = (dataSource, changedUnExpandedRowKeys) => {
const flatDataSource = this.flatDataSource(dataSource);
const expandedRowKeys = [];
_.each(flatDataSource, item => {
const { key } = item;
if (!changedUnExpandedRowKeys[key]) {
expandedRowKeys.push(key);
}
});
return expandedRowKeys;
};
/**
* @deprecated
*/
Expand Down Expand Up @@ -680,6 +691,22 @@ class Table extends Component {
renderFooter = option => {
return <div>{this.renderEmptyAndErrorInfo(option)}</div>;
};
onExpandHandler = (expanded, record) => {
const { changedUnExpandedRowKeys = {} } = this.state;
const { onExpand } = this.props;
const rowKey = this.getRowKey(record);
if (expanded) {
delete changedUnExpandedRowKeys[rowKey];
} else {
changedUnExpandedRowKeys[rowKey] = true;
}
this.setState({
changedUnExpandedRowKeys
});
if (onExpand) {
onExpand(expanded, record);
}
};
render() {
/* eslint-disable no-unused-vars */
let {
Expand All @@ -696,12 +723,14 @@ class Table extends Component {
expandedRowRender,
expandIconAsCell,
expandIconColumnIndex,
defaultExpandAllRows,
title = () => {},
footer = () => {},
locale,
hideExpandIcon,
onRow = () => {},
components,
onExpand,
...rest
} = this.props;
if (emptyContent === undefined) {
Expand All @@ -712,6 +741,18 @@ class Table extends Component {
const { filters = {}, searchValue, columnConfig } = this.state;
const { dataSource, total } = this.getDataSource();
const columns = this.getColumns(dataSource);
const defaultExpandAllRowsProps = !defaultExpandAllRows
? null
: (() => {
const { changedUnExpandedRowKeys = {} } = this.state;
const expandedRowKeys = this.getExpandedRowKeys(dataSource, changedUnExpandedRowKeys);

return {
expandedRowKeys,
onExpand: this.onExpandHandler
};
})();

return (
<TableContext.Provider
value={{
Expand All @@ -725,6 +766,7 @@ class Table extends Component {
<TableWrap className={className} style={style} hideExpandIcon={hideExpandIcon}>
<PopupContainer innerRef={_ref => (this.popupContainer = _ref)} />
<RcTable
{...defaultExpandAllRowsProps}
{...rest}
prefixCls={prefixCls}
data={dataSource}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Table/Table.md
Expand Up @@ -4,7 +4,8 @@

### 提示

* 为了确保数据的准确性请务必保证每条数据存在有效不重复的 key 或者使用 rowKey 来指定 key 的获取方式,表格中将会依照 key 来进行选择等操作。不传入将会使用数据在每一页中的 index 来作为 key,可能会造成 key 重复而导致错误
* 为了确保数据的准确性请务必保证每条数据存在有效不重复的 key 或者使用 rowKey 来指定 key 的获取方式,表格中将会依照 key 来进行选择等操作。不传入将会使用数据在每一页中的 index 来作为 key,可能会造成 key 重复而导致错误,甚至造成各种奇怪的错误现象。
* rowKey 支持函数,第二个参数为 record 在当前页面的 index,强烈不推荐使用!!!请务必注意。

### 演示

Expand Down Expand Up @@ -85,6 +86,8 @@

* defaultExpandAllRows - 默认展开扩展列

`使用时务必注意 rowKey 的使用,使用此属性可能会影响到表格的性能`

```js {"codepath": "defaultExpandAllRows.jsx"}
```

Expand Down
1 change: 0 additions & 1 deletion src/components/Table/__demo__/defaultExpandAllRows.jsx
Expand Up @@ -20,7 +20,6 @@ class Demo extends React.Component {
<Table
defaultExpandAllRows
expandedRowRender={record => <p>this is the expandedRow of {record.key}</p>}
onExpandedRowsChange={(...args) => console.log('onExpandedRowsChange: ', ...args)}
onExpand={(...args) => console.log('onExpand: ', ...args)}
dataSource={dataSource}
columns={columns}
Expand Down

0 comments on commit eb9b767

Please sign in to comment.