@@ -70,6 +70,7 @@ interface DialogProps {
7070 closeByEsc? : boolean
7171 beforeCloseFunction? : (() => void | Promise <void | boolean >) | null
7272 beforeOpenFunction? : (() => void | Promise <void >) | null
73+ beforeCancelFunction? : (() => void | Promise <void | boolean >) | null
7374 askForCloseConfirmation? : boolean
7475 closeConfirmationText? : string
7576 removeFromDomOnClose? : boolean
@@ -82,6 +83,7 @@ const props = withDefaults(defineProps<DialogProps>(), {
8283 closeByEsc: true ,
8384 beforeCloseFunction: null ,
8485 beforeOpenFunction: null ,
86+ beforeCancelFunction: null ,
8587 askForCloseConfirmation: false ,
8688 closeConfirmationText: ' Are you sure you want to close this dialog?' ,
8789 removeFromDomOnClose: false ,
@@ -109,19 +111,32 @@ async function close() {
109111 isModalOpen .value = false ;
110112}
111113
114+ async function cancel() {
115+ if (props .beforeCancelFunction ) {
116+ const shouldCancel = await props .beforeCancelFunction ?.();
117+ if (shouldCancel === false ) return ;
118+ }
119+ isModalOpen .value = false ;
120+ }
121+
112122defineOptions ({
113123 inheritAttrs: false ,
114124})
115125
116126defineExpose ({
117127 open: open ,
118128 close: close ,
129+ cancel: cancel ,
119130 tryToHideModal: tryToHideModal ,
120131})
121132
122- function tryToHideModal() {
123- if (! props .askForCloseConfirmation ) {
124- close ();
133+ function tryToHideModal(isCancelAction = false ) {
134+ if (! props .askForCloseConfirmation ) {
135+ if (isCancelAction ) {
136+ cancel ();
137+ } else {
138+ close ();
139+ }
125140 } else {
126141 showConfirmationOnClose .value = true ;
127142 }
@@ -136,10 +151,8 @@ function toggleModal() {
136151}
137152
138153function onEsc(event : KeyboardEvent ) {
139- if (event .key === ' Escape' ) {
140- if (props .closeByEsc ) {
141- tryToHideModal ();
142- }
154+ if (event .key === ' Escape' && props .closeByEsc ) {
155+ tryToHideModal (true );
143156 }
144157}
145158
@@ -154,7 +167,7 @@ onUnmounted(() => {
154167
155168function backdropClick(e : MouseEvent ) {
156169 if (props .closeByClickOutside && e .target === e .currentTarget ) {
157- tryToHideModal ();
170+ tryToHideModal (true );
158171 }
159172}
160173
0 commit comments