2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-10-11 13:31:57 +00:00
|
|
|
import PropTypes, { withUndefined } from '../_util/vue-types';
|
2020-08-13 09:46:44 +00:00
|
|
|
import {
|
|
|
|
isValidElement,
|
|
|
|
initDefaultProps,
|
|
|
|
splitAttrs,
|
|
|
|
findDOMNode,
|
|
|
|
filterEmpty,
|
|
|
|
} from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import Search from './search';
|
2020-03-07 11:45:13 +00:00
|
|
|
import defaultRenderList from './renderListBody';
|
2019-01-12 03:33:27 +00:00
|
|
|
import triggerEvent from '../_util/triggerEvent';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const defaultRender = () => null;
|
2018-04-07 02:32:13 +00:00
|
|
|
|
|
|
|
const TransferItem = {
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
title: PropTypes.string.isRequired,
|
2018-04-06 16:20:45 +00:00
|
|
|
description: PropTypes.string,
|
2020-10-10 10:16:28 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function isRenderResultPlainObject(result) {
|
|
|
|
return (
|
|
|
|
result &&
|
|
|
|
!isValidElement(result) &&
|
2018-04-06 16:20:45 +00:00
|
|
|
Object.prototype.toString.call(result) === '[object Object]'
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-06 16:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const TransferListProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
titleText: PropTypes.string,
|
2018-04-07 02:32:13 +00:00
|
|
|
dataSource: PropTypes.arrayOf(PropTypes.shape(TransferItem).loose),
|
2018-04-06 16:20:45 +00: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,
|
2020-10-10 10:16:28 +00:00
|
|
|
showSearch: PropTypes.looseBool,
|
2018-04-06 16:20:45 +00:00
|
|
|
searchPlaceholder: PropTypes.string,
|
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
itemUnit: PropTypes.string,
|
|
|
|
itemsUnit: PropTypes.string,
|
|
|
|
body: PropTypes.any,
|
2020-03-07 11:45:13 +00:00
|
|
|
renderList: PropTypes.any,
|
2018-04-06 16:20:45 +00:00
|
|
|
footer: PropTypes.any,
|
2020-10-11 13:31:57 +00:00
|
|
|
lazy: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])),
|
2020-10-10 10:16:28 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2020-03-07 11:45:13 +00:00
|
|
|
direction: PropTypes.string,
|
2020-10-10 10:16:28 +00:00
|
|
|
showSelectAll: PropTypes.looseBool,
|
2020-08-05 09:17:07 +00:00
|
|
|
onItemSelect: PropTypes.func,
|
|
|
|
onItemSelectAll: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-07-19 14:40:35 +00:00
|
|
|
function renderListNode(renderList, props) {
|
2020-03-07 11:45:13 +00:00
|
|
|
let bodyContent = renderList ? renderList(props) : null;
|
2020-08-13 09:46:44 +00:00
|
|
|
const customize = !!bodyContent && filterEmpty(bodyContent).length > 0;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!customize) {
|
2020-07-19 14:40:35 +00:00
|
|
|
bodyContent = defaultRenderList(props);
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
customize,
|
|
|
|
bodyContent,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:20:45 +00:00
|
|
|
export default {
|
|
|
|
name: 'TransferList',
|
|
|
|
mixins: [BaseMixin],
|
2020-07-19 14:40:35 +00:00
|
|
|
inheritAttrs: false,
|
2018-04-06 16:20:45 +00:00
|
|
|
props: initDefaultProps(TransferListProps, {
|
|
|
|
dataSource: [],
|
|
|
|
titleText: '',
|
|
|
|
showSearch: false,
|
|
|
|
lazy: {},
|
|
|
|
}),
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
this.timer = null;
|
|
|
|
this.triggerScrollTimer = null;
|
2018-04-06 16:20:45 +00:00
|
|
|
return {
|
2020-03-07 11:45:13 +00:00
|
|
|
filterValue: '',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-01-12 03:33:27 +00:00
|
|
|
clearTimeout(this.triggerScrollTimer);
|
2020-03-07 11:45:13 +00:00
|
|
|
// if (this.scrollEvent) {
|
|
|
|
// this.scrollEvent.remove();
|
|
|
|
// }
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.scrollEvent) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.scrollEvent.remove();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
handleScroll(e) {
|
|
|
|
this.$emit('scroll', e);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
getCheckStatus(filteredItems) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { checkedKeys } = this.$props;
|
2018-04-06 16:20:45 +00:00
|
|
|
if (checkedKeys.length === 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return 'none';
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
if (filteredItems.every(item => checkedKeys.indexOf(item.key) >= 0 || !!item.disabled)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return 'all';
|
2018-04-06 16:20:45 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return 'part';
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
getFilteredItems(dataSource, filterValue) {
|
|
|
|
const filteredItems = [];
|
|
|
|
const filteredRenderItems = [];
|
|
|
|
|
|
|
|
dataSource.forEach(item => {
|
|
|
|
const renderedItem = this.renderItemHtml(item);
|
|
|
|
const { renderedText } = renderedItem;
|
|
|
|
|
|
|
|
// Filter skip
|
|
|
|
if (filterValue && filterValue.trim() && !this.matchFilter(renderedText, item)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredItems.push(item);
|
|
|
|
filteredRenderItems.push(renderedItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
return { filteredItems, filteredRenderItems };
|
|
|
|
},
|
|
|
|
|
|
|
|
getListBody(
|
|
|
|
prefixCls,
|
|
|
|
searchPlaceholder,
|
|
|
|
filterValue,
|
|
|
|
filteredItems,
|
|
|
|
notFoundContent,
|
|
|
|
bodyDom,
|
|
|
|
filteredRenderItems,
|
|
|
|
checkedKeys,
|
|
|
|
renderList,
|
|
|
|
showSearch,
|
|
|
|
disabled,
|
|
|
|
) {
|
|
|
|
const search = showSearch ? (
|
|
|
|
<div class={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
|
|
|
onChange={this._handleFilter}
|
|
|
|
handleClear={this._handleClear}
|
|
|
|
placeholder={searchPlaceholder}
|
|
|
|
value={filterValue}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
let listBody = bodyDom;
|
|
|
|
if (!listBody) {
|
|
|
|
let bodyNode;
|
2020-07-19 14:40:35 +00:00
|
|
|
const { onEvents } = splitAttrs(this.$attrs);
|
2020-07-18 13:40:26 +00:00
|
|
|
const { bodyContent, customize } = renderListNode(renderList, {
|
2020-07-19 14:40:35 +00:00
|
|
|
...this.$props,
|
|
|
|
filteredItems,
|
|
|
|
filteredRenderItems,
|
|
|
|
selectedKeys: checkedKeys,
|
|
|
|
...onEvents,
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// We should wrap customize list body in a classNamed div to use flex layout.
|
|
|
|
if (customize) {
|
|
|
|
bodyNode = <div class={`${prefixCls}-body-customize-wrapper`}>{bodyContent}</div>;
|
|
|
|
} else {
|
|
|
|
bodyNode = filteredItems.length ? (
|
|
|
|
bodyContent
|
|
|
|
) : (
|
|
|
|
<div class={`${prefixCls}-body-not-found`}>{notFoundContent}</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
listBody = (
|
|
|
|
<div
|
|
|
|
class={classNames(
|
|
|
|
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{search}
|
|
|
|
{bodyNode}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return listBody;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCheckBox(filteredItems, showSelectAll, disabled) {
|
|
|
|
const checkStatus = this.getCheckStatus(filteredItems);
|
|
|
|
const checkedAll = checkStatus === 'all';
|
|
|
|
const checkAllCheckbox = showSelectAll !== false && (
|
|
|
|
<Checkbox
|
|
|
|
disabled={disabled}
|
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus === 'part'}
|
|
|
|
onChange={() => {
|
|
|
|
// Only select enabled items
|
|
|
|
this.$emit(
|
|
|
|
'itemSelectAll',
|
|
|
|
filteredItems.filter(item => !item.disabled).map(({ key }) => key),
|
|
|
|
!checkedAll,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return checkAllCheckbox;
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
_handleSelect(selectedItem) {
|
|
|
|
const { checkedKeys } = this.$props;
|
|
|
|
const result = checkedKeys.some(key => key === selectedItem.key);
|
|
|
|
this.handleSelect(selectedItem, !result);
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
_handleFilter(e) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { handleFilter } = this.$props;
|
|
|
|
const {
|
|
|
|
target: { value: filterValue },
|
|
|
|
} = e;
|
|
|
|
this.setState({ filterValue });
|
|
|
|
handleFilter(e);
|
|
|
|
if (!filterValue) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-06 16:20:45 +00:00
|
|
|
}
|
|
|
|
// Manually trigger scroll event for lazy search bug
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5631
|
2018-09-05 13:28:54 +00:00
|
|
|
this.triggerScrollTimer = setTimeout(() => {
|
2020-07-27 14:36:56 +00:00
|
|
|
const transferNode = findDOMNode(this);
|
2019-01-12 03:33:27 +00:00
|
|
|
const listNode = transferNode.querySelectorAll('.ant-transfer-list-content')[0];
|
2018-04-06 16:20:45 +00:00
|
|
|
if (listNode) {
|
2019-01-12 03:33:27 +00:00
|
|
|
triggerEvent(listNode, 'scroll');
|
2018-04-06 16:20:45 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
}, 0);
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
_handleClear(e) {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.setState({ filterValue: '' });
|
2019-01-12 03:33:27 +00:00
|
|
|
this.handleClear(e);
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
matchFilter(text, item) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { filterValue } = this.$data;
|
|
|
|
const { filterOption } = this.$props;
|
2018-04-06 16:20:45 +00:00
|
|
|
if (filterOption) {
|
2020-03-07 11:45:13 +00:00
|
|
|
return filterOption(filterValue, item);
|
2018-04-06 16:20:45 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
return text.indexOf(filterValue) >= 0;
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
renderItemHtml(item) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { renderItem = defaultRender } = this.$props;
|
2019-01-12 03:33:27 +00:00
|
|
|
const renderResult = renderItem(item);
|
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
|
2018-04-06 16:20:45 +00:00
|
|
|
return {
|
|
|
|
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
|
|
|
|
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
|
2020-03-07 11:45:13 +00:00
|
|
|
item,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
filterNull(arr) {
|
|
|
|
return arr.filter(item => {
|
|
|
|
return item !== null;
|
|
|
|
});
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { filterValue } = this.$data;
|
2018-04-06 16:20:45 +00:00
|
|
|
const {
|
2019-01-12 03:33:27 +00:00
|
|
|
prefixCls,
|
|
|
|
dataSource,
|
|
|
|
titleText,
|
|
|
|
checkedKeys,
|
|
|
|
disabled,
|
|
|
|
body,
|
|
|
|
footer,
|
|
|
|
showSearch,
|
|
|
|
searchPlaceholder,
|
|
|
|
notFoundContent,
|
|
|
|
itemUnit,
|
|
|
|
itemsUnit,
|
2020-03-07 11:45:13 +00:00
|
|
|
renderList,
|
|
|
|
showSelectAll,
|
2019-01-12 03:33:27 +00:00
|
|
|
} = this.$props;
|
2018-04-06 16:20:45 +00:00
|
|
|
|
|
|
|
// Custom Layout
|
2019-01-12 03:33:27 +00:00
|
|
|
const footerDom = footer && footer({ ...this.$props });
|
|
|
|
const bodyDom = body && body({ ...this.$props });
|
2018-04-06 16:20:45 +00:00
|
|
|
|
|
|
|
const listCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-with-footer`]: !!footerDom,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
// ====================== Get filtered, checked item list ======================
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const { filteredItems, filteredRenderItems } = this.getFilteredItems(dataSource, filterValue);
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
// ================================= List Body =================================
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const unit = dataSource.length > 1 ? itemsUnit : itemUnit;
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const listBody = this.getListBody(
|
|
|
|
prefixCls,
|
|
|
|
searchPlaceholder,
|
|
|
|
filterValue,
|
|
|
|
filteredItems,
|
|
|
|
notFoundContent,
|
|
|
|
bodyDom,
|
|
|
|
filteredRenderItems,
|
|
|
|
checkedKeys,
|
|
|
|
renderList,
|
|
|
|
showSearch,
|
|
|
|
disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const listFooter = footerDom ? <div class={`${prefixCls}-footer`}>{footerDom}</div> : null;
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const checkAllCheckbox = this.getCheckBox(filteredItems, showSelectAll, disabled);
|
2018-04-06 16:20:45 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-19 14:40:35 +00:00
|
|
|
<div class={listCls} style={this.$attrs.style}>
|
2018-04-06 16:20:45 +00:00
|
|
|
<div class={`${prefixCls}-header`}>
|
|
|
|
{checkAllCheckbox}
|
|
|
|
<span class={`${prefixCls}-header-selected`}>
|
|
|
|
<span>
|
2020-03-07 11:45:13 +00:00
|
|
|
{(checkedKeys.length > 0 ? `${checkedKeys.length}/` : '') + filteredItems.length}{' '}
|
2019-01-12 03:33:27 +00:00
|
|
|
{unit}
|
2018-04-06 16:20:45 +00:00
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
<span class={`${prefixCls}-header-title`}>{titleText}</span>
|
2018-04-06 16:20:45 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|