1
- import { Application , ShowModalOptions , View } from '@nativescript/core' ;
1
+ import {
2
+ Application ,
3
+ ShowModalOptions as CoreShowModalOptions ,
4
+ View ,
5
+ } from '@nativescript/core' ;
2
6
import {
3
7
App ,
4
8
Component ,
@@ -7,32 +11,32 @@ import {
7
11
unref ,
8
12
warn ,
9
13
} from '@vue/runtime-core' ;
10
- import { NSVElement , NSVRoot } from '../dom' ;
11
- import { createNativeView } from '../runtimeHelpers' ;
12
14
import { isObject } from '@vue/shared' ;
15
+ import { NSVElement , NSVRoot } from '../dom' ;
16
+ import { CreateNativeViewProps , createNativeView } from '../runtimeHelpers' ;
13
17
14
18
declare module'@vue/runtime-core' {
15
19
export interface ComponentCustomProperties {
16
- /**
17
- * todo: update docblock
18
- */
19
- $showModal :< T = any > (
20
- component :Component ,
21
- options ?:ModalOptions ,
20
+ $showModal :< T = any , P = any > (
21
+ component :Component < P > ,
22
+ options ?:ShowModalOptions < P , T > ,
22
23
) => Promise < T | false | undefined > ;
23
- $closeModal :( arg : any ) => void ;
24
+ $closeModal :< T = any > ( data : T , ... args : any [ ] ) => void ;
24
25
$modal :{
25
- close :( arg : any ) => void ;
26
+ close :< T = any > ( data : T , ... args : any [ ] ) => void ;
26
27
} ;
27
28
}
28
29
}
29
30
30
31
type ResolvableModalTarget = ComponentPublicInstance | NSVElement | View ;
31
32
32
- export interface ModalOptions extends Partial < ShowModalOptions > {
33
- props ?:Record < string , any > ;
33
+ export type ShowModalOptions < P = any , T = any > = Partial <
34
+ Omit < CoreShowModalOptions , 'closeCallback' >
35
+ > & {
36
+ closeCallback ?:( data ?:T , ...args :any [ ] ) => void ;
37
+ props ?:CreateNativeViewProps < P > ;
34
38
target ?:ResolvableModalTarget ;
35
- }
39
+ } ;
36
40
37
41
/**
38
42
*@internal
@@ -57,9 +61,9 @@ function resolveModalTarget(
57
61
return false ;
58
62
}
59
63
60
- export async function $showModal < T = any > (
61
- component :Component ,
62
- options :ModalOptions = { } ,
64
+ export async function $showModal < T = any , P = any > (
65
+ component :Component < P > ,
66
+ options :ShowModalOptions < P , T > = { } ,
63
67
) :Promise < T | false | undefined > {
64
68
const modalTarget = resolveModalTarget (
65
69
options . target ?? Application . getRootView ( ) ,
@@ -87,7 +91,7 @@ export async function $showModal<T = any>(
87
91
reload :reloadModal ,
88
92
} ) ;
89
93
90
- const closeCallback = ( data ?:T ) => {
94
+ const closeCallback = ( data ?:T , ... args : any ) => {
91
95
if ( isResolved ) return ;
92
96
93
97
if ( isReloading ) {
@@ -107,6 +111,10 @@ export async function $showModal<T = any>(
107
111
view . unmount ( ) ;
108
112
view = null ;
109
113
114
+ // call the closeCallback if it exists with all arguments
115
+ options . closeCallback ?.( data , ...args ) ;
116
+
117
+ // resolve the promise with the first argument, since Promise.resolve() expects only one argument
110
118
resolve ( data ) ;
111
119
} ;
112
120
@@ -118,7 +126,9 @@ export async function $showModal<T = any>(
118
126
...additionalOptions ,
119
127
} ) ;
120
128
} ;
121
- const closeModal = ( ...args :any [ ] ) => view . nativeView ?. closeModal ( ...args ) ;
129
+ const closeModal = ( ...args :any [ ] ) => {
130
+ view . nativeView ?. closeModal ( ...args ) ;
131
+ } ;
122
132
123
133
view . context . config . globalProperties . $closeModal = closeModal ;
124
134
view . context . config . globalProperties . $modal = { close :closeModal } ;