2015-11-25 23:17:06 +08:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
import Checkbox from '../checkbox';
|
2015-11-26 16:07:11 +08:00
|
|
|
import Search from './search.jsx';
|
2015-11-25 23:17:06 +08:00
|
|
|
import {classSet} from 'rc-util';
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
class TransferList extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelectALl() {
|
2015-12-21 15:29:02 +08:00
|
|
|
this.props.handleSelectAll(this.getGlobalCheckStatus(), this.filter);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 23:02:49 +08:00
|
|
|
handleSelect(selectedItem) {
|
2015-12-21 15:29:02 +08:00
|
|
|
const { checkedKeys } = this.props;
|
|
|
|
const result = checkedKeys.some((key) => key === selectedItem.key);
|
|
|
|
this.props.handleSelect(selectedItem, !result);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 16:07:11 +08:00
|
|
|
handleFilter(e) {
|
|
|
|
this.props.handleFilter(e);
|
|
|
|
}
|
|
|
|
|
2015-12-17 16:08:16 +08:00
|
|
|
handleClear() {
|
|
|
|
this.props.handleClear();
|
|
|
|
}
|
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
getGlobalCheckStatus() {
|
2015-12-21 15:29:02 +08:00
|
|
|
const { dataSource, checkedKeys } = this.props;
|
2015-11-25 23:17:06 +08:00
|
|
|
|
|
|
|
let globalCheckStatus;
|
|
|
|
|
2015-12-21 15:29:02 +08:00
|
|
|
if ( checkedKeys.length > 0 ) {
|
|
|
|
if ( checkedKeys.length < dataSource.length ) {
|
2015-11-25 23:17:06 +08:00
|
|
|
globalCheckStatus = 'part';
|
|
|
|
} else {
|
|
|
|
globalCheckStatus = 'all';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
globalCheckStatus = 'none';
|
|
|
|
}
|
|
|
|
return globalCheckStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCheckbox(props) {
|
|
|
|
const { prefixCls } = props;
|
|
|
|
const checkboxCls = {
|
|
|
|
[`${prefixCls}-checkbox`]: true,
|
|
|
|
};
|
|
|
|
if (props.checkPart) {
|
|
|
|
checkboxCls[`${prefixCls}-checkbox-indeterminate`] = true;
|
|
|
|
} else if (props.checked) {
|
|
|
|
checkboxCls[`${prefixCls}-checkbox-checked`] = true;
|
|
|
|
}
|
|
|
|
let customEle = null;
|
|
|
|
if (typeof props.checkable !== 'boolean') {
|
|
|
|
customEle = props.checkable;
|
|
|
|
}
|
|
|
|
if (props.disabled) {
|
|
|
|
checkboxCls[`${prefixCls}-checkbox-disabled`] = true;
|
|
|
|
return <span ref="checkbox" className={classSet(checkboxCls)}>{customEle}</span>;
|
|
|
|
}
|
|
|
|
return (<span ref="checkbox" className={classSet(checkboxCls)} onClick={this.handleSelectALl.bind(this)}>{customEle}</span>);
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:29:02 +08:00
|
|
|
matchFilter(text, filterText) {
|
|
|
|
const regex = new RegExp(filterText);
|
|
|
|
return text.match(regex);
|
|
|
|
}
|
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
render() {
|
2015-12-21 15:29:02 +08:00
|
|
|
let self = this;
|
|
|
|
const { prefixCls, dataSource, title, filter, checkedKeys, body, footer, showSearch } = this.props;
|
2015-11-25 23:17:06 +08:00
|
|
|
|
|
|
|
let globalCheckStatus = this.getGlobalCheckStatus();
|
|
|
|
|
2015-12-16 23:02:49 +08:00
|
|
|
// Custom Layout
|
2015-12-15 17:08:34 +08:00
|
|
|
const footerDom = footer({...this.props, globalCheckStatus});
|
2015-12-16 23:02:49 +08:00
|
|
|
const bodyDom = body({...this.props, globalCheckStatus});
|
2015-12-15 17:08:34 +08:00
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
return (<div className={prefixCls} {...this.props}>
|
2015-12-21 15:29:02 +08:00
|
|
|
<div className={`${prefixCls}-header`}>
|
2015-11-25 23:17:06 +08:00
|
|
|
{this.renderCheckbox({
|
|
|
|
prefixCls: 'ant-tree',
|
|
|
|
checked: globalCheckStatus === 'all',
|
|
|
|
checkPart: globalCheckStatus === 'part',
|
|
|
|
checkable: <span className={`ant-tree-checkbox-inner`}></span>
|
2015-12-21 15:29:02 +08:00
|
|
|
})} { (checkedKeys.length > 0 ? checkedKeys.length + '/' : '') + dataSource.length} 条
|
|
|
|
<span className={`${prefixCls}-header-title`}>{title}</span>
|
|
|
|
</div>
|
|
|
|
{ bodyDom ? bodyDom :
|
|
|
|
<div className={ showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`}>
|
|
|
|
{ showSearch ? <div className={`${prefixCls}-body-search-wrapper`}>
|
2015-12-17 16:08:16 +08:00
|
|
|
<Search className={`${prefixCls}-body-search-bar`} onChange={this.handleFilter.bind(this)} handleClear={this.handleClear.bind(this)} value={filter} />
|
2015-12-21 15:29:02 +08:00
|
|
|
</div> : null }
|
|
|
|
<ul>
|
|
|
|
{ dataSource.length > 0 ?
|
|
|
|
dataSource.map((item)=> {
|
|
|
|
// apply filter
|
|
|
|
const itemText = self.props.render(item);
|
|
|
|
const filterResult = self.matchFilter(itemText, filter);
|
|
|
|
|
|
|
|
if ( filterResult ) {
|
|
|
|
return <li onClick={this.handleSelect.bind(this, item)}>
|
|
|
|
<Checkbox checked={checkedKeys.some((key) => key === item.key)} />
|
|
|
|
{ self.props.render(item) }
|
|
|
|
</li>;
|
|
|
|
}
|
|
|
|
}) : <div className={`${prefixCls}-body-not-found`}>
|
|
|
|
Not Found
|
|
|
|
</div>
|
|
|
|
}
|
2015-12-16 23:02:49 +08:00
|
|
|
</ul>
|
|
|
|
</div>}
|
2015-12-15 17:08:34 +08:00
|
|
|
{ footerDom ? <div className={`${prefixCls}-footer`}>
|
|
|
|
{ footerDom }
|
2015-11-26 16:07:11 +08:00
|
|
|
</div> : null }
|
2015-11-25 23:17:06 +08:00
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TransferList.defaultProps = {
|
|
|
|
prefixCls: 'ant-transfer-list',
|
|
|
|
dataSource: [],
|
2015-12-21 15:29:02 +08:00
|
|
|
showSearch: false,
|
|
|
|
searchPlaceholder: '',
|
2015-12-15 17:08:34 +08:00
|
|
|
handleFilter: noop,
|
|
|
|
handleSelect: noop,
|
2015-12-21 15:29:02 +08:00
|
|
|
handleSelectAll: noop,
|
|
|
|
render: noop,
|
2015-12-15 17:08:34 +08:00
|
|
|
//advanced
|
|
|
|
footer: noop,
|
2015-12-16 23:02:49 +08:00
|
|
|
body: noop,
|
2015-11-25 23:17:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TransferList.propTypes = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
dataSource: PropTypes.array,
|
|
|
|
searchPlaceholder: PropTypes.string,
|
2015-11-26 16:07:11 +08:00
|
|
|
handleFilter: PropTypes.func,
|
2015-11-25 23:17:06 +08:00
|
|
|
handleSelect: PropTypes.func,
|
|
|
|
handleSelectAll: PropTypes.func,
|
2015-12-21 15:29:02 +08:00
|
|
|
body: PropTypes.func,
|
|
|
|
footer: PropTypes.func,
|
2015-11-25 23:17:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default TransferList;
|