@@ -59,9 +59,16 @@ export default {
5959
6060<script lang="ts" setup>
6161import { computed , inject , ref , watch } from ' vue'
62- import { messageBoxProps , type InputValidate , type MessageOptions , type MessageType } from ' ./types'
62+ import {
63+ messageBoxProps ,
64+ type InputValidate ,
65+ type MessageBeforeConfirmOption ,
66+ type MessageOptions ,
67+ type MessageResult ,
68+ type MessageType
69+ } from ' ./types'
6370import { defaultOptions , messageDefaultOptionKey } from ' .'
64- import { isDef } from ' ../common/util'
71+ import { isDef , isFunction } from ' ../common/util'
6572import { useTranslate } from ' ../composables/useTranslate'
6673
6774const props = defineProps (messageBoxProps )
@@ -83,10 +90,9 @@ const messageOption = inject(messageOptionKey, ref<MessageOptions>(defaultOption
8390 * 消息文案
8491 */
8592const msg = ref <string >(' ' )
86- // eslint-disable-next-line @typescript-eslint/ban-types
87- let onConfirm: Function | null = null
88- // eslint-disable-next-line @typescript-eslint/ban-types
89- let onCancel: Function | null = null
93+ let onConfirm: ((res : MessageResult ) => void ) | null = null
94+ let onCancel: ((res : MessageResult ) => void ) | null = null
95+ let beforeConfirm: ((options : MessageBeforeConfirmOption ) => void ) | null = null
9096const show = ref <boolean >(false )
9197/**
9298 * 标题
@@ -189,29 +195,53 @@ function toggleModal(action: 'confirm' | 'cancel' | 'modal') {
189195 if (type .value === ' prompt' && action === ' confirm' && ! validate ()) {
190196 return
191197 }
192- show .value = false
193198 switch (action ) {
194199 case ' confirm' :
195- onConfirm &&
196- onConfirm ({
200+ if (beforeConfirm ) {
201+ beforeConfirm ({
202+ resolve : (isPass ) => {
203+ if (isPass ) {
204+ handleConfirm ({
205+ action: action ,
206+ value: inputValue .value
207+ })
208+ }
209+ }
210+ })
211+ } else {
212+ handleConfirm ({
197213 action: action ,
198214 value: inputValue .value
199215 })
216+ }
200217 break
201218 case ' cancel' :
202- onCancel &&
203- onCancel ({
204- action: action
205- })
219+ handleCancel ({
220+ action: action
221+ })
206222 break
207223 default :
208- onCancel &&
209- onCancel ({
210- action: ' modal'
211- })
224+ handleCancel ({
225+ action: ' modal'
226+ })
212227 break
213228 }
214229}
230+
231+ function handleConfirm(result : MessageResult ) {
232+ show .value = false
233+ if (isFunction (onConfirm )) {
234+ onConfirm (result )
235+ }
236+ }
237+
238+ function handleCancel(result : MessageResult ) {
239+ show .value = false
240+ if (isFunction (onCancel )) {
241+ onCancel (result )
242+ }
243+ }
244+
215245/**
216246 * @description 如果存在校验规则行为,则进行判断校验是否通过规则。默认不存在校验直接铜鼓。
217247 * @return {Boolean} 是否通过校验
@@ -269,6 +299,7 @@ function reset(option: MessageOptions) {
269299 inputValidate = option .inputValidate !
270300 onConfirm = (option as any ).onConfirm
271301 onCancel = (option as any ).onCancel
302+ beforeConfirm = option .beforeConfirm !
272303 inputError .value = option .inputError !
273304 showErr .value = option .showErr !
274305 zIndex .value = option .zIndex !
0 commit comments