2019-01-12 11:33:27 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { isValidElement, initDefaultProps } from '../_util/props-util';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import Search from './search';
|
|
|
|
import Item from './item';
|
|
|
|
import triggerEvent from '../_util/triggerEvent';
|
|
|
|
import addEventListener from '../_util/Dom/addEventListener';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function noop() {}
|
2018-04-07 10:32:13 +08:00
|
|
|
|
|
|
|
const TransferItem = {
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
title: PropTypes.string.isRequired,
|
2018-04-07 00:20:45 +08:00
|
|
|
description: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function isRenderResultPlainObject(result) {
|
|
|
|
return (
|
|
|
|
result &&
|
|
|
|
!isValidElement(result) &&
|
2018-04-07 00:20:45 +08:00
|
|
|
Object.prototype.toString.call(result) === '[object Object]'
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const TransferListProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
titleText: PropTypes.string,
|
2018-04-07 10:32:13 +08:00
|
|
|
dataSource: PropTypes.arrayOf(PropTypes.shape(TransferItem).loose),
|
2018-04-07 00:20:45 +08:00
|
|
|
filter: PropTypes.string,
|
|
|
|
filterOption: PropTypes.func,
|
|
|
|
checkedKeys: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
handleFilter: PropTypes.func,
|
|
|
|
handleSelect: PropTypes.func,
|
|
|
|
handleSelectAll: PropTypes.func,
|
|
|
|
handleClear: PropTypes.func,
|
|
|
|
renderItem: PropTypes.func,
|
|
|
|
showSearch: PropTypes.bool,
|
|
|
|
searchPlaceholder: PropTypes.string,
|
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
itemUnit: PropTypes.string,
|
|
|
|
itemsUnit: PropTypes.string,
|
|
|
|
body: PropTypes.any,
|
|
|
|
footer: PropTypes.any,
|
2019-01-12 11:33:27 +08:00
|
|
|
lazy: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled: PropTypes.bool,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'TransferList',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: initDefaultProps(TransferListProps, {
|
|
|
|
dataSource: [],
|
|
|
|
titleText: '',
|
|
|
|
showSearch: false,
|
|
|
|
renderItem: noop,
|
|
|
|
lazy: {},
|
|
|
|
}),
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.timer = null;
|
|
|
|
this.triggerScrollTimer = null;
|
2018-04-07 00:20:45 +08:00
|
|
|
return {
|
|
|
|
mounted: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.timer = setTimeout(() => {
|
2018-04-07 00:20:45 +08:00
|
|
|
this.setState({
|
|
|
|
mounted: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
}, 0);
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.$refs.listContentWrapper) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const listContentWrapperDom = this.$refs.listContentWrapper.$el;
|
|
|
|
this.scrollEvent = addEventListener(listContentWrapperDom, 'scroll', this.handleScroll);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
clearTimeout(this.triggerScrollTimer);
|
2018-09-05 21:28:54 +08:00
|
|
|
if (this.scrollEvent) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.scrollEvent.remove();
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.scrollEvent) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.scrollEvent.remove();
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
if (this.$refs.listContentWrapper) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const listContentWrapperDom = this.$refs.listContentWrapper.$el;
|
|
|
|
this.scrollEvent = addEventListener(listContentWrapperDom, 'scroll', this.handleScroll);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handleScroll(e) {
|
|
|
|
this.$emit('scroll', e);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
getCheckStatus(filteredDataSource) {
|
|
|
|
const { checkedKeys } = this.$props;
|
2018-04-07 00:20:45 +08:00
|
|
|
if (checkedKeys.length === 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 'none';
|
2018-04-07 00:20:45 +08:00
|
|
|
} else if (filteredDataSource.every(item => checkedKeys.indexOf(item.key) >= 0)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 'all';
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return 'part';
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
_handleSelect(selectedItem) {
|
|
|
|
const { checkedKeys } = this.$props;
|
|
|
|
const result = checkedKeys.some(key => key === selectedItem.key);
|
|
|
|
this.handleSelect(selectedItem, !result);
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
_handleFilter(e) {
|
|
|
|
this.handleFilter(e);
|
2018-04-07 00:20:45 +08:00
|
|
|
if (!e.target.value) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
|
|
|
// Manually trigger scroll event for lazy search bug
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5631
|
2018-09-05 21:28:54 +08:00
|
|
|
this.triggerScrollTimer = setTimeout(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const transferNode = this.$el;
|
|
|
|
const listNode = transferNode.querySelectorAll('.ant-transfer-list-content')[0];
|
2018-04-07 00:20:45 +08:00
|
|
|
if (listNode) {
|
2019-01-12 11:33:27 +08:00
|
|
|
triggerEvent(listNode, 'scroll');
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
}, 0);
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
_handleClear(e) {
|
|
|
|
this.handleClear(e);
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
matchFilter(text, item) {
|
|
|
|
const { filter, filterOption } = this.$props;
|
2018-04-07 00:20:45 +08:00
|
|
|
if (filterOption) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return filterOption(filter, item);
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return text.indexOf(filter) >= 0;
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
renderItemHtml(item) {
|
|
|
|
const { renderItem = noop } = this.$props;
|
|
|
|
const renderResult = renderItem(item);
|
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
|
2018-04-07 00:20:45 +08:00
|
|
|
return {
|
|
|
|
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
|
|
|
|
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
filterNull(arr) {
|
|
|
|
return arr.filter(item => {
|
|
|
|
return item !== null;
|
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2018-04-07 00:20:45 +08:00
|
|
|
const {
|
2019-01-12 11:33:27 +08:00
|
|
|
prefixCls,
|
|
|
|
dataSource,
|
|
|
|
titleText,
|
|
|
|
checkedKeys,
|
|
|
|
lazy,
|
|
|
|
disabled,
|
|
|
|
body,
|
|
|
|
footer,
|
|
|
|
showSearch,
|
|
|
|
filter,
|
|
|
|
searchPlaceholder,
|
|
|
|
notFoundContent,
|
|
|
|
itemUnit,
|
|
|
|
itemsUnit,
|
|
|
|
} = this.$props;
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
// Custom Layout
|
2019-01-12 11:33:27 +08:00
|
|
|
const footerDom = footer && footer({ ...this.$props });
|
|
|
|
const bodyDom = body && body({ ...this.$props });
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
const listCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-with-footer`]: !!footerDom,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const filteredDataSource = [];
|
|
|
|
const totalDataSource = [];
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const showItems = dataSource.map(item => {
|
|
|
|
const { renderedText, renderedEl } = this.renderItemHtml(item);
|
2018-04-07 00:20:45 +08:00
|
|
|
if (filter && filter.trim() && !this.matchFilter(renderedText, item)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return null;
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// all show items
|
2019-01-12 11:33:27 +08:00
|
|
|
totalDataSource.push(item);
|
2018-04-07 00:20:45 +08:00
|
|
|
if (!item.disabled) {
|
|
|
|
// response to checkAll items
|
2019-01-12 11:33:27 +08:00
|
|
|
filteredDataSource.push(item);
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const checked = checkedKeys.indexOf(item.key) >= 0;
|
2018-04-07 00:20:45 +08:00
|
|
|
return (
|
|
|
|
<Item
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled={disabled}
|
2018-04-07 00:20:45 +08:00
|
|
|
key={item.key}
|
|
|
|
item={item}
|
|
|
|
lazy={lazy}
|
|
|
|
renderedText={renderedText}
|
|
|
|
renderedEl={renderedEl}
|
|
|
|
checked={checked}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
onClick={this._handleSelect}
|
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const unit = dataSource.length > 1 ? itemsUnit : itemUnit;
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
const search = showSearch ? (
|
|
|
|
<div class={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
2018-09-05 21:28:54 +08:00
|
|
|
onChange={this._handleFilter}
|
2018-04-07 00:20:45 +08:00
|
|
|
handleClear={this.handleClear}
|
|
|
|
placeholder={searchPlaceholder}
|
|
|
|
value={filter}
|
2019-01-07 20:58:18 +08:00
|
|
|
disabled={disabled}
|
2018-04-07 00:20:45 +08:00
|
|
|
/>
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
) : null;
|
|
|
|
const transitionName = this.mounted ? `${prefixCls}-content-item-highlight` : '';
|
2018-04-07 00:20:45 +08:00
|
|
|
const transitionProps = getTransitionProps(transitionName, {
|
|
|
|
leave: noop,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2019-01-07 20:58:18 +08:00
|
|
|
|
|
|
|
const searchNotFound = showItems.every(item => item === null) && (
|
|
|
|
<div class={`${prefixCls}-body-not-found`}>{notFoundContent}</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
const listBody = bodyDom || (
|
|
|
|
<div
|
2019-01-12 11:33:27 +08:00
|
|
|
class={classNames(
|
|
|
|
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`,
|
|
|
|
)}
|
2018-04-07 00:20:45 +08:00
|
|
|
>
|
|
|
|
{search}
|
2019-05-28 11:37:38 +08:00
|
|
|
{!searchNotFound && (
|
2019-03-28 20:56:06 +08:00
|
|
|
<transition-group
|
|
|
|
{...transitionProps}
|
|
|
|
tag="ul"
|
|
|
|
class={`${prefixCls}-content`}
|
|
|
|
ref="listContentWrapper"
|
|
|
|
>
|
|
|
|
{showItems}
|
|
|
|
</transition-group>
|
2019-05-28 11:37:38 +08:00
|
|
|
)}
|
2019-01-07 20:58:18 +08:00
|
|
|
{searchNotFound}
|
2018-04-07 00:20:45 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const listFooter = footerDom ? <div class={`${prefixCls}-footer`}>{footerDom}</div> : null;
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const checkStatus = this.getCheckStatus(filteredDataSource);
|
|
|
|
const checkedAll = checkStatus === 'all';
|
2018-04-07 00:20:45 +08:00
|
|
|
const checkAllCheckbox = (
|
|
|
|
<Checkbox
|
2019-01-12 11:33:27 +08:00
|
|
|
ref="checkbox"
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled={disabled}
|
2018-04-07 00:20:45 +08:00
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus === 'part'}
|
|
|
|
onChange={() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.handleSelectAll(filteredDataSource, checkedAll);
|
2018-04-07 00:20:45 +08:00
|
|
|
}}
|
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={listCls}>
|
|
|
|
<div class={`${prefixCls}-header`}>
|
|
|
|
{checkAllCheckbox}
|
|
|
|
<span class={`${prefixCls}-header-selected`}>
|
|
|
|
<span>
|
2019-01-12 11:33:27 +08:00
|
|
|
{(checkedKeys.length > 0 ? `${checkedKeys.length}/` : '') + totalDataSource.length}{' '}
|
|
|
|
{unit}
|
2018-04-07 00:20:45 +08:00
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
<span class={`${prefixCls}-header-title`}>{titleText}</span>
|
2018-04-07 00:20:45 +08:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|