From a081100cdcd586446a23f09f9e291e50232e9c53 Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Thu, 11 May 2017 11:38:32 +0800 Subject: [PATCH] refactor: simplify code --- components/transfer/index.tsx | 46 +++++++++++++++-------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/components/transfer/index.tsx b/components/transfer/index.tsx index 2b29face1a..ee015648d1 100644 --- a/components/transfer/index.tsx +++ b/components/transfer/index.tsx @@ -74,7 +74,10 @@ abstract class Transfer extends React.Component { lazy: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]), }; - splitedDataSource: any; + splitedDataSource: { + leftDataSource: TransferItem[], + rightDataSource: TransferItem[], + } | null; constructor(props: TransferProps) { super(props); @@ -102,15 +105,15 @@ abstract class Transfer extends React.Component { // clear key nolonger existed // clear checkedKeys according to targetKeys const { dataSource, targetKeys = [] } = nextProps; + const newSourceSelectedKeys: String[] = []; const newTargetSelectedKeys: String[] = []; - - dataSource.forEach(record => { - if (sourceSelectedKeys.includes(record.key) && !targetKeys.includes(record.key)) { - newSourceSelectedKeys.push(record.key); + dataSource.forEach(({ key }) => { + if (sourceSelectedKeys.includes(key) && !targetKeys.includes(key)) { + newSourceSelectedKeys.push(key); } - if (targetSelectedKeys.includes(record.key) && targetKeys.includes(record.key)) { - newTargetSelectedKeys.push(record.key); + if (targetSelectedKeys.includes(key) && targetKeys.includes(key)) { + newTargetSelectedKeys.push(key); } }); this.setState({ @@ -123,8 +126,8 @@ abstract class Transfer extends React.Component { if (nextProps.selectedKeys) { const targetKeys = nextProps.targetKeys; this.setState({ - sourceSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.indexOf(key) === -1), - targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.indexOf(key) > -1), + sourceSelectedKeys: nextProps.selectedKeys.filter(key => !targetKeys.includes(key)), + targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.includes(key)), }); } } @@ -134,34 +137,25 @@ abstract class Transfer extends React.Component { return this.splitedDataSource; } - const { rowKey, dataSource, targetKeys = [] } = props; + const { dataSource, rowKey, targetKeys = [] } = props; const leftDataSource: TransferItem[] = []; - const rightDataSource: TransferItem[] = []; - const tempRightDataSource: TransferItem[] = []; - + const rightDataSource: TransferItem[] = new Array(targetKeys.length); dataSource.forEach(record => { if (rowKey) { record.key = rowKey(record); } - if (targetKeys.includes(record.key)) { - tempRightDataSource.push(record); + + // rightDataSource should be ordered by targetKeys + // leftDataSource should be ordered by dataSource + const indexOfKey = targetKeys.indexOf(record.key); + if (indexOfKey !== -1) { + rightDataSource[indexOfKey] = record; } else { leftDataSource.push(record); } }); - const itemMap = {}; - tempRightDataSource.forEach((item) => { - itemMap[item.key] = item; - }); - - targetKeys.forEach((targetKey) => { - if (itemMap[targetKey]) { - rightDataSource.push(itemMap[targetKey]); - } - }); - this.splitedDataSource = { leftDataSource, rightDataSource,