import {computed, designComponent, getComponentCls, PropType, reactive, useModel} from "@peryl/react-compose"; import './dropdown.scss'; import {DropdownContextProvider, DropdownOptionAlign, DropdownOptionContextProvider, DropdownOptionTrigger, iDropdownContext, iDropdownOptionContext, renderDropdownPanel} from "./dropdown.utils"; import {findReactElement} from "../../utils/findReactElement"; import {StyleProps, useStyle} from "../../uses/useStyle"; import {Scroll} from "../Scroll"; import {delay} from "@peryl/utils/delay"; import {debounce} from "@peryl/utils/debounce"; import {DropdownOption} from "../DropdownOption"; import {DropdownGroup} from "../DropdownGroup"; import {unit} from "@peryl/utils/unit"; import {Popup} from "../Popup"; import {ePopupTrigger, iPopupAnimation, iPopupPlacement, iPopupTrigger} from "../usePopup/utils/popup.utils"; import ApplicationConfigurationProvider from "../ApplicationConfigurationProvider"; import {removeUnit} from "@peryl/utils/removeUnit"; export const Dropdown = designComponent({ name: '-dropdown', inheritPropsType: Popup, props: { /*---------------------------------------external attrs-------------------------------------------*/ disableCloseOnClickOption: { type: Boolean }, // 是否禁用点击option的时候自动关闭 align: { type: String as PropType }, // dropdown option的内容对其方式 size: StyleProps.size, scrollOptionLength: { type: Number, default: 6 }, // dropdown option超过一定数量时使用滚动容器 /*---------------------------------------popper attrs-------------------------------------------*/ modelValue: { type: Boolean }, trigger: { type: String as PropType, default: 'click' }, animation: { type: String as PropType, default: 'drop' }, noArrow: { type: Boolean, default: true, }, placement: { type: String as PropType, default: 'bottom-start' }, id: { type: String }, // 调试plain-popper标识 optionWidth: {}, // option选项宽度 scrollHeight: { type: Number }, // 选项过多时自定义滚动高度 }, emits: { onUpdateModelValue: (val?: boolean) => true, onSelect: (optionContext: iDropdownOptionContext) => true, onClose: () => true, }, slots: [ 'default', 'popper', ], scopeSlots: { reference: (scope: { open?: boolean }) => {}, }, setup({ props, slots, event: { emit }, scopeSlots, attrs }) { const configuration = ApplicationConfigurationProvider.inject(); const { styleComputed } = useStyle(); const model = useModel(() => props.modelValue, emit.onUpdateModelValue); const state = reactive({ cascadeOptions: [] as iDropdownOptionContext[] }); const trigger = computed(() => props.trigger || ePopupTrigger.click); const dropdownContentWidth = computed(() => !props.optionWidth ? undefined : unit(props.optionWidth as any)!); const scrollHeight = computed((): number => { if (props.scrollHeight != null) {return props.scrollHeight;} const fontSize = Number(removeUnit(configuration.value.theme.vars[`font-size-${styleComputed.value.size}`])); const paddingY = Number(removeUnit(configuration.value.theme.vars[`dropdown-size-${styleComputed.value.size}-padding-y`])); const itemHeight = fontSize + paddingY * 2 + 6 + 2; return itemHeight * 6; }); const handler: iDropdownContext["handler"] = { onClickOption: (optionContext) => { if (optionContext.hasChildren.value) { return; } if (!props.disableCloseOnClickOption) { model.value = false; } }, openDropdownOption: debounce((optionContext) => { emit.onSelect(optionContext); if (optionContext.hasChildren.value) { /*有子节点*/ const cascadeOptions = [] as iDropdownOptionContext[]; let optCtx: iDropdownOptionContext | null = optionContext; while (!!optCtx) { cascadeOptions.unshift(optCtx); optCtx = optCtx.parentOption; } setTimeout(() => {state.cascadeOptions = cascadeOptions;}, 23); } }, trigger.value === DropdownOptionTrigger.hover ? 300 : 0, trigger.value === DropdownOptionTrigger.hover), onPopperClose: () => { emit.onClose(); delay(23).then(() => state.cascadeOptions = []); }, }; DropdownContextProvider.provide({ handler, state, trigger, props }); const renderDropdownContent = (children: any, key: string) => { let hasScroll = false; const dropdownOptions = findReactElement(children, (node: any) => { if (node?.type?.use?.name === Scroll.use.name) { hasScroll = true; return false; } return node?.type?.use?.name === DropdownOption.use.name || node?.type?.use?.name === DropdownGroup.use.name; }); const publicDropdownPanelParams = { size: styleComputed.value.size, align: props.align, sizeEqual: attrs.sizeEqual }; if (hasScroll || !dropdownOptions || dropdownOptions.length <= props.scrollOptionLength) { return renderDropdownPanel({ ...publicDropdownPanelParams, attrs: { style: { width: dropdownContentWidth.value }, key }, content: children }); } else { return renderDropdownPanel({ ...publicDropdownPanelParams, attrs: { style: { height: scrollHeight.value + 'px', width: dropdownContentWidth.value, }, key }, content: <> {children} }); } }; return () => { const slotsPopper = slots.popper(); return ( {{ default: () => scopeSlots.reference({ open: model.value }, slots.default()), popper: () => ( <> {renderDropdownContent(slotsPopper, '_1')} {state.cascadeOptions.length > 0 && ( state.cascadeOptions.map((dropdownOptionContext) => ( renderDropdownContent( {dropdownOptionContext.slots.content()} , dropdownOptionContext.id ) )) )} ), }} ); }; }, }); export default Dropdown;