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
3.8 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(this.getGlobalCheckStatus());
}
9 years ago
handleSelect(selectedItem) {
this.props.handleSelect(selectedItem, !selectedItem.checked);
9 years ago
}
handleFilter(e) {
this.props.handleFilter(e);
}
9 years ago
getGlobalCheckStatus() {
let { dataSource } = this.props;
let globalCheckStatus;
let selectedRowLength = 0;
dataSource.forEach((data)=> {
if ( data.checked ) {
selectedRowLength++;
}
});
if ( selectedRowLength > 0 ) {
if ( selectedRowLength < dataSource.length ) {
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>);
}
render() {
9 years ago
const { prefixCls, config, header, footer, dataSource, filter, body } = this.props;
9 years ago
let globalCheckStatus = this.getGlobalCheckStatus();
9 years ago
// Custom Layout
9 years ago
const headerDom = header({...this.props, globalCheckStatus});
const footerDom = footer({...this.props, globalCheckStatus});
9 years ago
const bodyDom = body({...this.props, globalCheckStatus});
9 years ago
9 years ago
return (<div className={prefixCls} {...this.props}>
9 years ago
{ headerDom ? <div className={`${prefixCls}-header`}>
{ headerDom }
</div> : <div className={`${prefixCls}-header`}>
9 years ago
{this.renderCheckbox({
prefixCls: 'ant-tree',
checked: globalCheckStatus === 'all',
checkPart: globalCheckStatus === 'part',
checkable: <span className={`ant-tree-checkbox-inner`}></span>
9 years ago
})} {dataSource.length}
<span className={`${prefixCls}-header-title`}>{config.title}</span>
9 years ago
</div> }
9 years ago
{ bodyDom ? bodyDom : <div className={`${prefixCls}-body`}>
<div className={`${prefixCls}-body-search-wrapper`}>
<Search className={`${prefixCls}-body-search-bar`} onChange={this.handleFilter.bind(this)} value={filter} />
</div>
<ul className="">
{dataSource.map((item)=> {
return <li onClick={this.handleSelect.bind(this, item)}>
<Checkbox checked={item.checked} />
{ item.title }
</li>;})}
</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
defaultDataSource: [],
handleFilter: noop,
handleSelect: noop,
9 years ago
onChange: noop,
9 years ago
//advanced
header: noop,
footer: noop,
9 years ago
body: noop,
9 years ago
};
TransferList.propTypes = {
prefixCls: PropTypes.string,
dataSource: PropTypes.array,
9 years ago
footer: PropTypes.func,
9 years ago
searchPlaceholder: PropTypes.string,
handleFilter: PropTypes.func,
9 years ago
handleSelect: PropTypes.func,
handleSelectAll: PropTypes.func,
config: PropTypes.object,
};
export default TransferList;