Skip to content

Commit 702671b

Browse files
authored
feat(modal): enhance TINYModal functionality, add type definition and option interface (#3247)
1 parent da19616 commit 702671b

File tree

1 file changed

+93
-31
lines changed

1 file changed

+93
-31
lines changed

packages/vue/src/modal/index.ts

+93-31
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,88 @@ import { MsgQueue } from '@opentiny/vue-renderless/modal'
1515
import TINYModal from './src/index'
1616
import Popconfirm from '@opentiny/vue-popconfirm'
1717
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
1873

19-
TINYModal.version = version
74+
TINYModalComponent.version = version
2075

21-
TINYModal.model = {
76+
TINYModalComponent.model = {
2277
prop: 'modelValue',
2378
event: 'update:modelValue'
2479
}
2580

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) => {
2883
if (options && options.id && MsgQueue.some((comp) => comp.id === options.id)) {
2984
resolve('exist')
3085
} else {
3186
let events = options.events || {}
32-
let $modal
87+
let $modal: ComponentPublicInstance
3388
options.events = Object.assign({}, events, {
34-
hide(params) {
89+
hide(params: any) {
3590
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()
3893
}
3994
resolve(params.type)
4095
},
41-
confirm(params) {
96+
confirm(params: any) {
4297
events.confirm && events.confirm.call(this, params)
4398
},
44-
show(params) {
99+
show(params: any) {
45100
events.show && events.show.call(this, params)
46101
}
47102
})
@@ -50,27 +105,30 @@ export function Modal(options) {
50105
el: document.createElement('div'),
51106
propsData: Object.assign(
52107
{
53-
'tiny_mode': TINYModal.tiny_mode,
54-
'tiny_theme': TINYModal.tiny_theme
108+
'tiny_mode': TINYModalComponent.tiny_mode,
109+
'tiny_theme': TINYModalComponent.tiny_theme
55110
},
56111
options
57112
),
58-
component: options.componentType === 'popconfirm' ? Popconfirm : TINYModal
113+
component: options.componentType === 'popconfirm' ? Popconfirm : TINYModalComponent
59114
})
60115

61-
const open = $modal[options.componentType === 'popconfirm' ? 'show' : 'open']
116+
const open = $modal[options.componentType === 'popconfirm' ? 'show' : 'open'] as Function
62117
if (open) {
63118
open()
64119
}
65-
setTimeout(() => (modalPromise.vm = $modal), 0)
120+
setTimeout(() => {
121+
;(modalPromise as ModalPromise).vm = $modal
122+
}, 0)
66123
}
67-
})
124+
}) as ModalPromise
68125
return modalPromise
69126
}
70127
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]
72130

73-
const defOpts = {
131+
const defOpts: Record<ModalType, Partial<ModalOptions>> = {
74132
alert: {
75133
showFooter: true,
76134
type: 'alert'
@@ -91,8 +149,12 @@ const defOpts = {
91149
}
92150

93151
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> = {}
96158

97159
if (typeof message === 'object' && message !== null) {
98160
opts = message
@@ -119,20 +181,20 @@ export const message = (Modal as any).message
119181
export const confirm = (Modal as any).confirm
120182
export const popconfirm = (Modal as any).popconfirm
121183

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
126188
// vue3 或 vue2
127189
const isVue2 = !!Vue.component
128190
const tinyMode = isVue2 ? Vue.prototype.tiny_mode : Vue.config.globalProperties.tiny_mode
129191
const tinyTheme = isVue2 ? Vue.prototype.tiny_theme : Vue.config.globalProperties.tiny_theme
130192
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
134196
},
135-
init(root) {
197+
init(root: any) {
136198
let prefix = root.$TinyModalApiPrefix || root.$apiPrefix || '$'
137199

138200
root[`${prefix}alert`] = (Modal as any).alert
@@ -142,8 +204,8 @@ setupComponent.TINYModal = {
142204
}
143205
}
144206

145-
TINYModal.install = function (Vue) {
146-
Vue.component(TINYModal.name, TINYModal)
207+
TINYModalComponent.install = function (Vue: any) {
208+
Vue.component(TINYModalComponent.name, TINYModalComponent)
147209
}
148210

149-
export default TINYModal
211+
export default TINYModalComponent

0 commit comments

Comments
 (0)