ant-design/components/transfer/list.jsx

127 lines
3.3 KiB
React
Raw Normal View History

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() {
this.props.handleSelectAll(this.getGlobalCheckStatus());
}
handleSelect(selectedItem, e) {
this.props.handleSelect(selectedItem, e.target.checked);
}
2015-11-26 16:07:11 +08:00
handleFilter(e) {
this.props.handleFilter(e);
}
2015-11-25 23:17:06 +08:00
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() {
2015-11-26 16:07:11 +08:00
const { prefixCls, config, footer, extraRender, dataSource, filter } = this.props;
2015-11-25 23:17:06 +08:00
let globalCheckStatus = this.getGlobalCheckStatus();
return (<div className={prefixCls} {...this.props}>
<div className={`${prefixCls}-header`}>
{this.renderCheckbox({
prefixCls: 'ant-tree',
checked: globalCheckStatus === 'all',
checkPart: globalCheckStatus === 'part',
checkable: <span className={`ant-tree-checkbox-inner`}></span>
2015-11-26 11:18:37 +08:00
})} {dataSource.length} {config.title}
2015-11-25 23:17:06 +08:00
</div>
<div className={`${prefixCls}-body`}>
2015-11-26 16:07:11 +08:00
<Search onChange={this.handleFilter.bind(this)} value={filter} />
2015-11-25 23:17:06 +08:00
<ul>
{dataSource.map((item)=> {
return <li>
<label>
<Checkbox checked={item.checked} onChange={this.handleSelect.bind(this, item)} />
{ item.title }
</label>
2015-11-26 16:07:11 +08:00
<div className="extra-holder">
{extraRender(item)}
</div>
2015-11-25 23:17:06 +08:00
</li>;})}
</ul>
</div>
2015-11-26 16:07:11 +08:00
{ footer ? <div className={`${prefixCls}-footer`}>
{ footer }
</div> : null }
2015-11-25 23:17:06 +08:00
</div>);
}
}
TransferList.defaultProps = {
prefixCls: 'ant-transfer-list',
dataSource: [],
onChange: noop,
2015-11-26 16:07:11 +08:00
footer: [],
2015-11-25 23:17:06 +08:00
extraRender: noop,
};
TransferList.propTypes = {
prefixCls: PropTypes.string,
dataSource: PropTypes.array,
footer: 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,
config: PropTypes.object,
};
export default TransferList;