1
- import type { ComputedRef , Ref } from 'vue' ;
2
- import { isRef , unref , computed , defineComponent , ref , watch } from 'vue' ;
3
- import type { VueNode } from '../../_util/type' ;
1
+ import type { Ref } from 'vue' ;
2
+ import { isRef , unref , computed , defineComponent , shallowRef , watch } from 'vue' ;
3
+ import type { MaybeRef , VueNode } from '../../_util/type' ;
4
4
import type { ModalFuncProps } from '../Modal' ;
5
5
import type { HookModalRef } from './HookModal' ;
6
6
import type { ModalStaticFunctions } from '../confirm' ;
@@ -12,28 +12,29 @@ import destroyFns from '../destroyFns';
12
12
let uuid = 0 ;
13
13
14
14
interface ElementsHolderRef {
15
- addModal : ( modal : ComputedRef < JSX . Element > ) => ( ) => void ;
15
+ addModal : ( modal : ( ) => JSX . Element ) => ( ) => void ;
16
16
}
17
17
18
18
const ElementsHolder = defineComponent ( {
19
19
name : 'ElementsHolder' ,
20
20
inheritAttrs : false ,
21
21
setup ( _ , { expose } ) {
22
- const modals = ref < ComputedRef < JSX . Element > [ ] > ( [ ] ) ;
23
- const addModal = ( modal : ComputedRef < JSX . Element > ) => {
22
+ const modals = shallowRef < ( ( ) => JSX . Element ) [ ] > ( [ ] ) ;
23
+ const addModal = ( modal : ( ) => JSX . Element ) => {
24
24
modals . value . push ( modal ) ;
25
+ modals . value = modals . value . slice ( ) ;
25
26
return ( ) => {
26
27
modals . value = modals . value . filter ( currentModal => currentModal !== modal ) ;
27
28
} ;
28
29
} ;
29
30
30
31
expose ( { addModal } ) ;
31
32
return ( ) => {
32
- return < > { modals . value . map ( modal => modal . value ) } </ > ;
33
+ return modals . value . map ( modal => modal ( ) ) ;
33
34
} ;
34
35
} ,
35
36
} ) ;
36
- export type ModalFuncWithRef = ( props : Ref < ModalFuncProps > | ModalFuncProps ) => {
37
+ export type ModalFuncWithRef = ( props : MaybeRef < ModalFuncProps > ) => {
37
38
destroy : ( ) => void ;
38
39
update : ( configUpdate : ModalFuncProps ) => void ;
39
40
} ;
@@ -42,9 +43,9 @@ function useModal(): readonly [
42
43
Omit < ModalStaticFunctions < ModalFuncWithRef > , 'warn' > ,
43
44
( ) => VueNode ,
44
45
] {
45
- const holderRef = ref < ElementsHolderRef > ( null ) ;
46
+ const holderRef = shallowRef < ElementsHolderRef > ( null ) ;
46
47
// ========================== Effect ==========================
47
- const actionQueue = ref ( [ ] ) ;
48
+ const actionQueue = shallowRef ( [ ] ) ;
48
49
watch (
49
50
actionQueue ,
50
51
( ) => {
@@ -65,10 +66,10 @@ function useModal(): readonly [
65
66
const getConfirmFunc = ( withFunc : ( config : ModalFuncProps ) => ModalFuncProps ) =>
66
67
function hookConfirm ( config : Ref < ModalFuncProps > | ModalFuncProps ) {
67
68
uuid += 1 ;
68
- const open = ref ( true ) ;
69
- const modalRef = ref < HookModalRef > ( null ) ;
70
- const configRef = ref ( unref ( config ) ) ;
71
- const updateConfig = ref ( { } ) ;
69
+ const open = shallowRef ( true ) ;
70
+ const modalRef = shallowRef < HookModalRef > ( null ) ;
71
+ const configRef = shallowRef ( unref ( config ) ) ;
72
+ const updateConfig = shallowRef ( { } ) ;
72
73
watch (
73
74
( ) => config ,
74
75
val => {
@@ -78,9 +79,17 @@ function useModal(): readonly [
78
79
} ) ;
79
80
} ,
80
81
) ;
82
+
83
+ const destroyAction = ( ...args : any [ ] ) => {
84
+ open . value = false ;
85
+ const triggerCancel = args . some ( param => param && param . triggerCancel ) ;
86
+ if ( configRef . value . onCancel && triggerCancel ) {
87
+ configRef . value . onCancel ( ( ) => { } , ...args . slice ( 1 ) ) ;
88
+ }
89
+ } ;
81
90
// eslint-disable-next-line prefer-const
82
91
let closeFunc : Function | undefined ;
83
- const modal = computed ( ( ) => (
92
+ const modal = ( ) => (
84
93
< HookModal
85
94
key = { `modal-${ uuid } ` }
86
95
config = { withFunc ( configRef . value ) }
@@ -91,22 +100,14 @@ function useModal(): readonly [
91
100
closeFunc ?.( ) ;
92
101
} }
93
102
/>
94
- ) ) ;
103
+ ) ;
95
104
96
105
closeFunc = holderRef . value ?. addModal ( modal ) ;
97
106
98
107
if ( closeFunc ) {
99
108
destroyFns . push ( closeFunc ) ;
100
109
}
101
110
102
- const destroyAction = ( ...args : any [ ] ) => {
103
- open . value = false ;
104
- const triggerCancel = args . some ( param => param && param . triggerCancel ) ;
105
- if ( configRef . value . onCancel && triggerCancel ) {
106
- configRef . value . onCancel ( ( ) => { } , ...args . slice ( 1 ) ) ;
107
- }
108
- } ;
109
-
110
111
const updateAction = ( newConfig : ModalFuncProps ) => {
111
112
configRef . value = {
112
113
...configRef . value ,
0 commit comments