import {computed, designComponent, getComponentCls, reactive, useClasses} from "@peryl/react-compose"; import {useWatchAutoClear} from "../../utils/watchEffectAutoClear"; import {createSingleSelectRender} from "./createSingleSelectRender"; import {createMultipleSelectRender} from "./createMultipleSelectRender"; import {iSelectCompositionParams, SelectEmitsOption, SelectPropOptions, SelectScopeSlotsOption, SelectSlotsOption, useSelectHook} from "./select.utils"; import './select.scss'; import {useEdit} from "../../uses/useEdit"; import {useStyle} from "../../uses/useStyle"; import {createIsMatch} from "../../utils/isObjectCommonMatch"; import Input from "../Input"; import {createPopupAttrsGetter} from "../usePopup/utils/createPopperAttrsGetter"; import {useParentPopupId} from "../usePopup/utils/popup.utils"; import ApplicationConfigurationProvider from "../ApplicationConfigurationProvider"; import {removeUnit} from "@peryl/utils/removeUnit"; export const Select = designComponent({ name: '-select', props: SelectPropOptions, emits: SelectEmitsOption, slots: SelectSlotsOption, scopeSlots: SelectScopeSlotsOption, inheritPropsType: Input, setup({ props, slots, scopeSlots, event: { emit } }) { const parentPopupId = useParentPopupId(); const configuration = ApplicationConfigurationProvider.inject(); const { editComputed } = useEdit(); const { styleComputed } = useStyle(); const hooks = useSelectHook(); const getPublicPopperAttrs = createPopupAttrsGetter(() => ({ offset: 2, noArrow: true, sizeEqual: true, animation: 'drop', noPadding: true, popperSize: styleComputed.value.size, ...((props.popperAttrs as any) || {}), externalClass: [getComponentCls('select-popup'), (props.popperAttrs as any)?.externalClass], parentId: parentPopupId, })); const isPopperShow = (() => { const state = reactive({ value: false }); hooks.onPopperShow.use(() => {state.value = true;}); hooks.onPopperHide.use(() => {state.value = false;}); return state; })(); const classes = useClasses(() => [ getComponentCls('select'), { [`select-popper-show`]: isPopperShow.value } ]); 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 selectCompositionParams: iSelectCompositionParams = { props, classes, hooks, emit, slots, scopeSlots, editComputed, styleComputed, isMatch: createIsMatch(props), getPublicPopperAttrs, scrollHeight, }; const methods = { show: () => {hooks.onTriggerShow.exec(undefined);}, hide: () => {hooks.onTriggerHide.exec(undefined);}, }; useWatchAutoClear(() => props.multiple, multiple => { return multiple ? createMultipleSelectRender(selectCompositionParams) : createSingleSelectRender(selectCompositionParams); }); return { refer: { methods, }, render: () => hooks.renderInput.exec(null), }; }, });