import TransBtn from './TransBtn'; import PropTypes from '../_util/vue-types'; import KeyCode from '../_util/KeyCode'; import classNames from '../_util/classNames'; import pickAttrs from '../_util/pickAttrs'; import { isValidElement } from '../_util/props-util'; import createRef from '../_util/createRef'; import { computed, reactive, watch } from 'vue'; import List from '../vc-virtual-list/List'; const OptionListProps = { prefixCls: PropTypes.string, id: PropTypes.string, options: PropTypes.array, flattenOptions: PropTypes.array, height: PropTypes.number, itemHeight: PropTypes.number, values: PropTypes.any, multiple: PropTypes.bool, open: PropTypes.bool, defaultActiveFirstOption: PropTypes.bool, notFoundContent: PropTypes.any, menuItemSelectedIcon: PropTypes.any, childrenAsData: PropTypes.bool, searchValue: PropTypes.string, virtual: PropTypes.bool, onSelect: PropTypes.func, onToggleOpen: PropTypes.func, /** Tell Select that some value is now active to make accessibility work */ onActiveValue: PropTypes.func, onScroll: PropTypes.func, /** Tell Select that mouse enter the popup to force re-render */ onMouseenter: PropTypes.func, }; /** * Using virtual list of option display. * Will fallback to dom if use customize render. */ const OptionList = { props: OptionListProps, name: 'OptionList', inheritAttrs: false, setup(props) { const itemPrefixCls = computed(() => `${props.prefixCls}-item`); // =========================== List =========================== const listRef = createRef(); const onListMouseDown = event => { event.preventDefault(); }; const scrollIntoView = index => { if (listRef.current) { listRef.current.scrollTo({ index }); } }; // ========================== Active ========================== const getEnabledActiveIndex = (index, offset = 1) => { const len = props.flattenOptions.length; for (let i = 0; i < len; i += 1) { const current = (index + i * offset + len) % len; const { group, data } = props.flattenOptions[current]; if (!group && !data.disabled) { return current; } } return -1; }; const state = reactive({ activeIndex: getEnabledActiveIndex(0), }); const setActive = (index, fromKeyboard = false) => { state.activeIndex = index; const info = { source: fromKeyboard ? 'keyboard' : 'mouse' }; // Trigger active event const flattenItem = props.flattenOptions[index]; if (!flattenItem) { props.onActiveValue(null, -1, info); return; } props.onActiveValue(flattenItem.data.value, index, info); }; // Auto active first item when list length or searchValue changed watch([props.flattenOptions.length, props.searchValue], () => { setActive(props.defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1); }); // Auto scroll to item position in single mode watch(props.open, () => { /** * React will skip `onChange` when component update. * `setActive` function will call root accessibility state update which makes re-render. * So we need to delay to let Input component trigger onChange first. */ const timeoutId = setTimeout(() => { if (!props.multiple && props.open && props.values.size === 1) { const value = Array.from(props.values)[0]; const index = props.flattenOptions.findIndex(({ data }) => data.value === value); setActive(index); scrollIntoView(index); } }); return () => clearTimeout(timeoutId); }); // ========================== Values ========================== const onSelectValue = value => { if (value !== undefined) { props.onSelect(value, { selected: !props.values.has(value) }); } // Single mode should always close by select if (!props.multiple) { props.onToggleOpen(false); } }; function renderItem(index) { const item = props.flattenOptions[index]; if (!item) return null; const itemData = item.data || {}; const { value, label, children } = itemData; const attrs = pickAttrs(itemData, true); const mergedLabel = props.childrenAsData ? children : label; return item ? (