From 0ce7d690b6a401428ab083eba81887e7e4767178 Mon Sep 17 00:00:00 2001 From: yang1206 Date: Thu, 29 Feb 2024 10:14:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(popup,menu):=20=E4=BF=AE=E5=A4=8D=20popup?= =?UTF-8?q?=E7=BB=84=E4=BB=B6update:visible=E4=BA=8B=E4=BB=B6=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E8=B0=83=E7=94=A8=E4=B8=8Emenu=20=E7=BB=84=E4=BB=B6cl?= =?UTF-8?q?ose=20=E4=BA=8B=E4=BB=B6=E5=9C=A8=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E4=B8=AD=E6=97=A0=E6=B3=95=E8=A7=A6=E5=8F=91=20(#205)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closed #196 --- .../nutui/components/menuitem/menuitem.vue | 3 +- packages/nutui/components/popup/use-popup.ts | 39 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/nutui/components/menuitem/menuitem.vue b/packages/nutui/components/menuitem/menuitem.vue index b3f87001..0748c3eb 100644 --- a/packages/nutui/components/menuitem/menuitem.vue +++ b/packages/nutui/components/menuitem/menuitem.vue @@ -139,7 +139,8 @@ export default defineComponent({ :lock-scroll="parent?.props.lockScroll" :close-on-click-overlay="parent?.props.closeOnClickOverlay" @closed="handleClose" - @update:visible="handleVisible" + @open="handleVisible(true)" + @close="handleVisible(false)" > diff --git a/packages/nutui/components/popup/use-popup.ts b/packages/nutui/components/popup/use-popup.ts index 94581c29..ac3499f1 100644 --- a/packages/nutui/components/popup/use-popup.ts +++ b/packages/nutui/components/popup/use-popup.ts @@ -8,6 +8,7 @@ const initIndex = 500 let _zIndex = initIndex const componentName = `${PREFIX}-popup` export function usePopup(props: PopupProps, emit: SetupContext['emit']) { + let opened: boolean const state = reactive({ zIndex: props.zIndex, showSlot: true, @@ -36,21 +37,25 @@ export function usePopup(props: PopupProps, emit: SetupContext['emit }) const open = () => { - if (props.zIndex !== initIndex) - _zIndex = Number(props.zIndex) + if (!opened) { + opened = true + if (props.zIndex !== initIndex) + _zIndex = Number(props.zIndex) - emit('update:visible', true) - state.zIndex = ++_zIndex - state.showSlot = true + emit(UPDATE_VISIBLE_EVENT, true) + state.zIndex = ++_zIndex + state.showSlot = true - emit(OPEN_EVENT) + emit(OPEN_EVENT) + } } const close = () => { - // if (props.visible) - // return // 避免重复调用 - emit(UPDATE_VISIBLE_EVENT, false) - emit(CLOSE_EVENT) + if (opened) { + opened = false + emit(UPDATE_VISIBLE_EVENT, false) + emit(CLOSE_EVENT) + } } const onClick = (e: Event) => { @@ -60,15 +65,13 @@ export function usePopup(props: PopupProps, emit: SetupContext['emit const onClickCloseIcon = (e: Event) => { e.stopPropagation() emit('click-close-icon') - emit(UPDATE_VISIBLE_EVENT, false) - // close(); + close() } const onClickOverlay = () => { emit('click-overlay') if (props.closeOnClickOverlay) - emit(UPDATE_VISIBLE_EVENT, false) - // close(); + close() } const onOpened = () => { @@ -85,7 +88,13 @@ export function usePopup(props: PopupProps, emit: SetupContext['emit watch( () => props.visible, (val) => { - val ? open() : close() + if (val && !opened) + open() + + if (!val && opened) { + opened = false + emit('close') + } }, ) watchEffect(() => {