Improve transfer perfermance, close #2112

This commit is contained in:
afc163 2016-06-21 17:41:39 +08:00
parent 48e8c513f5
commit 3c408e0d22
2 changed files with 29 additions and 8 deletions

View File

@ -55,13 +55,28 @@ export default class Transfer extends React.Component {
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const { leftCheckedKeys, rightCheckedKeys } = this.state; const { leftCheckedKeys, rightCheckedKeys } = this.state;
const { leftDataSource, rightDataSource } = this.splitDataSource(nextProps); if (nextProps.targetKeys !== this.props.targetKeys ||
this.setState({ nextProps.dataSource !== this.props.dataSource) {
leftCheckedKeys: leftCheckedKeys.filter(data => leftDataSource.filter(leftData => leftData.key === data).length), // clear cached splited dataSource
rightCheckedKeys: rightCheckedKeys.filter(data => rightDataSource.filter(rightData => rightData.key === data).length), this.splitedDataSource = null;
});
const { dataSource, targetKeys } = nextProps;
// clear key nolonger existed
// clear checkedKeys according to targetKeys
this.setState({
leftCheckedKeys: leftCheckedKeys
.filter(data => dataSource.filter(item => item.key === data).length)
.filter(data => targetKeys.filter(key => key === data).length === 0),
rightCheckedKeys: rightCheckedKeys
.filter(data => dataSource.filter(item => item.key === data).length)
.filter(data => targetKeys.filter(key => key === data).length > 0),
});
}
} }
splitDataSource(props) { splitDataSource(props) {
if (this.splitedDataSource) {
return this.splitedDataSource;
}
const { targetKeys } = props; const { targetKeys } = props;
let { dataSource } = props; let { dataSource } = props;
@ -86,10 +101,12 @@ export default class Transfer extends React.Component {
}); });
} }
return { this.splitedDataSource = {
leftDataSource, leftDataSource,
rightDataSource, rightDataSource,
}; };
return this.splitedDataSource;
} }
moveTo = (direction) => { moveTo = (direction) => {
@ -194,7 +211,7 @@ export default class Transfer extends React.Component {
handleSelect = (direction, selectedItem, checked) => { handleSelect = (direction, selectedItem, checked) => {
const { leftCheckedKeys, rightCheckedKeys } = this.state; const { leftCheckedKeys, rightCheckedKeys } = this.state;
const holder = direction === 'left' ? leftCheckedKeys : rightCheckedKeys; const holder = direction === 'left' ? [...leftCheckedKeys] : [...rightCheckedKeys];
let index; let index;
holder.forEach((key, i) => { holder.forEach((key, i) => {
if (key === selectedItem.key) { if (key === selectedItem.key) {

View File

@ -3,6 +3,7 @@ import Checkbox from '../checkbox';
import Search from './search'; import Search from './search';
import classNames from 'classnames'; import classNames from 'classnames';
import Animate from 'rc-animate'; import Animate from 'rc-animate';
import PureRenderMixin from 'react-addons-pure-render-mixin';
function noop() { function noop() {
} }
@ -60,6 +61,10 @@ export default class TransferList extends React.Component {
}, 0); }, 0);
} }
shouldComponentUpdate(...args) {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
handleSelectAll = () => { handleSelectAll = () => {
this.props.handleSelectAll(); this.props.handleSelectAll();
} }
@ -128,7 +133,6 @@ export default class TransferList extends React.Component {
} else { } else {
itemText = renderResult; itemText = renderResult;
} }
const filterResult = this.matchFilter(itemText, filter); const filterResult = this.matchFilter(itemText, filter);
return !!filterResult; return !!filterResult;
}).map((item) => { }).map((item) => {