import classNames from '../_util/classNames'; import PropTypes, { withUndefined } from '../_util/vue-types'; import { isValidElement, splitAttrs, findDOMNode, filterEmpty } from '../_util/props-util'; import initDefaultProps from '../_util/props-util/initDefaultProps'; import BaseMixin from '../_util/BaseMixin'; import Checkbox from '../checkbox'; import Search from './search'; import defaultRenderList from './renderListBody'; import triggerEvent from '../_util/triggerEvent'; import { defineComponent, nextTick, VNode, VNodeTypes } from 'vue'; import { RadioChangeEvent } from '../radio/interface'; const defaultRender = () => null; const TransferItem = { key: PropTypes.string.isRequired, title: PropTypes.string.isRequired, description: PropTypes.string, disabled: PropTypes.looseBool, }; export interface DataSourceItem { key: string; title: string; description?: string; disabled?: boolean; } function isRenderResultPlainObject(result: VNode) { return ( result && !isValidElement(result) && Object.prototype.toString.call(result) === '[object Object]' ); } export const TransferListProps = { prefixCls: PropTypes.string, titleText: PropTypes.string, dataSource: PropTypes.arrayOf(PropTypes.shape(TransferItem).loose), 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.looseBool, searchPlaceholder: PropTypes.string, notFoundContent: PropTypes.any, itemUnit: PropTypes.string, itemsUnit: PropTypes.string, body: PropTypes.any, renderList: PropTypes.any, footer: PropTypes.any, lazy: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])), disabled: PropTypes.looseBool, direction: PropTypes.string, showSelectAll: PropTypes.looseBool, onItemSelect: PropTypes.func, onItemSelectAll: PropTypes.func, onScroll: PropTypes.func, }; function renderListNode(renderList: Function, props: any) { let bodyContent = renderList ? renderList(props) : null; const customize = !!bodyContent && filterEmpty(bodyContent).length > 0; if (!customize) { bodyContent = defaultRenderList(props); } return { customize, bodyContent, }; } export default defineComponent({ name: 'TransferList', mixins: [BaseMixin], inheritAttrs: false, props: initDefaultProps(TransferListProps, { dataSource: [], titleText: '', showSearch: false, lazy: {}, }), setup() { return { timer: null, triggerScrollTimer: null, scrollEvent: null, }; }, data() { return { filterValue: '', }; }, beforeUnmount() { clearTimeout(this.triggerScrollTimer); // if (this.scrollEvent) { // this.scrollEvent.remove(); // } }, updated() { nextTick(() => { if (this.scrollEvent) { this.scrollEvent.remove(); } }); }, methods: { handleScroll(e: Event) { this.$emit('scroll', e); }, getCheckStatus(filteredItems: DataSourceItem[]) { const { checkedKeys } = this.$props; if (checkedKeys.length === 0) { return 'none'; } if (filteredItems.every(item => checkedKeys.indexOf(item.key) >= 0 || !!item.disabled)) { return 'all'; } return 'part'; }, getFilteredItems(dataSource: DataSourceItem[], filterValue: string) { 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: string, searchPlaceholder: string, filterValue: string, filteredItems: DataSourceItem[], notFoundContent: unknown, bodyDom: unknown, filteredRenderItems: unknown, checkedKeys: string[], renderList: Function, showSearch: boolean, disabled: boolean, ) { const search = showSearch ? (