import Menu, { Item as MenuItem } from '../../menu'; import type { PropType } from 'vue'; import { onBeforeUnmount, defineComponent, inject, ref } from 'vue'; import type { OptionProps } from './Option'; import MentionsContextKey from './MentionsContext'; import Spin from '../../spin'; function noop() {} export default defineComponent({ name: 'DropdownMenu', props: { prefixCls: String, options: { type: Array as PropType, default: () => [], }, }, slots: ['notFoundContent', 'option'], setup(props, { slots }) { const { activeIndex, setActiveIndex, selectOption, onFocus = noop, loading, } = inject(MentionsContextKey, { activeIndex: ref(), loading: ref(false), }); let timeoutId: any; const onMousedown = (e: MouseEvent) => { clearTimeout(timeoutId); timeoutId = setTimeout(() => { onFocus(e); }); }; onBeforeUnmount(() => { clearTimeout(timeoutId); }); return () => { const { prefixCls, options } = props; const activeOption = options[activeIndex.value] || {}; return ( { const option = options.find(({ value }) => value === key); selectOption(option); }} onMousedown={onMousedown} > {!loading.value && options.map((option, index) => { const { value, disabled, label = option.value } = option; return ( { setActiveIndex(index); }} > {slots.option?.(option) ?? (typeof label === 'function' ? label({ value, disabled }) : label)} ); })} {!loading.value && options.length === 0 ? ( {slots.notFoundContent?.()} ) : null} {loading.value && ( )} ); }; }, });