From 7831553bee3bf3761b0794103a62c8404a98720a Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 30 Dec 2021 21:15:42 +0800 Subject: [PATCH] fix(Popup): close event emiited twice (#10132) --- packages/vant/src/popup/Popup.tsx | 7 ++++--- packages/vant/src/popup/test/index.spec.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/vant/src/popup/Popup.tsx b/packages/vant/src/popup/Popup.tsx index a98363f02..78efca23f 100644 --- a/packages/vant/src/popup/Popup.tsx +++ b/packages/vant/src/popup/Popup.tsx @@ -213,10 +213,11 @@ export default defineComponent({ watch( () => props.show, - (value) => { - if (value) { + (show) => { + if (show && !opened) { open(); - } else { + } + if (!show && opened) { opened = false; emit('close'); } diff --git a/packages/vant/src/popup/test/index.spec.js b/packages/vant/src/popup/test/index.spec.js index 13047ded1..85ef77403 100644 --- a/packages/vant/src/popup/test/index.spec.js +++ b/packages/vant/src/popup/test/index.spec.js @@ -129,6 +129,22 @@ test('should emit close event when show prop is set to false', async () => { expect(onClose).toHaveBeenCalledTimes(1); }); +test('should emit close event after clicking the overlay', async () => { + const onClose = jest.fn(); + wrapper = mount(Popup, { + props: { + show: true, + onClose, + 'onUpdate:show': (show) => { + wrapper.setProps({ show }); + }, + }, + }); + + await wrapper.find('.van-overlay').trigger('click'); + expect(onClose).toHaveBeenCalledTimes(1); +}); + test('should change duration when using duration prop', () => { wrapper = mount(Popup, { props: {