Browse Source

refactor: improve performance of Transfer #5997 (#6001)

* fix bug #5997

* change order for review
pull/6059/head
wangtao0101 8 years ago
committed by Benjy Cui
parent
commit
0d070d3763
  1. 55
      components/transfer/index.tsx
  2. 6
      tsconfig.json

55
components/transfer/index.tsx

@ -91,24 +91,34 @@ abstract class Transfer extends React.Component<TransferProps, any> {
componentWillReceiveProps(nextProps: TransferProps) { componentWillReceiveProps(nextProps: TransferProps) {
const { sourceSelectedKeys, targetSelectedKeys } = this.state; const { sourceSelectedKeys, targetSelectedKeys } = this.state;
if (nextProps.targetKeys !== this.props.targetKeys || if (nextProps.targetKeys !== this.props.targetKeys ||
nextProps.dataSource !== this.props.dataSource) { nextProps.dataSource !== this.props.dataSource) {
// clear cached splited dataSource // clear cached splited dataSource
this.splitedDataSource = null; this.splitedDataSource = null;
const { dataSource, targetKeys = [] } = nextProps; if (!nextProps.selectedKeys) {
function existInDateSourcekey(key) { // clear key nolonger existed
return dataSource.some(item => item.key === key); // 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);
}
if (targetSelectedKeys.includes(record.key) && targetKeys.includes(record.key)) {
newTargetSelectedKeys.push(record.key);
}
});
this.setState({
sourceSelectedKeys: newSourceSelectedKeys,
targetSelectedKeys: newTargetSelectedKeys,
});
} }
// clear key nolonger existed
// clear checkedKeys according to targetKeys
this.setState({
sourceSelectedKeys: sourceSelectedKeys.filter(existInDateSourcekey)
.filter(data => targetKeys.filter(key => key === data).length === 0),
targetSelectedKeys: targetSelectedKeys.filter(existInDateSourcekey)
.filter(data => targetKeys.filter(key => key === data).length > 0),
});
} }
if (nextProps.selectedKeys) { if (nextProps.selectedKeys) {
const targetKeys = nextProps.targetKeys; const targetKeys = nextProps.targetKeys;
this.setState({ this.setState({
@ -117,24 +127,25 @@ abstract class Transfer extends React.Component<TransferProps, any> {
}); });
} }
} }
splitDataSource(props: TransferProps) { splitDataSource(props: TransferProps) {
if (this.splitedDataSource) { if (this.splitedDataSource) {
return this.splitedDataSource; return this.splitedDataSource;
} }
const { rowKey, dataSource, targetKeys = [] } = props; const { rowKey, dataSource, targetKeys = [] } = props;
if (rowKey) {
dataSource.forEach(record => {
record.key = rowKey(record);
});
}
const leftDataSource = dataSource.filter(({ key }) => targetKeys.indexOf(key) === -1); const leftDataSource: TransferItem[] = [];
const rightDataSource: TransferItem[] = []; const rightDataSource: TransferItem[] = [];
targetKeys.forEach((targetKey) => {
const targetItem = dataSource.filter(record => record.key === targetKey)[0]; dataSource.forEach(record => {
if (targetItem) { if (rowKey) {
rightDataSource.push(targetItem); record.key = rowKey(record);
}
if (targetKeys.includes(record.key)) {
rightDataSource.push(record);
} else {
leftDataSource.push(record);
} }
}); });

6
tsconfig.json

@ -5,7 +5,11 @@
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"jsx": "preserve", "jsx": "preserve",
"target": "es6" "target": "es6",
"lib": [
"dom",
"es7"
]
}, },
"exclude": [ "exclude": [
"node_modules" "node_modules"

Loading…
Cancel
Save