@@ -15,33 +15,88 @@ import { MsgQueue } from '@opentiny/vue-renderless/modal'
15
15
import TINYModal from './src/index'
16
16
import Popconfirm from '@opentiny/vue-popconfirm'
17
17
import { version } from './package.json'
18
+ import type { ComponentPublicInstance } from '@opentiny/vue-common'
19
+
20
+ // 定义Modal选项接口
21
+ interface ModalOptions {
22
+ id ?: string
23
+ events ?: {
24
+ hide ?: ( params : any ) => void
25
+ confirm ?: ( params : any ) => void
26
+ show ?: ( params : any ) => void
27
+ }
28
+ componentType ?: 'alert' | 'confirm' | 'message' | 'popconfirm'
29
+ message ?: string
30
+ title ?: string
31
+ showFooter ?: boolean
32
+ type ?: string
33
+ status ?: string
34
+ mask ?: boolean
35
+ lockView ?: boolean
36
+ showHeader ?: boolean
37
+ showClose ?: boolean
38
+ [ key : string ] : any
39
+ }
40
+
41
+ // 定义ModalPromise接口,扩展Promise
42
+ interface ModalPromise extends Promise < string > {
43
+ vm ?: ComponentPublicInstance
44
+ }
45
+
46
+ // 定义TINYModal接口
47
+ interface TINYModalInstance extends ComponentPublicInstance {
48
+ version : string
49
+ model : {
50
+ prop : string
51
+ event : string
52
+ }
53
+ tiny_mode ?: boolean
54
+ tiny_theme ?: string
55
+ installed : boolean
56
+ alert : ( message : string | ModalOptions , title ?: string , options ?: ModalOptions ) => ModalPromise
57
+ confirm : ( message : string | ModalOptions , title ?: string , options ?: ModalOptions ) => ModalPromise
58
+ message : ( message : string | ModalOptions , title ?: string , options ?: ModalOptions ) => ModalPromise
59
+ popconfirm : ( message : string | ModalOptions , title ?: string , options ?: ModalOptions ) => ModalPromise
60
+ install : ( Vue : any ) => void
61
+ }
62
+
63
+ // 定义setupComponent接口
64
+ interface SetupComponent {
65
+ TINYModal : {
66
+ install : ( Vue : any ) => void
67
+ init : ( root : any ) => void
68
+ }
69
+ }
70
+
71
+ // 扩展TINYModal类型
72
+ const TINYModalComponent = TINYModal as unknown as TINYModalInstance
18
73
19
- TINYModal . version = version
74
+ TINYModalComponent . version = version
20
75
21
- TINYModal . model = {
76
+ TINYModalComponent . model = {
22
77
prop : 'modelValue' ,
23
78
event : 'update:modelValue'
24
79
}
25
80
26
- export function Modal ( options ) {
27
- const modalPromise = new Promise ( ( resolve ) => {
81
+ export function Modal ( options : ModalOptions ) : ModalPromise {
82
+ const modalPromise = new Promise < string > ( ( resolve ) => {
28
83
if ( options && options . id && MsgQueue . some ( ( comp ) => comp . id === options . id ) ) {
29
84
resolve ( 'exist' )
30
85
} else {
31
86
let events = options . events || { }
32
- let $modal
87
+ let $modal : ComponentPublicInstance
33
88
options . events = Object . assign ( { } , events , {
34
- hide ( params ) {
89
+ hide ( params : any ) {
35
90
events . hide && events . hide . call ( this , params )
36
- if ( $modal . beforeUnmouted ) {
37
- $modal . beforeUnmouted ( )
91
+ if ( $modal && ' beforeUnmouted' in $modal ) {
92
+ ; ( $modal as any ) . beforeUnmouted ( )
38
93
}
39
94
resolve ( params . type )
40
95
} ,
41
- confirm ( params ) {
96
+ confirm ( params : any ) {
42
97
events . confirm && events . confirm . call ( this , params )
43
98
} ,
44
- show ( params ) {
99
+ show ( params : any ) {
45
100
events . show && events . show . call ( this , params )
46
101
}
47
102
} )
@@ -50,27 +105,30 @@ export function Modal(options) {
50
105
el : document . createElement ( 'div' ) ,
51
106
propsData : Object . assign (
52
107
{
53
- 'tiny_mode' : TINYModal . tiny_mode ,
54
- 'tiny_theme' : TINYModal . tiny_theme
108
+ 'tiny_mode' : TINYModalComponent . tiny_mode ,
109
+ 'tiny_theme' : TINYModalComponent . tiny_theme
55
110
} ,
56
111
options
57
112
) ,
58
- component : options . componentType === 'popconfirm' ? Popconfirm : TINYModal
113
+ component : options . componentType === 'popconfirm' ? Popconfirm : TINYModalComponent
59
114
} )
60
115
61
- const open = $modal [ options . componentType === 'popconfirm' ? 'show' : 'open' ]
116
+ const open = $modal [ options . componentType === 'popconfirm' ? 'show' : 'open' ] as Function
62
117
if ( open ) {
63
118
open ( )
64
119
}
65
- setTimeout ( ( ) => ( modalPromise . vm = $modal ) , 0 )
120
+ setTimeout ( ( ) => {
121
+ ; ( modalPromise as ModalPromise ) . vm = $modal
122
+ } , 0 )
66
123
}
67
- } )
124
+ } ) as ModalPromise
68
125
return modalPromise
69
126
}
70
127
const modal = Modal
71
- const types = [ 'alert' , 'confirm' , 'message' , 'popconfirm' ]
128
+ const types = [ 'alert' , 'confirm' , 'message' , 'popconfirm' ] as const
129
+ type ModalType = ( typeof types ) [ number ]
72
130
73
- const defOpts = {
131
+ const defOpts : Record < ModalType , Partial < ModalOptions > > = {
74
132
alert : {
75
133
showFooter : true ,
76
134
type : 'alert'
@@ -91,8 +149,12 @@ const defOpts = {
91
149
}
92
150
93
151
types . forEach ( ( type ) => {
94
- TINYModal [ type ] = Modal [ type ] = function ( message , title , options ) {
95
- let opts
152
+ TINYModalComponent [ type ] = Modal [ type ] = function (
153
+ message : string | ModalOptions ,
154
+ title ?: string ,
155
+ options ?: ModalOptions
156
+ ) : ModalPromise {
157
+ let opts : Partial < ModalOptions > = { }
96
158
97
159
if ( typeof message === 'object' && message !== null ) {
98
160
opts = message
@@ -119,20 +181,20 @@ export const message = (Modal as any).message
119
181
export const confirm = ( Modal as any ) . confirm
120
182
export const popconfirm = ( Modal as any ) . popconfirm
121
183
122
- TINYModal . installed = false
123
- setupComponent . TINYModal = {
124
- install ( Vue ) {
125
- if ( TINYModal . installed ) return
184
+ TINYModalComponent . installed = false
185
+ ; ( setupComponent as SetupComponent ) . TINYModal = {
186
+ install ( Vue : any ) {
187
+ if ( TINYModalComponent . installed ) return
126
188
// vue3 或 vue2
127
189
const isVue2 = ! ! Vue . component
128
190
const tinyMode = isVue2 ? Vue . prototype . tiny_mode : Vue . config . globalProperties . tiny_mode
129
191
const tinyTheme = isVue2 ? Vue . prototype . tiny_theme : Vue . config . globalProperties . tiny_theme
130
192
const specifyPc = typeof process === 'object' ? process . env ?. TINY_MODE : null
131
- TINYModal . tiny_mode = specifyPc || ( tinyMode && tinyMode . value )
132
- TINYModal . tiny_theme = tinyTheme && tinyTheme . value
133
- TINYModal . installed = true
193
+ TINYModalComponent . tiny_mode = specifyPc || ( tinyMode && tinyMode . value )
194
+ TINYModalComponent . tiny_theme = tinyTheme && tinyTheme . value
195
+ TINYModalComponent . installed = true
134
196
} ,
135
- init ( root ) {
197
+ init ( root : any ) {
136
198
let prefix = root . $TinyModalApiPrefix || root . $apiPrefix || '$'
137
199
138
200
root [ `${ prefix } alert` ] = ( Modal as any ) . alert
@@ -142,8 +204,8 @@ setupComponent.TINYModal = {
142
204
}
143
205
}
144
206
145
- TINYModal . install = function ( Vue ) {
146
- Vue . component ( TINYModal . name , TINYModal )
207
+ TINYModalComponent . install = function ( Vue : any ) {
208
+ Vue . component ( TINYModalComponent . name , TINYModalComponent )
147
209
}
148
210
149
- export default TINYModal
211
+ export default TINYModalComponent
0 commit comments