From e8bbdf974b044698a6a0f5ed8c76690a2e5cf205 Mon Sep 17 00:00:00 2001 From: Jeremy <15975785+jw-foss@users.noreply.github.com> Date: Tue, 31 Jan 2023 11:58:52 +0800 Subject: [PATCH] feat: integrate use popper (#11045) * feat: integrate use popper * Integrate popper with use popper hook. * Reorganize code for better readabilities. * fix: contentStyle typing * fix: test failure * fix: slider placement testing * refactor: slider test case refactoring * fix: virtual triggering --------- Co-authored-by: JeremyWuuuuu <15975785+JeremyWuuuuu@users.noreply.github.com> --- .../popper/__tests__/content.test.tsx | 46 ++++- packages/components/popper/src/arrow.vue | 9 +- .../popper/src/composables/index.ts | 3 + .../popper/src/composables/use-content-dom.ts | 59 ++++++ .../popper/src/composables/use-content.ts | 89 +++++++++ .../popper/src/composables/use-focus-trap.ts | 61 ++++++ packages/components/popper/src/content.vue | 173 ++++-------------- packages/components/popper/src/utils.ts | 21 +-- .../slider/__tests__/slider.test.tsx | 3 +- packages/hooks/use-popper/index.ts | 6 +- packages/tokens/popper.ts | 3 +- 11 files changed, 308 insertions(+), 165 deletions(-) create mode 100644 packages/components/popper/src/composables/index.ts create mode 100644 packages/components/popper/src/composables/use-content-dom.ts create mode 100644 packages/components/popper/src/composables/use-content.ts create mode 100644 packages/components/popper/src/composables/use-focus-trap.ts diff --git a/packages/components/popper/__tests__/content.test.tsx b/packages/components/popper/__tests__/content.test.tsx index b4830af8a9..d46cc9fab8 100644 --- a/packages/components/popper/__tests__/content.test.tsx +++ b/packages/components/popper/__tests__/content.test.tsx @@ -1,4 +1,4 @@ -import { nextTick, ref } from 'vue' +import { defineComponent, nextTick, ref } from 'vue' import { mount } from '@vue/test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { POPPER_INJECTION_KEY } from '@element-plus/tokens' @@ -14,6 +14,21 @@ const popperInjection = { contentRef: ref(), } +const TestComponent = defineComponent({ + setup() { + return { + contentRef: ref(), + } + }, + render() { + return ( + + {AXIOM} + + ) + }, +}) + const mountContent = (props = {}) => mount({AXIOM}, { global: { @@ -23,6 +38,15 @@ const mountContent = (props = {}) => }, }) +const mountWrappedContent = (props = {}) => + mount(, { + global: { + provide: { + [POPPER_INJECTION_KEY as symbol]: popperInjection, + }, + }, + }) + describe('', () => { describe('with triggerRef provided', () => { const triggerKls = 'el-popper__trigger' @@ -48,8 +72,16 @@ describe('', () => { expect(wrapper.html()).toContain(AXIOM) expect(popperInjection.popperInstanceRef.value).toBeDefined() expect(wrapper.classes()).toEqual(['el-popper', 'is-dark']) + expect(wrapper.vm.contentStyle).toHaveLength(3) expect(wrapper.vm.contentStyle[0]).toHaveProperty('zIndex') - expect(wrapper.vm.contentStyle[1]).toBeUndefined() + expect(wrapper.vm.contentStyle[1]).toEqual({}) + expect(wrapper.vm.contentStyle[2]).toEqual( + expect.objectContaining({ + position: 'absolute', + top: '0', + left: '0', + }) + ) }) it('should be able to be pure and themed', async () => { @@ -94,7 +126,6 @@ describe('', () => { describe('instantiate popper instance', () => { it('should be able to update the current instance', async () => { - wrapper = mountContent() await nextTick() vi.spyOn( @@ -113,10 +144,11 @@ describe('', () => { }) it('should be able to update the reference node', async () => { - wrapper = mountContent() + const w = mountWrappedContent() await nextTick() - const oldInstance = wrapper.vm.popperInstanceRef + const { contentRef } = w.vm + const oldInstance = contentRef.popperInstanceRef const newRef = document.createElement('div') newRef.classList.add('new-ref') @@ -124,13 +156,13 @@ describe('', () => { popperInjection.triggerRef.value = newRef await nextTick() - expect(wrapper.vm.popperInstanceRef).not.toStrictEqual(oldInstance) + expect(contentRef.popperInstanceRef).not.toStrictEqual(oldInstance) popperInjection.triggerRef.value = undefined await nextTick() - expect(wrapper.vm.popperInstanceRef).toBeUndefined() + expect(contentRef.popperInstanceRef).toBeUndefined() }) }) }) diff --git a/packages/components/popper/src/arrow.vue b/packages/components/popper/src/arrow.vue index 7914b80e79..fb3aac4f74 100644 --- a/packages/components/popper/src/arrow.vue +++ b/packages/components/popper/src/arrow.vue @@ -1,5 +1,10 @@