From ed174ff73fea164c021857b4bd56d6c76ea8f877 Mon Sep 17 00:00:00 2001 From: zz <2418184580@qq.com> Date: Wed, 22 Jun 2022 01:01:11 +0800 Subject: [PATCH] refactor(components): [popover] switch to script-setup syntax (#8273) --- .../popover/__tests__/directive.test.ts | 2 +- .../popover/__tests__/popover.test.ts | 2 +- packages/components/popover/index.ts | 36 ++--- packages/components/popover/src/directive.ts | 5 +- packages/components/popover/src/index.vue | 136 ------------------ packages/components/popover/src/popover.ts | 18 ++- packages/components/popover/src/popover.vue | 109 ++++++++++++++ packages/utils/vue/install.ts | 8 ++ 8 files changed, 149 insertions(+), 167 deletions(-) delete mode 100644 packages/components/popover/src/index.vue create mode 100644 packages/components/popover/src/popover.vue diff --git a/packages/components/popover/__tests__/directive.test.ts b/packages/components/popover/__tests__/directive.test.ts index c2db6b8bda984..f13c6f7ded33b 100644 --- a/packages/components/popover/__tests__/directive.test.ts +++ b/packages/components/popover/__tests__/directive.test.ts @@ -2,7 +2,7 @@ import { nextTick, ref } from 'vue' import { afterEach, describe, expect, test } from 'vitest' import makeMount from '@element-plus/test-utils/make-mount' import { rAF } from '@element-plus/test-utils/tick' -import Popover from '../src/index.vue' +import Popover from '../src/popover.vue' import PopoverDirective, { VPopover } from '../src/directive' const AXIOM = 'Rem is the best girl' diff --git a/packages/components/popover/__tests__/popover.test.ts b/packages/components/popover/__tests__/popover.test.ts index 9764ddf56c90d..f0c381d4a0c53 100644 --- a/packages/components/popover/__tests__/popover.test.ts +++ b/packages/components/popover/__tests__/popover.test.ts @@ -4,7 +4,7 @@ import { POPPER_CONTAINER_SELECTOR, useZIndex } from '@element-plus/hooks' import makeMount from '@element-plus/test-utils/make-mount' import { rAF } from '@element-plus/test-utils/tick' import { ElPopperTrigger } from '@element-plus/components/popper' -import Popover from '../src/index.vue' +import Popover from '../src/popover.vue' const AXIOM = 'Rem is the best girl' diff --git a/packages/components/popover/index.ts b/packages/components/popover/index.ts index 91afb371eb1f5..bd0ad3ecf492f 100644 --- a/packages/components/popover/index.ts +++ b/packages/components/popover/index.ts @@ -1,28 +1,16 @@ -import Popover from './src/index.vue' -import PopoverDirective, { VPopover } from './src/directive' - -import type { App } from 'vue' -import type { SFCWithInstall } from '@element-plus/utils' +import { withInstall, withInstallDirective } from '@element-plus/utils' -Popover.install = (app: App): void => { - app.component(Popover.name, Popover) -} -;(PopoverDirective as SFCWithInstall).install = ( - app: App -) => { - app.directive(VPopover, PopoverDirective) -} - -const _PopoverDirective = PopoverDirective as SFCWithInstall< - typeof PopoverDirective -> +import Popover from './src/popover.vue' +import PopoverDirective, { VPopover } from './src/directive' -Popover.directive = _PopoverDirective +export const ElPopoverDirective = withInstallDirective( + PopoverDirective, + VPopover +) -const _Popover = Popover as any as SFCWithInstall & { - directive: typeof _PopoverDirective -} +export const ElPopover = withInstall(Popover, { + directive: ElPopoverDirective, +}) +export default ElPopover -export default _Popover -export const ElPopover = _Popover -export const ElPopoverDirective = _PopoverDirective +export * from './src/popover' diff --git a/packages/components/popover/src/directive.ts b/packages/components/popover/src/directive.ts index 8c1eb42ef108b..bde11f7545af0 100644 --- a/packages/components/popover/src/directive.ts +++ b/packages/components/popover/src/directive.ts @@ -1,9 +1,8 @@ -import type ElPopover from './index.vue' import type { DirectiveBinding, ObjectDirective } from 'vue' +import type { PopoverInstance } from './popover' const attachEvents = (el: HTMLElement, binding: DirectiveBinding) => { - const popperComponent: InstanceType = - binding.arg || binding.value + const popperComponent: PopoverInstance = binding.arg || binding.value const popover = popperComponent?.popperRef if (popover) { popover.triggerRef = el diff --git a/packages/components/popover/src/index.vue b/packages/components/popover/src/index.vue deleted file mode 100644 index ca572c1af382c..0000000000000 --- a/packages/components/popover/src/index.vue +++ /dev/null @@ -1,136 +0,0 @@ - - diff --git a/packages/components/popover/src/popover.ts b/packages/components/popover/src/popover.ts index dec7f228d664a..857fb264211dd 100644 --- a/packages/components/popover/src/popover.ts +++ b/packages/components/popover/src/popover.ts @@ -1,11 +1,13 @@ -import { buildProps } from '@element-plus/utils' +import { buildProps, isBoolean } from '@element-plus/utils' import { useTooltipContentProps, useTooltipTriggerProps, } from '@element-plus/components/tooltip' import { dropdownProps } from '@element-plus/components/dropdown' +import type { ExtractPropTypes } from 'vue' +import type Popover from './popover.vue' -export const usePopoverProps = buildProps({ +export const popoverProps = buildProps({ trigger: useTooltipTriggerProps.trigger, placement: dropdownProps.placement, disabled: useTooltipTriggerProps.disabled, @@ -56,3 +58,15 @@ export const usePopoverProps = buildProps({ default: true, }, } as const) +export type PopoverProps = ExtractPropTypes + +export const popoverEmits = { + 'update:visible': (value: boolean) => isBoolean(value), + 'before-enter': () => true, + 'before-leave': () => true, + 'after-enter': () => true, + 'after-leave': () => true, +} +export type PopoverEmits = typeof popoverEmits + +export type PopoverInstance = InstanceType diff --git a/packages/components/popover/src/popover.vue b/packages/components/popover/src/popover.vue new file mode 100644 index 0000000000000..daaf2b7784cdc --- /dev/null +++ b/packages/components/popover/src/popover.vue @@ -0,0 +1,109 @@ + + diff --git a/packages/utils/vue/install.ts b/packages/utils/vue/install.ts index d2bea3748abb8..fe1c5125893d5 100644 --- a/packages/utils/vue/install.ts +++ b/packages/utils/vue/install.ts @@ -30,6 +30,14 @@ export const withInstallFunction = (fn: T, name: string) => { return fn as SFCInstallWithContext } +export const withInstallDirective = (directive: T, name: string) => { + ;(directive as SFCWithInstall).install = (app: App): void => { + app.directive(name, directive) + } + + return directive as SFCWithInstall +} + export const withNoopInstall = (component: T) => { ;(component as SFCWithInstall).install = NOOP