2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2021-09-25 16:51:32 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { isValidElement, splitAttrs, filterEmpty } from '../_util/props-util';
|
|
|
|
import DownOutlined from '@ant-design/icons-vue/DownOutlined';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Checkbox from '../checkbox';
|
2021-09-25 16:51:32 +08:00
|
|
|
import Menu from '../menu';
|
|
|
|
import Dropdown from '../dropdown';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Search from './search';
|
2021-09-25 16:51:32 +08:00
|
|
|
import ListBody from './ListBody';
|
|
|
|
import type { VNode, VNodeTypes, ExtractPropTypes, PropType } from 'vue';
|
|
|
|
import { watchEffect, computed, defineComponent, ref } from 'vue';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { RadioChangeEvent } from '../radio/interface';
|
2022-03-12 09:56:32 +08:00
|
|
|
import type { TransferDirection, TransferItem } from './index';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
const defaultRender = () => null;
|
2018-04-07 10:32:13 +08:00
|
|
|
|
2020-11-13 10:51:12 +08:00
|
|
|
function isRenderResultPlainObject(result: VNode) {
|
2019-01-12 11:33:27 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
function getEnabledItemKeys<RecordType extends TransferItem>(items: RecordType[]) {
|
|
|
|
return items.filter(data => !data.disabled).map(data => data.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const transferListProps = {
|
2018-04-07 00:20:45 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2021-09-25 16:51:32 +08:00
|
|
|
dataSource: { type: Array as PropType<TransferItem[]>, default: [] },
|
2018-04-07 00:20:45 +08:00
|
|
|
filter: PropTypes.string,
|
|
|
|
filterOption: PropTypes.func,
|
|
|
|
checkedKeys: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
handleFilter: PropTypes.func,
|
|
|
|
handleClear: PropTypes.func,
|
|
|
|
renderItem: PropTypes.func,
|
2021-09-25 16:51:32 +08:00
|
|
|
showSearch: PropTypes.looseBool.def(false),
|
2018-04-07 00:20:45 +08:00
|
|
|
searchPlaceholder: PropTypes.string,
|
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
itemUnit: PropTypes.string,
|
|
|
|
itemsUnit: PropTypes.string,
|
2020-03-07 19:45:13 +08:00
|
|
|
renderList: PropTypes.any,
|
2020-10-10 18:16:28 +08:00
|
|
|
disabled: PropTypes.looseBool,
|
2022-03-12 09:56:32 +08:00
|
|
|
direction: String as PropType<TransferDirection>,
|
2020-10-10 18:16:28 +08:00
|
|
|
showSelectAll: PropTypes.looseBool,
|
2021-09-25 16:51:32 +08:00
|
|
|
remove: PropTypes.string,
|
|
|
|
selectAll: PropTypes.string,
|
|
|
|
selectCurrent: PropTypes.string,
|
|
|
|
selectInvert: PropTypes.string,
|
|
|
|
removeAll: PropTypes.string,
|
|
|
|
removeCurrent: PropTypes.string,
|
|
|
|
selectAllLabel: PropTypes.any,
|
|
|
|
showRemove: PropTypes.looseBool,
|
|
|
|
pagination: PropTypes.any,
|
2020-08-05 17:17:07 +08:00
|
|
|
onItemSelect: PropTypes.func,
|
|
|
|
onItemSelectAll: PropTypes.func,
|
2021-09-25 16:51:32 +08:00
|
|
|
onItemRemove: PropTypes.func,
|
2020-08-05 17:17:07 +08:00
|
|
|
onScroll: PropTypes.func,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export type TransferListProps = Partial<ExtractPropTypes<typeof transferListProps>>;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2020-10-20 16:45:49 +08:00
|
|
|
export default defineComponent({
|
2018-04-07 00:20:45 +08:00
|
|
|
name: 'TransferList',
|
2020-07-19 22:40:35 +08:00
|
|
|
inheritAttrs: false,
|
2021-09-25 16:51:32 +08:00
|
|
|
props: transferListProps,
|
|
|
|
emits: ['scroll', 'itemSelectAll', 'itemRemove', 'itemSelect'],
|
|
|
|
slots: ['footer', 'titleText'],
|
|
|
|
setup(props, { attrs, slots }) {
|
|
|
|
const filterValue = ref('');
|
|
|
|
const transferNode = ref();
|
|
|
|
const defaultListBodyRef = ref();
|
|
|
|
|
|
|
|
const renderListBody = (renderList: any, props: any) => {
|
|
|
|
let bodyContent = renderList ? renderList(props) : null;
|
|
|
|
const customize = !!bodyContent && filterEmpty(bodyContent).length > 0;
|
|
|
|
if (!customize) {
|
|
|
|
bodyContent = <ListBody {...props} ref={defaultListBodyRef} />;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
customize,
|
|
|
|
bodyContent,
|
|
|
|
};
|
2020-10-20 16:45:49 +08:00
|
|
|
};
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
const renderItemHtml = (item: TransferItem) => {
|
|
|
|
const { renderItem = defaultRender } = props;
|
|
|
|
const renderResult = renderItem(item);
|
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
|
|
|
|
return {
|
|
|
|
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
|
|
|
|
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
|
|
|
|
item,
|
|
|
|
};
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
const filteredItems = ref([]);
|
|
|
|
const filteredRenderItems = ref([]);
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
const fItems = [];
|
|
|
|
const fRenderItems = [];
|
|
|
|
|
|
|
|
props.dataSource.forEach(item => {
|
|
|
|
const renderedItem = renderItemHtml(item);
|
|
|
|
const { renderedText } = renderedItem;
|
|
|
|
|
|
|
|
// Filter skip
|
|
|
|
if (filterValue.value && filterValue.value.trim() && !matchFilter(renderedText, item)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
fItems.push(item);
|
|
|
|
fRenderItems.push(renderedItem);
|
|
|
|
});
|
|
|
|
filteredItems.value = fItems;
|
|
|
|
filteredRenderItems.value = fRenderItems;
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
const checkStatus = computed(() => {
|
|
|
|
const { checkedKeys } = props;
|
2018-04-07 00:20:45 +08:00
|
|
|
if (checkedKeys.length === 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 'none';
|
2020-03-07 19:45:13 +08:00
|
|
|
}
|
2021-09-25 16:51:32 +08:00
|
|
|
if (
|
|
|
|
filteredItems.value.every(item => checkedKeys.indexOf(item.key) >= 0 || !!item.disabled)
|
|
|
|
) {
|
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';
|
2021-09-25 16:51:32 +08:00
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const enabledItemKeys = computed(() => {
|
|
|
|
return getEnabledItemKeys(filteredItems.value);
|
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const getNewSelectKeys = (keys, unCheckedKeys) => {
|
|
|
|
return Array.from(new Set([...keys, ...props.checkedKeys])).filter(
|
|
|
|
key => unCheckedKeys.indexOf(key) === -1,
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
const getCheckBox = ({ disabled, prefixCls }: { disabled?: boolean; prefixCls?: string }) => {
|
2021-09-25 16:51:32 +08:00
|
|
|
const checkedAll = checkStatus.value === 'all';
|
2022-03-12 09:56:32 +08:00
|
|
|
const checkAllCheckbox = (
|
2021-09-25 16:51:32 +08:00
|
|
|
<Checkbox
|
|
|
|
disabled={disabled}
|
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus.value === 'part'}
|
|
|
|
class={`${prefixCls}-checkbox`}
|
|
|
|
onChange={() => {
|
|
|
|
// Only select enabled items
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const keys = enabledItemKeys.value;
|
|
|
|
props.onItemSelectAll(
|
|
|
|
getNewSelectKeys(!checkedAll ? keys : [], checkedAll ? props.checkedKeys : []),
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return checkAllCheckbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFilter = (e: RadioChangeEvent) => {
|
|
|
|
const {
|
|
|
|
target: { value: filter },
|
|
|
|
} = e;
|
|
|
|
filterValue.value = filter;
|
|
|
|
props.handleFilter?.(e);
|
|
|
|
};
|
|
|
|
const handleClear = (e: Event) => {
|
|
|
|
filterValue.value = '';
|
|
|
|
props.handleClear?.(e);
|
|
|
|
};
|
|
|
|
const matchFilter = (text: string, item: TransferItem) => {
|
|
|
|
const { filterOption } = props;
|
|
|
|
if (filterOption) {
|
|
|
|
return filterOption(filterValue.value, item);
|
|
|
|
}
|
|
|
|
return text.indexOf(filterValue.value) >= 0;
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const getSelectAllLabel = (selectedCount: number, totalCount: number) => {
|
|
|
|
const { itemsUnit, itemUnit, selectAllLabel } = props;
|
|
|
|
if (selectAllLabel) {
|
|
|
|
return typeof selectAllLabel === 'function'
|
|
|
|
? selectAllLabel({ selectedCount, totalCount })
|
|
|
|
: selectAllLabel;
|
|
|
|
}
|
|
|
|
const unit = totalCount > 1 ? itemsUnit : itemUnit;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{(selectedCount > 0 ? `${selectedCount}/` : '') + totalCount} {unit}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const getListBody = (
|
2020-11-13 10:51:12 +08:00
|
|
|
prefixCls: string,
|
|
|
|
searchPlaceholder: string,
|
|
|
|
checkedKeys: string[],
|
|
|
|
renderList: Function,
|
|
|
|
showSearch: boolean,
|
|
|
|
disabled: boolean,
|
2021-09-25 16:51:32 +08:00
|
|
|
) => {
|
2020-03-07 19:45:13 +08:00
|
|
|
const search = showSearch ? (
|
|
|
|
<div class={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
2021-09-25 16:51:32 +08:00
|
|
|
onChange={handleFilter}
|
|
|
|
handleClear={handleClear}
|
2020-03-07 19:45:13 +08:00
|
|
|
placeholder={searchPlaceholder}
|
2021-09-25 16:51:32 +08:00
|
|
|
value={filterValue.value}
|
2020-03-07 19:45:13 +08:00
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
let bodyNode: VNodeTypes;
|
|
|
|
const { onEvents } = splitAttrs(attrs);
|
|
|
|
const { bodyContent, customize } = renderListBody(renderList, {
|
|
|
|
...props,
|
|
|
|
filteredItems: filteredItems.value,
|
|
|
|
filteredRenderItems: filteredRenderItems.value,
|
|
|
|
selectedKeys: checkedKeys,
|
|
|
|
...onEvents,
|
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08: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.value.length ? (
|
|
|
|
bodyContent
|
|
|
|
) : (
|
|
|
|
<div class={`${prefixCls}-body-not-found`}>{props.notFoundContent}</div>
|
2020-03-07 19:45:13 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={
|
|
|
|
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`
|
|
|
|
}
|
|
|
|
ref={transferNode}
|
|
|
|
>
|
|
|
|
{search}
|
|
|
|
{bodyNode}
|
|
|
|
</div>
|
2020-03-07 19:45:13 +08:00
|
|
|
);
|
2021-09-25 16:51:32 +08:00
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
return () => {
|
2020-03-07 19:45:13 +08:00
|
|
|
const {
|
2021-09-25 16:51:32 +08:00
|
|
|
prefixCls,
|
|
|
|
checkedKeys,
|
|
|
|
disabled,
|
|
|
|
showSearch,
|
|
|
|
searchPlaceholder,
|
|
|
|
selectAll,
|
|
|
|
selectCurrent,
|
|
|
|
selectInvert,
|
|
|
|
removeAll,
|
|
|
|
removeCurrent,
|
|
|
|
renderList,
|
|
|
|
onItemSelectAll,
|
|
|
|
onItemRemove,
|
2022-03-12 09:56:32 +08:00
|
|
|
showSelectAll = true,
|
2021-09-25 16:51:32 +08:00
|
|
|
showRemove,
|
|
|
|
pagination,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
// Custom Layout
|
|
|
|
const footerDom = slots.footer?.({ ...props });
|
|
|
|
|
|
|
|
const listCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-with-pagination`]: !!pagination,
|
|
|
|
[`${prefixCls}-with-footer`]: !!footerDom,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
// ================================= List Body =================================
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const listBody = getListBody(
|
|
|
|
prefixCls,
|
|
|
|
searchPlaceholder,
|
|
|
|
checkedKeys,
|
|
|
|
renderList,
|
|
|
|
showSearch,
|
|
|
|
disabled,
|
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const listFooter = footerDom ? <div class={`${prefixCls}-footer`}>{footerDom}</div> : null;
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
const checkAllCheckbox = !showRemove && !pagination && getCheckBox({ disabled, prefixCls });
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
let menu = null;
|
|
|
|
if (showRemove) {
|
|
|
|
menu = (
|
|
|
|
<Menu>
|
|
|
|
{/* Remove Current Page */}
|
|
|
|
{pagination && (
|
|
|
|
<Menu.Item
|
2022-03-12 09:56:32 +08:00
|
|
|
key="removeCurrent"
|
2021-09-25 16:51:32 +08:00
|
|
|
onClick={() => {
|
|
|
|
const pageKeys = getEnabledItemKeys(
|
|
|
|
(defaultListBodyRef.value.items || []).map(entity => entity.item),
|
|
|
|
);
|
|
|
|
onItemRemove?.(pageKeys);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{removeCurrent}
|
|
|
|
</Menu.Item>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* Remove All */}
|
|
|
|
<Menu.Item
|
2022-03-12 09:56:32 +08:00
|
|
|
key="removeAll"
|
2021-09-25 16:51:32 +08:00
|
|
|
onClick={() => {
|
|
|
|
onItemRemove?.(enabledItemKeys.value);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{removeAll}
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item
|
2022-03-12 09:56:32 +08:00
|
|
|
key="selectAll"
|
2021-09-25 16:51:32 +08:00
|
|
|
onClick={() => {
|
|
|
|
const keys = enabledItemKeys.value;
|
|
|
|
onItemSelectAll(getNewSelectKeys(keys, []));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{selectAll}
|
|
|
|
</Menu.Item>
|
|
|
|
{pagination && (
|
|
|
|
<Menu.Item
|
|
|
|
onClick={() => {
|
|
|
|
const pageKeys = getEnabledItemKeys(
|
|
|
|
(defaultListBodyRef.value.items || []).map(entity => entity.item),
|
|
|
|
);
|
|
|
|
onItemSelectAll(getNewSelectKeys(pageKeys, []));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{selectCurrent}
|
|
|
|
</Menu.Item>
|
|
|
|
)}
|
|
|
|
<Menu.Item
|
2022-03-12 09:56:32 +08:00
|
|
|
key="selectInvert"
|
2021-09-25 16:51:32 +08:00
|
|
|
onClick={() => {
|
|
|
|
let availableKeys: string[];
|
|
|
|
if (pagination) {
|
|
|
|
availableKeys = getEnabledItemKeys(
|
|
|
|
(defaultListBodyRef.value.items || []).map(entity => entity.item),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
availableKeys = enabledItemKeys.value;
|
|
|
|
}
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const checkedKeySet = new Set(checkedKeys);
|
|
|
|
const newCheckedKeys: string[] = [];
|
|
|
|
const newUnCheckedKeys: string[] = [];
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
availableKeys.forEach(key => {
|
|
|
|
if (checkedKeySet.has(key)) {
|
|
|
|
newUnCheckedKeys.push(key);
|
|
|
|
} else {
|
|
|
|
newCheckedKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
onItemSelectAll(getNewSelectKeys(newCheckedKeys, newUnCheckedKeys));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{selectInvert}
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
}
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const dropdown = (
|
|
|
|
<Dropdown class={`${prefixCls}-header-dropdown`} overlay={menu} disabled={disabled}>
|
|
|
|
<DownOutlined />
|
|
|
|
</Dropdown>
|
|
|
|
);
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
return (
|
|
|
|
<div class={listCls} style={attrs.style}>
|
|
|
|
<div class={`${prefixCls}-header`}>
|
2022-03-12 09:56:32 +08:00
|
|
|
{showSelectAll ? (
|
|
|
|
<>
|
|
|
|
{checkAllCheckbox}
|
|
|
|
{dropdown}
|
|
|
|
</>
|
|
|
|
) : null}
|
2021-09-25 16:51:32 +08:00
|
|
|
<span class={`${prefixCls}-header-selected`}>
|
|
|
|
<span>{getSelectAllLabel(checkedKeys.length, filteredItems.value.length)}</span>
|
|
|
|
<span class={`${prefixCls}-header-title`}>{slots.titleText?.()}</span>
|
2018-04-07 00:20:45 +08:00
|
|
|
</span>
|
2021-09-25 16:51:32 +08:00
|
|
|
</div>
|
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
2018-04-07 00:20:45 +08:00
|
|
|
</div>
|
2021-09-25 16:51:32 +08:00
|
|
|
);
|
|
|
|
};
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
2020-10-20 16:45:49 +08:00
|
|
|
});
|