2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-09-25 08:51:32 +00: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 03:33:27 +00:00
|
|
|
import Checkbox from '../checkbox';
|
2021-09-25 08:51:32 +00:00
|
|
|
import Menu from '../menu';
|
|
|
|
import Dropdown from '../dropdown';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Search from './search';
|
2021-09-25 08:51:32 +00:00
|
|
|
import ListBody from './ListBody';
|
|
|
|
import type { VNode, VNodeTypes, ExtractPropTypes, PropType } from 'vue';
|
|
|
|
import { watchEffect, computed, defineComponent, ref } from 'vue';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { RadioChangeEvent } from '../radio/interface';
|
2022-03-12 01:56:32 +00:00
|
|
|
import type { TransferDirection, TransferItem } from './index';
|
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
|
|
|
|
2020-11-13 02:51:12 +00:00
|
|
|
function isRenderResultPlainObject(result: VNode) {
|
2019-01-12 03:33:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
function getEnabledItemKeys<RecordType extends TransferItem>(items: RecordType[]) {
|
|
|
|
return items.filter(data => !data.disabled).map(data => data.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const transferListProps = {
|
2022-03-26 14:52:54 +00:00
|
|
|
prefixCls: String,
|
2021-09-25 08:51:32 +00:00
|
|
|
dataSource: { type: Array as PropType<TransferItem[]>, default: [] },
|
2022-03-26 14:52:54 +00:00
|
|
|
filter: String,
|
|
|
|
filterOption: Function,
|
2018-04-06 16:20:45 +00:00
|
|
|
checkedKeys: PropTypes.arrayOf(PropTypes.string),
|
2022-03-26 14:52:54 +00:00
|
|
|
handleFilter: Function,
|
|
|
|
handleClear: Function,
|
|
|
|
renderItem: Function,
|
|
|
|
showSearch: { type: Boolean, default: false },
|
|
|
|
searchPlaceholder: String,
|
2018-04-06 16:20:45 +00:00
|
|
|
notFoundContent: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
itemUnit: String,
|
|
|
|
itemsUnit: String,
|
2020-03-07 11:45:13 +00:00
|
|
|
renderList: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
disabled: { type: Boolean, default: undefined },
|
2022-03-12 01:56:32 +00:00
|
|
|
direction: String as PropType<TransferDirection>,
|
2022-03-26 14:52:54 +00:00
|
|
|
showSelectAll: { type: Boolean, default: undefined },
|
|
|
|
remove: String,
|
|
|
|
selectAll: String,
|
|
|
|
selectCurrent: String,
|
|
|
|
selectInvert: String,
|
|
|
|
removeAll: String,
|
|
|
|
removeCurrent: String,
|
2021-09-25 08:51:32 +00:00
|
|
|
selectAllLabel: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
showRemove: { type: Boolean, default: undefined },
|
2021-09-25 08:51:32 +00:00
|
|
|
pagination: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
onItemSelect: Function,
|
|
|
|
onItemSelectAll: Function,
|
|
|
|
onItemRemove: Function,
|
|
|
|
onScroll: Function,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
export type TransferListProps = Partial<ExtractPropTypes<typeof transferListProps>>;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-10-20 08:45:49 +00:00
|
|
|
export default defineComponent({
|
2018-04-06 16:20:45 +00:00
|
|
|
name: 'TransferList',
|
2020-07-19 14:40:35 +00:00
|
|
|
inheritAttrs: false,
|
2021-09-25 08:51:32 +00:00
|
|
|
props: transferListProps,
|
2022-03-26 14:52:54 +00:00
|
|
|
// emits: ['scroll', 'itemSelectAll', 'itemRemove', 'itemSelect'],
|
2021-09-25 08:51:32 +00:00
|
|
|
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 08:45:49 +00:00
|
|
|
};
|
2021-09-25 08:51:32 +00: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 03:33:27 +00:00
|
|
|
};
|
2021-09-25 08:51:32 +00: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 03:33:27 +00:00
|
|
|
});
|
2021-09-25 08:51:32 +00:00
|
|
|
|
|
|
|
const checkStatus = computed(() => {
|
|
|
|
const { checkedKeys } = 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
|
|
|
}
|
2021-09-25 08:51:32 +00:00
|
|
|
if (
|
|
|
|
filteredItems.value.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';
|
2021-09-25 08:51:32 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const enabledItemKeys = computed(() => {
|
|
|
|
return getEnabledItemKeys(filteredItems.value);
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const getNewSelectKeys = (keys, unCheckedKeys) => {
|
|
|
|
return Array.from(new Set([...keys, ...props.checkedKeys])).filter(
|
|
|
|
key => unCheckedKeys.indexOf(key) === -1,
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-03-12 01:56:32 +00:00
|
|
|
const getCheckBox = ({ disabled, prefixCls }: { disabled?: boolean; prefixCls?: string }) => {
|
2021-09-25 08:51:32 +00:00
|
|
|
const checkedAll = checkStatus.value === 'all';
|
2022-03-12 01:56:32 +00:00
|
|
|
const checkAllCheckbox = (
|
2021-09-25 08:51:32 +00:00
|
|
|
<Checkbox
|
|
|
|
disabled={disabled}
|
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus.value === 'part'}
|
|
|
|
class={`${prefixCls}-checkbox`}
|
|
|
|
onChange={() => {
|
|
|
|
// Only select enabled items
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00: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 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00: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 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const getListBody = (
|
2020-11-13 02:51:12 +00:00
|
|
|
prefixCls: string,
|
|
|
|
searchPlaceholder: string,
|
|
|
|
checkedKeys: string[],
|
|
|
|
renderList: Function,
|
|
|
|
showSearch: boolean,
|
|
|
|
disabled: boolean,
|
2021-09-25 08:51:32 +00:00
|
|
|
) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const search = showSearch ? (
|
|
|
|
<div class={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
2021-09-25 08:51:32 +00:00
|
|
|
onChange={handleFilter}
|
|
|
|
handleClear={handleClear}
|
2020-03-07 11:45:13 +00:00
|
|
|
placeholder={searchPlaceholder}
|
2021-09-25 08:51:32 +00:00
|
|
|
value={filterValue.value}
|
2020-03-07 11:45:13 +00:00
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2021-09-25 08:51:32 +00: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 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +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.value.length ? (
|
|
|
|
bodyContent
|
|
|
|
) : (
|
|
|
|
<div class={`${prefixCls}-body-not-found`}>{props.notFoundContent}</div>
|
2020-03-07 11:45:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={
|
|
|
|
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`
|
|
|
|
}
|
|
|
|
ref={transferNode}
|
|
|
|
>
|
|
|
|
{search}
|
|
|
|
{bodyNode}
|
|
|
|
</div>
|
2020-03-07 11:45:13 +00:00
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
return () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const {
|
2021-09-25 08:51:32 +00:00
|
|
|
prefixCls,
|
|
|
|
checkedKeys,
|
|
|
|
disabled,
|
|
|
|
showSearch,
|
|
|
|
searchPlaceholder,
|
|
|
|
selectAll,
|
|
|
|
selectCurrent,
|
|
|
|
selectInvert,
|
|
|
|
removeAll,
|
|
|
|
removeCurrent,
|
|
|
|
renderList,
|
|
|
|
onItemSelectAll,
|
|
|
|
onItemRemove,
|
2022-03-12 01:56:32 +00:00
|
|
|
showSelectAll = true,
|
2021-09-25 08:51:32 +00: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 03:33:27 +00:00
|
|
|
});
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
// ================================= List Body =================================
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const listBody = getListBody(
|
|
|
|
prefixCls,
|
|
|
|
searchPlaceholder,
|
|
|
|
checkedKeys,
|
|
|
|
renderList,
|
|
|
|
showSearch,
|
|
|
|
disabled,
|
|
|
|
);
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const listFooter = footerDom ? <div class={`${prefixCls}-footer`}>{footerDom}</div> : null;
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2022-03-12 01:56:32 +00:00
|
|
|
const checkAllCheckbox = !showRemove && !pagination && getCheckBox({ disabled, prefixCls });
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
let menu = null;
|
|
|
|
if (showRemove) {
|
|
|
|
menu = (
|
|
|
|
<Menu>
|
|
|
|
{/* Remove Current Page */}
|
|
|
|
{pagination && (
|
|
|
|
<Menu.Item
|
2022-03-12 01:56:32 +00:00
|
|
|
key="removeCurrent"
|
2021-09-25 08:51:32 +00:00
|
|
|
onClick={() => {
|
|
|
|
const pageKeys = getEnabledItemKeys(
|
|
|
|
(defaultListBodyRef.value.items || []).map(entity => entity.item),
|
|
|
|
);
|
|
|
|
onItemRemove?.(pageKeys);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{removeCurrent}
|
|
|
|
</Menu.Item>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* Remove All */}
|
|
|
|
<Menu.Item
|
2022-03-12 01:56:32 +00:00
|
|
|
key="removeAll"
|
2021-09-25 08:51:32 +00:00
|
|
|
onClick={() => {
|
|
|
|
onItemRemove?.(enabledItemKeys.value);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{removeAll}
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
menu = (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item
|
2022-03-12 01:56:32 +00:00
|
|
|
key="selectAll"
|
2021-09-25 08:51:32 +00: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 01:56:32 +00:00
|
|
|
key="selectInvert"
|
2021-09-25 08:51:32 +00:00
|
|
|
onClick={() => {
|
|
|
|
let availableKeys: string[];
|
|
|
|
if (pagination) {
|
|
|
|
availableKeys = getEnabledItemKeys(
|
|
|
|
(defaultListBodyRef.value.items || []).map(entity => entity.item),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
availableKeys = enabledItemKeys.value;
|
|
|
|
}
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const checkedKeySet = new Set(checkedKeys);
|
|
|
|
const newCheckedKeys: string[] = [];
|
|
|
|
const newUnCheckedKeys: string[] = [];
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00: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-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const dropdown = (
|
|
|
|
<Dropdown class={`${prefixCls}-header-dropdown`} overlay={menu} disabled={disabled}>
|
|
|
|
<DownOutlined />
|
|
|
|
</Dropdown>
|
|
|
|
);
|
2018-04-06 16:20:45 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
return (
|
|
|
|
<div class={listCls} style={attrs.style}>
|
|
|
|
<div class={`${prefixCls}-header`}>
|
2022-03-12 01:56:32 +00:00
|
|
|
{showSelectAll ? (
|
|
|
|
<>
|
|
|
|
{checkAllCheckbox}
|
|
|
|
{dropdown}
|
|
|
|
</>
|
|
|
|
) : null}
|
2021-09-25 08:51:32 +00:00
|
|
|
<span class={`${prefixCls}-header-selected`}>
|
|
|
|
<span>{getSelectAllLabel(checkedKeys.length, filteredItems.value.length)}</span>
|
|
|
|
<span class={`${prefixCls}-header-title`}>{slots.titleText?.()}</span>
|
2018-04-06 16:20:45 +00:00
|
|
|
</span>
|
2021-09-25 08:51:32 +00:00
|
|
|
</div>
|
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
2018-04-06 16:20:45 +00:00
|
|
|
</div>
|
2021-09-25 08:51:32 +00:00
|
|
|
);
|
|
|
|
};
|
2018-04-06 16:20:45 +00:00
|
|
|
},
|
2020-10-20 08:45:49 +00:00
|
|
|
});
|