@@ -9,8 +9,9 @@ const fomanticModalFn = $.fn.modal;
99export function initAriaModalPatch ( ) {
1010 if ( $ . fn . modal === ariaModalFn ) throw new Error ( 'initAriaModalPatch could only be called once' ) ;
1111 $ . fn . modal = ariaModalFn ;
12- $ . fn . fomanticExt . onModalBeforeHidden = onModalBeforeHidden ;
1312 ( ariaModalFn as FomanticInitFunction ) . settings = fomanticModalFn . settings ;
13+ $ . fn . fomanticExt . onModalBeforeHidden = onModalBeforeHidden ;
14+ $ . fn . modal . settings . onApprove = onModalApproveDefault ;
1415}
1516
1617// the patched `$.fn.modal` modal function
@@ -34,6 +35,29 @@ function ariaModalFn(this: any, ...args: Parameters<FomanticInitFunction>) {
3435function onModalBeforeHidden ( this : any ) {
3536 const $modal = $ ( this ) ;
3637 const elModal = $modal [ 0 ] ;
37- queryElems ( elModal , 'form' , ( form : HTMLFormElement ) => form . reset ( ) ) ;
3838 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 ;
3963}
0 commit comments