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.

250 lines
7.1 KiB

9 years ago
import React, { Component, PropTypes } from 'react';
import List from './list';
import Operation from './operation';
import Search from './search';
9 years ago
import classNames from 'classnames';
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
if (targetKeys.length > 0) {
9 years ago
targetKeys.forEach((targetKey) => {
rightDataSource.push(leftDataSource.filter((data, index) => {
if (data.key === targetKey) {
9 years ago
leftDataSource.splice(index, 1);
return true;
}
})[0]);
9 years ago
});
}
9 years ago
return {
leftDataSource,
rightDataSource,
9 years ago
};
9 years ago
}
9 years ago
moveTo(direction) {
9 years ago
const { targetKeys } = this.props;
const { leftCheckedKeys, rightCheckedKeys } = this.state;
const moveKeys = direction === 'right' ? leftCheckedKeys : rightCheckedKeys;
9 years ago
// move items to target box
const newTargetKeys = direction === 'right'
? moveKeys.concat(targetKeys)
: targetKeys.filter(targetKey => !moveKeys.some(checkedKey => targetKey === checkedKey));
9 years ago
// empty checked keys
this.setState({
[direction === 'right' ? 'leftCheckedKeys' : 'rightCheckedKeys']: [],
9 years ago
});
this.props.onChange(newTargetKeys, direction, moveKeys);
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) {
return dataSource.filter(item => {
9 years ago
const itemText = this.props.render(item);
return this.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);
const holder = (checkStatus === 'all') ? [] :
this.filterDataSource(dataSource, filter).map(item => item.key);
9 years ago
this.setState({
[`${direction}CheckedKeys`]: holder,
9 years ago
});
}
handleFilter(direction, e) {
9 years ago
this.setState({
// deselect all
[`${direction}CheckedKeys`]: [],
// add filter
[`${direction}Filter`]: e.target.value,
9 years ago
});
}
9 years ago
handleClear(direction) {
9 years ago
this.setState({
[`${direction}Filter`]: '',
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;
let index;
holder.forEach((key, i) => {
if (key === selectedItem.key) {
index = i;
}
});
if (index > -1) {
9 years ago
holder.splice(index, 1);
}
if (checked) {
9 years ago
holder.push(selectedItem.key);
}
9 years ago
this.setState({
[`${direction}CheckedKeys`]: holder,
9 years ago
});
}
render() {
9 years ago
const {
prefixCls, titles, operations, showSearch,
searchPlaceholder, body, footer, listStyle, className,
} = 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
9 years ago
const cls = classNames({
[className]: !!className,
prefixCls: true,
});
return (
<div className={cls}>
<List titleText={titles[0]}
dataSource={leftDataSource}
filter={leftFilter}
style={listStyle}
checkedKeys={leftCheckedKeys}
checkStatus={leftCheckStatus}
handleFilter={this.handleFilter.bind(this, 'left')}
handleClear={this.handleClear.bind(this, 'left')}
handleSelect={this.handleSelect.bind(this, 'left')}
handleSelectAll={this.handleSelectAll.bind(this, 'left')}
position="left"
render={this.props.render}
showSearch={showSearch}
searchPlaceholder={searchPlaceholder}
body={body}
footer={footer}
prefixCls={`${prefixCls}-list`}/>
<Operation rightActive={rightActive}
rightArrowText={operations[0]}
moveToRight={this.moveTo.bind(this, 'right')}
leftActive={leftActive}
leftArrowText={operations[1]}
moveToLeft={this.moveTo.bind(this, 'left')}
className={`${prefixCls}-operation`}/>
<List titleText={titles[1]}
dataSource={rightDataSource}
filter={rightFilter}
style={listStyle}
checkedKeys={rightCheckedKeys}
checkStatus={rightCheckStatus}
handleFilter={this.handleFilter.bind(this, 'right')}
handleClear={this.handleClear.bind(this, 'right')}
handleSelect={this.handleSelect.bind(this, 'right')}
handleSelectAll={this.handleSelectAll.bind(this, 'right')}
position="right"
render={this.props.render}
showSearch={showSearch}
searchPlaceholder={searchPlaceholder}
body={body}
footer={footer}
prefixCls={`${prefixCls}-list`}/>
</div>
);
9 years ago
}
}
Transfer.defaultProps = {
prefixCls: 'ant-transfer',
dataSource: [],
9 years ago
render: noop,
targetKeys: [],
9 years ago
onChange: noop,
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,
height: PropTypes.number,
9 years ago
listStyle: PropTypes.object,
className: PropTypes.string,
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;