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.

247 lines
6.2 KiB

9 years ago
import React, { Component, PropTypes } from 'react';
import List from './list.jsx';
import Operation from './operation.jsx';
9 years ago
function noop() {
}
class Transfer extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: props.dataSource,
leftFilter: '',
rightFilter: '',
9 years ago
};
}
componentWillReceiveProps(nextProps) {
this.setState({
dataSource: nextProps.dataSource,
});
}
9 years ago
checkDirection(direction) {
9 years ago
const { filterKey } = this.props;
9 years ago
let { dataSource } = this.state;
9 years ago
let result = false;
if ( direction === 'right' ) {
dataSource.forEach((data) => {
if ( !data[filterKey] && data.checked ) {
result = true;
}
});
} else {
dataSource.forEach((data) => {
if ( data[filterKey] && data.checked ) {
result = true;
}
});
}
return result;
9 years ago
}
9 years ago
moveTo(direction) {
const { filterKey } = this.props;
let { dataSource } = this.state;
9 years ago
// TODO: 验证是否可点
9 years ago
if ( direction === 'right' ) {
dataSource.forEach((data) => {
// 左边向右移动
if ( !data[filterKey] && data.checked ) {
data[filterKey] = true;
data.checked = false;
}
});
this.setState({
leftFilter: '',
dataSource: dataSource,
});
9 years ago
} else {
dataSource.forEach((data) => {
if ( data[filterKey] && data.checked ) {
data[filterKey] = false;
data.checked = false;
}
});
this.setState({
rightFilter: '',
dataSource: dataSource,
});
9 years ago
}
}
handleSelectAll(direction, globalCheckStatus) {
const { filterKey } = this.props;
const { dataSource, leftFilter, rightFilter } = this.state;
9 years ago
switch ( globalCheckStatus ) {
// 选中部分,则全选
case 'part':
case 'none':
dataSource.forEach((data)=> {
// data[filterKey] true 时,在右侧
if ( direction === 'right' && data[filterKey] && this.matchFilter(data.title, rightFilter)
|| direction === 'left' && !data[filterKey] && this.matchFilter(data.title, leftFilter)) {
9 years ago
data.checked = true;
}
});
break;
case 'all':
dataSource.forEach((data)=> {
if ( direction === 'right' && data[filterKey] && this.matchFilter(data.title, rightFilter)
|| direction === 'left' && !data[filterKey] && this.matchFilter(data.title, leftFilter)) {
9 years ago
data.checked = false;
}
});
break;
default:
break;
}
this.setState({
dataSource: dataSource,
});
}
handleFilter(direction, e) {
const filterText = e.target.value;
if ( direction === 'left') {
this.setState({
9 years ago
'leftFilter': filterText,
});
} else {
this.setState({
9 years ago
'rightFilter': filterText,
});
}
}
9 years ago
handleClear(direction) {
if ( direction === 'left') {
this.setState({
'leftFilter': '',
});
} else {
this.setState({
'rightFilter': '',
});
}
}
matchFilter(text, filterText) {
const regex = new RegExp(filterText);
return text.match(regex);
}
9 years ago
handleSelect(selectedItem, checked) {
const { dataSource } = this.state;
dataSource.forEach((data)=> {
if ( data.value === selectedItem.value ) {
data.checked = checked;
}
});
this.setState({
dataSource: dataSource,
});
}
render() {
9 years ago
const { prefixCls, leftConfig, rightConfig, filterKey, showSearch, header, body, footer } = this.props;
const { dataSource, leftFilter, rightFilter } = this.state;
9 years ago
let leftDataSource = [];
let rightDataSource = [];
9 years ago
let leftActive = this.checkDirection('left');
let rightActive = this.checkDirection('right');
9 years ago
9 years ago
dataSource.map((item)=> {
// filter item
9 years ago
if ( item[filterKey] ) {
if ( this.matchFilter(item.title, rightFilter) ) {
rightDataSource.push(item);
}
9 years ago
} else {
if ( this.matchFilter(item.title, leftFilter) ) {
leftDataSource.push(item);
}
9 years ago
}
});
return <div className={prefixCls}>
9 years ago
<List config={leftConfig}
dataSource={leftDataSource}
filter={leftFilter}
handleFilter={this.handleFilter.bind(this, 'left')}
9 years ago
handleClear={this.handleClear.bind(this, 'left')}
handleSelect={this.handleSelect.bind(this)}
handleSelectAll={this.handleSelectAll.bind(this, 'left')}
9 years ago
position="left"
9 years ago
showSearch={showSearch}
header={header}
body={body}
footer={footer}
/>
9 years ago
<Operation rightActive={rightActive} rightArrowText={leftConfig.operationText} moveToRight={this.moveTo.bind(this, 'right')}
leftActive={leftActive} leftArrowText={rightConfig.operationText} moveToLeft={this.moveTo.bind(this, 'left')} />
<List config={rightConfig}
dataSource={rightDataSource}
filter={rightFilter}
handleFilter={this.handleFilter.bind(this, 'right')}
9 years ago
handleClear={this.handleClear.bind(this, 'right')}
handleSelect={this.handleSelect.bind(this)}
handleSelectAll={this.handleSelectAll.bind(this, 'right')}
9 years ago
position="right"
9 years ago
showSearch={showSearch}
header={header}
body={body}
footer={footer}
/>
9 years ago
</div>;
}
}
// onChange-> do operation
// onSelect-> select action row
Transfer.defaultProps = {
prefixCls: 'ant-transfer',
dataSource: [],
9 years ago
dataIndex: 'title',
filterKey: 'chosen',
onChange: noop,
onSelect: noop,
leftConfig: {
title: '源列表',
operationText: '审核入库',
},
rightConfig: {
title: '目的列表',
operationText: '审核出库',
},
showSearch: false,
searchPlaceholder: '请输入搜索内容',
9 years ago
header: noop,
footer: noop,
body: noop,
9 years ago
};
Transfer.propTypes = {
prefixCls: PropTypes.string,
dataSource: PropTypes.array,
showSearch: PropTypes.bool,
searchPlaceholder: PropTypes.string,
operationText: PropTypes.string,
leftTitle: PropTypes.string,
rightTitle: PropTypes.string,
onChange: PropTypes.func,
extraRender: PropTypes.func,
};
export default Transfer;