ant-design/components/transfer/index.jsx

196 lines
5.2 KiB
React
Raw Normal View History

2015-11-25 23:17:06 +08:00
import React, { Component, PropTypes } from 'react';
import List from './list.jsx';
2015-11-26 16:07:11 +08:00
import Operation from './operation.jsx';
2015-11-25 23:17:06 +08:00
function noop() {
}
class Transfer extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: props.dataSource,
2015-11-26 16:07:11 +08:00
leftFilter: '',
rightFilter: '',
2015-11-25 23:17:06 +08:00
};
}
2015-11-26 11:18:37 +08:00
componentWillReceiveProps(nextProps) {
this.setState({
dataSource: nextProps.dataSource,
});
}
2015-11-25 23:17:06 +08:00
moveTo(direction) {
2015-11-26 16:07:11 +08:00
// TODO: 将新移动的元素置顶: 存index至state中
2015-11-25 23:17:06 +08:00
const { filterKey } = this.props;
let { dataSource } = this.state;
if ( direction === 'right' ) {
dataSource.forEach((data) => {
// 左边向右移动
if ( !data[filterKey] && data.checked ) {
data[filterKey] = true;
data.checked = false;
}
});
2015-11-26 16:07:11 +08:00
this.setState({
leftFilter: '',
dataSource: dataSource,
});
2015-11-25 23:17:06 +08:00
} else {
dataSource.forEach((data) => {
if ( data[filterKey] && data.checked ) {
data[filterKey] = false;
data.checked = false;
}
});
2015-11-26 16:07:11 +08:00
this.setState({
rightFilter: '',
dataSource: dataSource,
});
2015-11-25 23:17:06 +08:00
}
}
handleSelectAll(direction, globalCheckStatus) {
const { filterKey } = this.props;
2015-11-26 16:07:11 +08:00
const { dataSource, leftFilter, rightFilter } = this.state;
2015-11-25 23:17:06 +08:00
switch ( globalCheckStatus ) {
// 选中部分,则全选
case 'part':
case 'none':
dataSource.forEach((data)=> {
// data[filterKey] true 时,在右侧
2015-11-26 16:07:11 +08:00
if ( direction === 'right' && data[filterKey] && this.matchFilter(data.title, rightFilter)
|| direction === 'left' && !data[filterKey] && this.matchFilter(data.title, leftFilter)) {
2015-11-25 23:17:06 +08:00
data.checked = true;
}
});
break;
case 'all':
dataSource.forEach((data)=> {
2015-11-26 16:07:11 +08:00
if ( direction === 'right' && data[filterKey] && this.matchFilter(data.title, rightFilter)
|| direction === 'left' && !data[filterKey] && this.matchFilter(data.title, leftFilter)) {
2015-11-25 23:17:06 +08:00
data.checked = false;
}
});
break;
default:
break;
}
this.setState({
dataSource: dataSource,
});
}
2015-11-26 16:07:11 +08:00
handleFilter(direction, e) {
const filterText = e.target.value;
if ( direction === 'left') {
this.setState({
'leftFilter': filterText
});
} else {
this.setState({
'rightFilter': filterText
});
}
}
matchFilter(text, filterText) {
const regex = new RegExp(filterText);
return text.match(regex);
}
2015-11-25 23:17:06 +08:00
handleSelect(selectedItem, checked) {
const { dataSource } = this.state;
dataSource.forEach((data)=> {
if ( data.value === selectedItem.value ) {
data.checked = checked;
}
});
this.setState({
dataSource: dataSource,
});
}
render() {
2015-11-26 16:07:11 +08:00
const { prefixCls, leftConfig, rightConfig, filterKey, extraRender } = this.props;
const { dataSource, leftFilter, rightFilter } = this.state;
2015-11-25 23:17:06 +08:00
let leftDataSource = [];
let rightDataSource = [];
dataSource.map((item)=> {
2015-11-26 16:07:11 +08:00
// filter item
2015-11-25 23:17:06 +08:00
if ( item[filterKey] ) {
2015-11-26 16:07:11 +08:00
if ( this.matchFilter(item.title, rightFilter) ) {
rightDataSource.push(item);
}
2015-11-25 23:17:06 +08:00
} else {
2015-11-26 16:07:11 +08:00
if ( this.matchFilter(item.title, leftFilter) ) {
leftDataSource.push(item);
}
2015-11-25 23:17:06 +08:00
}
});
return <div className={prefixCls}>
2015-11-26 16:07:11 +08:00
<List style={{ width: '40%', display: 'inline-block'}} config={leftConfig} dataSource={leftDataSource}
filter={leftFilter}
handleFilter={this.handleFilter.bind(this, 'left')}
handleSelect={this.handleSelect.bind(this)}
handleSelectAll={this.handleSelectAll.bind(this, 'left')}
extraRender={extraRender}/>
<Operation rightArrowText={leftConfig.operationText} moveToRight={this.moveTo.bind(this, 'right')}
leftArrowText={rightConfig.operationText} moveToLeft={this.moveTo.bind(this, 'left')} />
<List style={{ width: '40%', display: 'inline-block'}} config={rightConfig} dataSource={rightDataSource}
filter={rightFilter}
handleFilter={this.handleFilter.bind(this, 'right')}
handleSelect={this.handleSelect.bind(this)}
handleSelectAll={this.handleSelectAll.bind(this, 'right')}
extraRender={extraRender} />
2015-11-25 23:17:06 +08:00
</div>;
}
}
// onChange-> do operation
// onSelect-> select action row
Transfer.defaultProps = {
prefixCls: 'ant-transfer',
2015-11-26 11:18:37 +08:00
dataSource: [],
2015-11-25 23:17:06 +08:00
dataIndex: 'title',
filterKey: 'chosen',
onChange: noop,
onSelect: noop,
leftConfig: {
title: '源列表',
operationText: '审核入库',
},
rightConfig: {
title: '目的列表',
operationText: '审核出库',
},
showSearch: false,
searchPlaceholder: '请输入搜索内容',
extraRender: noop,
2015-11-26 16:07:11 +08:00
footer: ()=> {
},
2015-11-25 23:17:06 +08:00
};
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;