diff --git a/src/hooks/useModal/index.ts b/src/hooks/useModal/index.ts index 61cf6184c..aabec858f 100644 --- a/src/hooks/useModal/index.ts +++ b/src/hooks/useModal/index.ts @@ -1,52 +1,11 @@ -import { createVNode, render, getCurrentInstance, nextTick } from 'vue'; -import { MyModal } from './modal'; -import type { App, ComponentInternalInstance } from 'vue'; -import type { HookModalProps } from './types'; -export { useFormModal } from './useFormModal'; - -let _app: App; - -export const useModal = () => { - let _modalInstance: ComponentInternalInstance; - const appContext = _app._context || getCurrentInstance()?.appContext; - - const getModalInstance = () => { - if (_modalInstance) { - return _modalInstance; - } - const container = document.createElement('div'); - const vnode = createVNode(MyModal); - vnode.appContext = appContext; - render(vnode, container); - _modalInstance = vnode.component!; - _modalInstance.props._closeModal = hide; - return _modalInstance; - }; - - const hide = () => { - Object.assign(_modalInstance?.props, { visible: false }); - }; +import { installUseModal } from './useModal'; +import type { App } from 'vue'; - const show = async (props: HookModalProps) => { - const modalInstance = getModalInstance(); - - Object.assign(modalInstance?.props, { - ...props, - visible: true, - }); - await nextTick(); - }; - - return { - show, - hide, - }; -}; - -export type ModalInstance = ReturnType; +export { useModal, type ModalInstance } from './useModal'; +export { useFormModal } from './useFormModal'; export const install = (app: App) => { - _app = app; + installUseModal(app); }; export default install; diff --git a/src/hooks/useModal/useFormModal.tsx b/src/hooks/useModal/useFormModal.tsx index c36f485f2..2c75fd91e 100644 --- a/src/hooks/useModal/useFormModal.tsx +++ b/src/hooks/useModal/useFormModal.tsx @@ -1,5 +1,5 @@ import { nextTick, ref } from 'vue'; -import { useModal } from './index'; +import { useModal } from './useModal'; import type { SchemaFormRef, FormSchema } from '@/components/core/schema-form'; import type { FormModalProps } from './types'; import { SchemaForm } from '@/components/core/schema-form'; diff --git a/src/hooks/useModal/useModal.ts b/src/hooks/useModal/useModal.ts new file mode 100644 index 000000000..c8f3ce4e0 --- /dev/null +++ b/src/hooks/useModal/useModal.ts @@ -0,0 +1,49 @@ +import { createVNode, render, getCurrentInstance, nextTick } from 'vue'; +import { MyModal } from './modal'; +import type { App, ComponentInternalInstance } from 'vue'; +import type { HookModalProps } from './types'; + +let _app: App; + +export const useModal = () => { + let _modalInstance: ComponentInternalInstance; + const appContext = _app?._context || getCurrentInstance()?.appContext; + + const getModalInstance = () => { + if (_modalInstance) { + return _modalInstance; + } + const container = document.createElement('div'); + const vnode = createVNode(MyModal); + vnode.appContext = appContext; + render(vnode, container); + _modalInstance = vnode.component!; + _modalInstance.props._closeModal = hide; + return _modalInstance; + }; + + const hide = () => { + Object.assign(_modalInstance?.props, { visible: false }); + }; + + const show = async (props: HookModalProps) => { + const modalInstance = getModalInstance(); + + Object.assign(modalInstance?.props, { + ...props, + visible: true, + }); + await nextTick(); + }; + + return { + show, + hide, + }; +}; + +export type ModalInstance = ReturnType; + +export const installUseModal = (app: App) => { + _app = app; +};