@@ -9,8 +9,9 @@ const fomanticModalFn = $.fn.modal;
9
9
export function initAriaModalPatch ( ) {
10
10
if ( $ . fn . modal === ariaModalFn ) throw new Error ( 'initAriaModalPatch could only be called once' ) ;
11
11
$ . fn . modal = ariaModalFn ;
12
- $ . fn . fomanticExt . onModalBeforeHidden = onModalBeforeHidden ;
13
12
( ariaModalFn as FomanticInitFunction ) . settings = fomanticModalFn . settings ;
13
+ $ . fn . fomanticExt . onModalBeforeHidden = onModalBeforeHidden ;
14
+ $ . fn . modal . settings . onApprove = onModalApproveDefault ;
14
15
}
15
16
16
17
// the patched `$.fn.modal` modal function
@@ -34,6 +35,29 @@ function ariaModalFn(this: any, ...args: Parameters<FomanticInitFunction>) {
34
35
function onModalBeforeHidden ( this : any ) {
35
36
const $modal = $ ( this ) ;
36
37
const elModal = $modal [ 0 ] ;
37
- queryElems ( elModal , 'form' , ( form : HTMLFormElement ) => form . reset ( ) ) ;
38
38
hideToastsFrom ( elModal . closest ( '.ui.dimmer' ) ?? document . body ) ;
39
+
40
+ // reset the form after the modal is hidden, after other modal events and handlers (e.g. "onApprove", form submit)
41
+ setTimeout ( ( ) => {
42
+ queryElems ( elModal , 'form' , ( form : HTMLFormElement ) => form . reset ( ) ) ;
43
+ } , 0 ) ;
44
+ }
45
+
46
+ function onModalApproveDefault ( this : any ) {
47
+ const $modal = $ ( this ) ;
48
+ const selectors = $modal . modal ( 'setting' , 'selector' ) ;
49
+ const elModal = $modal [ 0 ] ;
50
+ const elApprove = elModal . querySelector ( selectors . approve ) ;
51
+ const elForm = elApprove ?. closest ( 'form' ) ;
52
+ if ( ! elForm ) return true ; // no form, just allow closing the modal
53
+
54
+ // "form-fetch-action" can handle network errors gracefully,
55
+ // so keep the modal dialog to make users can re-submit the form if anything wrong happens.
56
+ if ( elForm . matches ( '.form-fetch-action' ) ) return false ;
57
+
58
+ // There is an abuse for the "modal" + "form" combination, the "Approve" button is a traditional form submit button in the form.
59
+ // Then "approve" and "submit" occur at the same time, the modal will be closed immediately before the form is submitted.
60
+ // So here we prevent the modal from closing automatically by returning false, add the "is-loading" class to the form element.
61
+ elForm . classList . add ( 'is-loading' ) ;
62
+ return false ;
39
63
}
0 commit comments