From 190aba24ee3c0c7312c5e8ed9e0d04be9a2eb043 Mon Sep 17 00:00:00 2001 From: RaoHai Date: Fri, 6 May 2016 12:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=20Transfer=20Datasource=20=E5=92=8C=20CheckedK?= =?UTF-8?q?eys=20=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9=20(#1590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Transfer Datasource 和 CheckedKeys 逻辑修改 + close #1587 + 当 Transfer 的 Datasource 变化时,会移除组件的 CheckedKeys 中已不在 Datasource 中的 key * remove leftDataSource and rightDatasource from this.state --- components/transfer/index.jsx | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/components/transfer/index.jsx b/components/transfer/index.jsx index 6ab90fae61..c9651b8d80 100644 --- a/components/transfer/index.jsx +++ b/components/transfer/index.jsx @@ -45,7 +45,6 @@ export default class Transfer extends React.Component { constructor(props) { super(props); - this.state = { leftFilter: '', rightFilter: '', @@ -53,9 +52,16 @@ export default class Transfer extends React.Component { rightCheckedKeys: [], }; } - - splitDataSource() { - const { targetKeys, dataSource } = this.props; + componentWillReceiveProps(nextProps) { + const { leftCheckedKeys, rightCheckedKeys } = this.state; + const { leftDataSource, rightDataSource } = this.splitDataSource(nextProps); + this.setState({ + leftCheckedKeys: leftCheckedKeys.filter(data => leftDataSource.filter(leftData => leftData.key === data).length), + rightCheckedKeys: rightCheckedKeys.filter(data => rightDataSource.filter(rightData => rightData.key === data).length), + }); + } + splitDataSource(props) { + const { targetKeys, dataSource } = props; let leftDataSource = [...dataSource]; let rightDataSource = []; @@ -99,7 +105,7 @@ export default class Transfer extends React.Component { moveToRight = () => this.moveTo('right') getGlobalCheckStatus(direction) { - const { leftDataSource, rightDataSource } = this.splitDataSource(); + const { leftDataSource, rightDataSource } = this.splitDataSource(this.props); const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state; const dataSource = direction === 'left' ? leftDataSource : rightDataSource; @@ -134,7 +140,7 @@ export default class Transfer extends React.Component { } handleSelectAll = (direction) => { - const { leftDataSource, rightDataSource } = this.splitDataSource(); + const { leftDataSource, rightDataSource } = this.splitDataSource(this.props); const { leftFilter, rightFilter } = this.state; const dataSource = direction === 'left' ? leftDataSource : rightDataSource; const filter = direction === 'left' ? leftFilter : rightFilter; @@ -201,7 +207,7 @@ export default class Transfer extends React.Component { } = this.props; const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state; - const { leftDataSource, rightDataSource } = this.splitDataSource(); + const { leftDataSource, rightDataSource } = this.splitDataSource(this.props); const leftActive = rightCheckedKeys.length > 0; const rightActive = leftCheckedKeys.length > 0;