You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.0 KiB

9 years ago
import React, { Component, PropTypes } from 'react';
import List from './list.jsx';
import Operation from './operation.jsx';
import Search from './search.jsx';
9 years ago
function noop() {
}
class Transfer extends Component {
constructor(props) {
super(props);
this.state = {
leftFilter: '',
rightFilter: '',
9 years ago
leftCheckedKeys: [],
rightCheckedKeys: [],
9 years ago
};
}
9 years ago
splitDataSource() {
const { targetKeys, dataSource } = this.props;
9 years ago
let leftDataSource = Object.assign([], dataSource);
let rightDataSource = [];
9 years ago
9 years ago
if ( targetKeys.length > 0 ) {
targetKeys.forEach((targetKey) => {
rightDataSource.push(leftDataSource.find((data, index) => {
if( data.key === targetKey ) {
leftDataSource.splice(index, 1);
return true;
}
}));
9 years ago
});
}
9 years ago
return {
leftDataSource: leftDataSource,
rightDataSource: rightDataSource,
};
9 years ago
}
9 years ago
moveTo(direction) {
9 years ago
const { targetKeys } = this.props;
const { leftCheckedKeys, rightCheckedKeys } = this.state;
// move items to target box
const newTargetKeys = direction === 'right' ?
leftCheckedKeys.concat(targetKeys) :
targetKeys.filter((targetKey) => !rightCheckedKeys.some((checkedKey) => targetKey === checkedKey));
// empty checked keys
this.setState({
[direction === 'right' ? 'leftCheckedKeys' : 'rightCheckedKeys']: [],
});
this.props.onChange(newTargetKeys);
9 years ago
}
getGlobalCheckStatus(direction) {
9 years ago
const { leftDataSource, rightDataSource } = this.splitDataSource();
const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state;
9 years ago
const dataSource = direction === 'left' ? leftDataSource : rightDataSource;
const filter = direction === 'left' ? leftFilter : rightFilter;
const checkedKeys = direction === 'left' ? leftCheckedKeys : rightCheckedKeys;
const filteredDataSource = this.filterDataSource(dataSource, filter);
let globalCheckStatus;
if ( checkedKeys.length > 0 ) {
if ( checkedKeys.length < filteredDataSource.length ) {
globalCheckStatus = 'part';
} else {
globalCheckStatus = 'all';
}
} else {
globalCheckStatus = 'none';
}
return globalCheckStatus;
}
filterDataSource(dataSource, filter) {
const self = this;
return dataSource.filter(item => {
const itemText = self.props.render(item);
return self.matchFilter(itemText, filter);
});
}
matchFilter(text, filterText) {
const regex = new RegExp(filterText);
return text.match(regex);
}
handleSelectAll(direction) {
const { leftDataSource, rightDataSource } = this.splitDataSource();
const { leftFilter, rightFilter } = this.state;
const dataSource = direction === 'left' ? leftDataSource : rightDataSource;
const filter = direction === 'left' ? leftFilter : rightFilter;
const checkStatus = this.getGlobalCheckStatus(direction);
9 years ago
let holder = [];
if ( checkStatus === 'all' ) {
9 years ago
holder = [];
} else {
holder = this.filterDataSource(dataSource, filter).map(item => item.key);
9 years ago
}
this.setState({
9 years ago
[direction === 'left' ? 'leftCheckedKeys' : 'rightCheckedKeys']: holder,
9 years ago
});
}
handleFilter(direction, e) {
9 years ago
this.setState({
// deselect all
[direction === 'left' ? 'leftCheckedKeys' : 'rightCheckedKeys']: [],
// add filter
9 years ago
[direction === 'left' ? 'leftFilter' : 'rightFilter']: e.target.value,
});
}
9 years ago
handleClear(direction) {
9 years ago
this.setState({
[direction === 'left' ? 'leftFilter' : 'rightFilter']: '',
9 years ago
});
9 years ago
}
9 years ago
9 years ago
handleSelect(direction, selectedItem, checked) {
const { leftCheckedKeys, rightCheckedKeys } = this.state;
const holder = direction === 'left' ? leftCheckedKeys : rightCheckedKeys;
const index = holder.findIndex((key) => key === selectedItem.key);
if ( index > -1 ) {
holder.splice(index, 1);
}
if ( checked ) {
holder.push(selectedItem.key);
}
9 years ago
this.setState({
9 years ago
[direction === 'left' ? 'leftCheckedKeys' : 'rightCheckedKeys']: holder,
9 years ago
});
}
render() {
9 years ago
const { prefixCls, titles, operations, showSearch, searchPlaceholder, body, footer, boxWidth } = this.props;
9 years ago
const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state;
9 years ago
9 years ago
const { leftDataSource, rightDataSource } = this.splitDataSource();
const leftActive = rightCheckedKeys.length > 0;
const rightActive = leftCheckedKeys.length > 0;
const leftCheckStatus = this.getGlobalCheckStatus('left');
const rightCheckStatus = this.getGlobalCheckStatus('right');
9 years ago
return <div className={prefixCls}>
9 years ago
<List title={titles[0]}
9 years ago
dataSource={leftDataSource}
filter={leftFilter}
9 years ago
width={boxWidth}
9 years ago
checkedKeys={leftCheckedKeys}
checkStatus={leftCheckStatus}
handleFilter={this.handleFilter.bind(this, 'left')}
9 years ago
handleClear={this.handleClear.bind(this, 'left')}
9 years ago
handleSelect={this.handleSelect.bind(this, 'left')}
handleSelectAll={this.handleSelectAll.bind(this, 'left')}
9 years ago
position="left"
9 years ago
render={this.props.render}
9 years ago
showSearch={showSearch}
9 years ago
searchPlaceholder={searchPlaceholder}
9 years ago
body={body}
footer={footer}
/>
9 years ago
<Operation rightActive={rightActive} rightArrowText={operations[0]} moveToRight={this.moveTo.bind(this, 'right')}
leftActive={leftActive} leftArrowText={operations[1]} moveToLeft={this.moveTo.bind(this, 'left')} />
<List title={titles[1]}
9 years ago
dataSource={rightDataSource}
filter={rightFilter}
9 years ago
width={boxWidth}
9 years ago
checkedKeys={rightCheckedKeys}
checkStatus={rightCheckStatus}
handleFilter={this.handleFilter.bind(this, 'right')}
9 years ago
handleClear={this.handleClear.bind(this, 'right')}
9 years ago
handleSelect={this.handleSelect.bind(this, 'right')}
handleSelectAll={this.handleSelectAll.bind(this, 'right')}
9 years ago
position="right"
9 years ago
render={this.props.render}
9 years ago
showSearch={showSearch}
9 years ago
searchPlaceholder={searchPlaceholder}
9 years ago
body={body}
footer={footer}
/>
9 years ago
</div>;
}
}
Transfer.defaultProps = {
prefixCls: 'ant-transfer',
dataSource: [],
9 years ago
render: noop,
targetKeys: [],
9 years ago
onChange: noop,
9 years ago
boxWidth: 160,
9 years ago
titles: ['源列表', '目的列表'],
operations: [],
9 years ago
showSearch: false,
searchPlaceholder: '请输入搜索内容',
9 years ago
body: noop,
9 years ago
footer: noop,
9 years ago
};
Transfer.propTypes = {
prefixCls: PropTypes.string,
dataSource: PropTypes.array,
9 years ago
render: PropTypes.func,
targetKeys: PropTypes.array,
onChange: PropTypes.func,
9 years ago
boxWidth: PropTypes.number,
9 years ago
titles: PropTypes.array,
operations: PropTypes.array,
9 years ago
showSearch: PropTypes.bool,
searchPlaceholder: PropTypes.string,
9 years ago
body: PropTypes.func,
footer: PropTypes.func,
9 years ago
};
Transfer.List = List;
Transfer.Operation = Operation;
Transfer.Search = Search;
9 years ago
export default Transfer;