Browse Source

Fix Transfer search and check all behavious, close #2396

pull/2452/head
afc163 8 years ago
parent
commit
e7f6f4b869
  1. 37
      components/transfer/index.jsx
  2. 44
      components/transfer/list.jsx

37
components/transfer/index.jsx

@ -130,40 +130,16 @@ export default class Transfer extends React.Component {
moveToLeft = () => this.moveTo('left') moveToLeft = () => this.moveTo('left')
moveToRight = () => this.moveTo('right') moveToRight = () => this.moveTo('right')
getGlobalCheckStatus(direction) { handleSelectAll = (direction, filteredDataSource, checkAll) => {
const { leftDataSource, rightDataSource } = this.splitDataSource(this.props); const holder = checkAll ? [] : filteredDataSource.map(item => item.key);
const { leftCheckedKeys, rightCheckedKeys } = this.state;
const dataSource = direction === 'left' ? leftDataSource : rightDataSource;
const checkedKeys = direction === 'left' ? leftCheckedKeys : rightCheckedKeys;
let globalCheckStatus;
if (checkedKeys.length > 0) {
if (checkedKeys.length < dataSource.length) {
globalCheckStatus = 'part';
} else {
globalCheckStatus = 'all';
}
} else {
globalCheckStatus = 'none';
}
return globalCheckStatus;
}
handleSelectAll = (direction) => {
const { leftDataSource, rightDataSource } = this.splitDataSource(this.props);
const dataSource = direction === 'left' ? leftDataSource : rightDataSource;
const checkStatus = this.getGlobalCheckStatus(direction);
const holder = (checkStatus === 'all') ? [] : dataSource.map(item => item.key);
this.setState({ this.setState({
[`${direction}CheckedKeys`]: holder, [`${direction}CheckedKeys`]: holder,
}); });
} }
handleLeftSelectAll = () => this.handleSelectAll('left') handleLeftSelectAll = (...args) => this.handleSelectAll('left', ...args)
handleRightSelectAll = () => this.handleSelectAll('right') handleRightSelectAll = (...args) => this.handleSelectAll('right', ...args)
handleFilter = (direction, e) => { handleFilter = (direction, e) => {
this.setState({ this.setState({
@ -219,9 +195,6 @@ export default class Transfer extends React.Component {
const leftActive = rightCheckedKeys.length > 0; const leftActive = rightCheckedKeys.length > 0;
const rightActive = leftCheckedKeys.length > 0; const rightActive = leftCheckedKeys.length > 0;
const leftCheckStatus = this.getGlobalCheckStatus('left');
const rightCheckStatus = this.getGlobalCheckStatus('right');
const cls = classNames({ const cls = classNames({
[className]: !!className, [className]: !!className,
[prefixCls]: true, [prefixCls]: true,
@ -234,7 +207,6 @@ export default class Transfer extends React.Component {
filter={leftFilter} filter={leftFilter}
style={listStyle} style={listStyle}
checkedKeys={leftCheckedKeys} checkedKeys={leftCheckedKeys}
checkStatus={leftCheckStatus}
handleFilter={this.handleLeftFilter} handleFilter={this.handleLeftFilter}
handleClear={this.handleLeftClear} handleClear={this.handleLeftClear}
handleSelect={this.handleLeftSelect} handleSelect={this.handleLeftSelect}
@ -260,7 +232,6 @@ export default class Transfer extends React.Component {
filter={rightFilter} filter={rightFilter}
style={listStyle} style={listStyle}
checkedKeys={rightCheckedKeys} checkedKeys={rightCheckedKeys}
checkStatus={rightCheckStatus}
handleFilter={this.handleRightFilter} handleFilter={this.handleRightFilter}
handleClear={this.handleRightClear} handleClear={this.handleRightClear}
handleSelect={this.handleRightSelect} handleSelect={this.handleRightSelect}

44
components/transfer/list.jsx

@ -71,8 +71,14 @@ export default class TransferList extends React.Component {
return PureRenderMixin.shouldComponentUpdate.apply(this, args); return PureRenderMixin.shouldComponentUpdate.apply(this, args);
} }
handleSelectAll = () => { getCheckStatus(filteredDataSource) {
this.props.handleSelectAll(); const { checkedKeys } = this.props;
if (checkedKeys.length === 0) {
return 'none';
} else if (filteredDataSource.every(item => checkedKeys.indexOf(item.key) >= 0)) {
return 'all';
}
return 'part';
} }
handleSelect = (selectedItem) => { handleSelect = (selectedItem) => {
@ -89,24 +95,23 @@ export default class TransferList extends React.Component {
this.props.handleClear(); this.props.handleClear();
} }
renderCheckbox(props) { renderCheckbox({ prefixCls, filteredDataSource, checked, checkPart, disabled, checkable }) {
const { prefixCls } = props; const checkAll = (!checkPart) && checked;
const checkboxCls = classNames({ const checkboxCls = classNames({
[`${prefixCls}-checkbox`]: true, [`${prefixCls}-checkbox`]: true,
[`${prefixCls}-checkbox-indeterminate`]: props.checkPart, [`${prefixCls}-checkbox-indeterminate`]: checkPart,
[`${prefixCls}-checkbox-checked`]: (!props.checkPart) && props.checked, [`${prefixCls}-checkbox-checked`]: checkAll,
[`${prefixCls}-checkbox-disabled`]: !!props.disabled, [`${prefixCls}-checkbox-disabled`]: disabled,
}); });
let customEle = null;
if (typeof props.checkable !== 'boolean') {
customEle = props.checkable;
}
return ( return (
<span ref="checkbox" <span
ref="checkbox"
className={checkboxCls} className={checkboxCls}
onClick={(!props.disabled) && this.handleSelectAll} onClick={() => this.props.handleSelectAll(filteredDataSource, checkAll)}
> >
{customEle} {(typeof checkable !== 'boolean') ? checkable : null}
</span> </span>
); );
} }
@ -117,7 +122,7 @@ export default class TransferList extends React.Component {
render() { render() {
const { prefixCls, dataSource, titleText, filter, checkedKeys, const { prefixCls, dataSource, titleText, filter, checkedKeys,
checkStatus, body, footer, showSearch, render, style } = this.props; body, footer, showSearch, render, style } = this.props;
let { searchPlaceholder, notFoundContent } = this.props; let { searchPlaceholder, notFoundContent } = this.props;
@ -130,7 +135,9 @@ export default class TransferList extends React.Component {
[`${prefixCls}-with-footer`]: !!footerDom, [`${prefixCls}-with-footer`]: !!footerDom,
}); });
const showItems = dataSource.map((item) => { const filteredDataSource = [];
const showItems = dataSource.map(item => {
const renderResult = render(item); const renderResult = render(item);
let renderedText; let renderedText;
let renderedEl; let renderedEl;
@ -147,6 +154,8 @@ export default class TransferList extends React.Component {
return null; return null;
} }
filteredDataSource.push(item);
return ( return (
<li onClick={() => this.handleSelect(item)} key={item.key} title={renderedText}> <li onClick={() => this.handleSelect(item)} key={item.key} title={renderedText}>
<Checkbox checked={checkedKeys.some(key => key === item.key)} /> <Checkbox checked={checkedKeys.some(key => key === item.key)} />
@ -167,6 +176,8 @@ export default class TransferList extends React.Component {
|| this.context.antLocale.Transfer.notFoundContent; || this.context.antLocale.Transfer.notFoundContent;
} }
const checkStatus = this.getCheckStatus(filteredDataSource);
return ( return (
<div className={listCls} style={style}> <div className={listCls} style={style}>
<div className={`${prefixCls}-header`}> <div className={`${prefixCls}-header`}>
@ -175,6 +186,7 @@ export default class TransferList extends React.Component {
checked: checkStatus === 'all', checked: checkStatus === 'all',
checkPart: checkStatus === 'part', checkPart: checkStatus === 'part',
checkable: <span className={'ant-transfer-checkbox-inner'}></span>, checkable: <span className={'ant-transfer-checkbox-inner'}></span>,
filteredDataSource,
})} })}
<span className={`${prefixCls}-header-selected`}> <span className={`${prefixCls}-header-selected`}>
<span> <span>

Loading…
Cancel
Save