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.2 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
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
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) {
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);
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 + 'CheckedKeys']: holder,
9 years ago
});
}
handleFilter(direction, e) {
9 years ago
this.setState({
// deselect all
9 years ago
[direction + 'CheckedKeys']: [],
// add filter
9 years ago
[direction + 'Filter']: e.target.value,