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.

137 lines
4.1 KiB

9 years ago
import React, { Component, PropTypes } from 'react';
import Checkbox from '../checkbox';
import Search from './search.jsx';
9 years ago
import {classSet} from 'rc-util';
function noop() {
}
class TransferList extends Component {
constructor(props) {
super(props);
}
handleSelectALl() {
this.props.handleSelectAll();
9 years ago
}
9 years ago
handleSelect(selectedItem) {
9 years ago
const { checkedKeys } = this.props;
const result = checkedKeys.some((key) => key === selectedItem.key);
this.props.handleSelect(selectedItem, !result);
9 years ago
}
handleFilter(e) {
this.props.handleFilter(e);
}
9 years ago
handleClear() {
this.props.handleClear();
}
9 years ago
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>);
}
9 years ago
matchFilter(text, filterText) {
const regex = new RegExp(filterText);
return text.match(regex);
}
9 years ago
render() {
9 years ago
let self = this;
9 years ago
const { prefixCls, dataSource, title, filter, width, checkedKeys, checkStatus, body, footer, showSearch } = this.props;
9 years ago
9 years ago
// Custom Layout
const footerDom = footer({...this.props});
const bodyDom = body({...this.props});
9 years ago
9 years ago
return (<div className={prefixCls} {...this.props} style={{width: width}}>
9 years ago
<div className={`${prefixCls}-header`}>
9 years ago
{this.renderCheckbox({
prefixCls: 'ant-tree',
checked: checkStatus === 'all',
checkPart: checkStatus === 'part',
9 years ago
checkable: <span className={`ant-tree-checkbox-inner`}></span>
9 years ago
})} { (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`}>
9 years ago
<Search className={`${prefixCls}-body-search-bar`} onChange={this.handleFilter.bind(this)} handleClear={this.handleClear.bind(this)} value={filter} />
9 years ago
</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)} key={item.key}>
9 years ago
<Checkbox checked={checkedKeys.some((key) => key === item.key)} />
{ self.props.render(item) }
</li>;
}
}) : <div className={`${prefixCls}-body-not-found`}>
Not Found
</div>
}
9 years ago
</ul>
</div>}
9 years ago
{ footerDom ? <div className={`${prefixCls}-footer`}>
{ footerDom }
</div> : null }
9 years ago
</div>);
}
}
TransferList.defaultProps = {
prefixCls: 'ant-transfer-list',
dataSource: [],
9 years ago
showSearch: false,
searchPlaceholder: '',
9 years ago
width: 160,
9 years ago
handleFilter: noop,
handleSelect: noop,
9 years ago
handleSelectAll: noop,
render: noop,
9 years ago
//advanced
9 years ago
body: noop,
footer: noop,
9 years ago
};
TransferList.propTypes = {
prefixCls: PropTypes.string,
dataSource: PropTypes.array,
showSearch: PropTypes.bool,
9 years ago
searchPlaceholder: PropTypes.string,
9 years ago
width: PropTypes.number,
handleFilter: PropTypes.func,
9 years ago
handleSelect: PropTypes.func,
handleSelectAll: PropTypes.func,
render: PropTypes.func,
9 years ago
body: PropTypes.func,
footer: PropTypes.func,
9 years ago
};
export default TransferList;