Skip to content

Commit 45bc180

Browse files
authored
Make "cancel" buttons have proper type in modal forms (#25618)
Replace #25446, fix #25438 All "cancel" buttons which do not have "type" should not submit the form, should not be triggered by "Enter". This is a complete fix for all modal dialogs. The major change is "modules/aria/modal.js", "devtest" related code is for demo/test purpose.
1 parent 2aa6a78 commit 45bc180

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

routers/web/devtest/devtest.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func List(ctx *context.Context) {
3434

3535
func FetchActionTest(ctx *context.Context) {
3636
_ = ctx.Req.ParseForm()
37-
ctx.Flash.Info(ctx.Req.Method + " " + ctx.Req.RequestURI + "<br>" +
37+
ctx.Flash.Info("fetch-action: " + ctx.Req.Method + " " + ctx.Req.RequestURI + "<br>" +
3838
"Form: " + ctx.Req.Form.Encode() + "<br>" +
3939
"PostForm: " + ctx.Req.PostForm.Encode(),
4040
)
@@ -52,5 +52,15 @@ func Tmpl(ctx *context.Context) {
5252
ctx.Data["TimePast1y"] = now.Add(-1 * 366 * 86400 * time.Second)
5353
ctx.Data["TimeFuture1y"] = now.Add(1 * 366 * 86400 * time.Second)
5454

55+
if ctx.Req.Method == "POST" {
56+
_ = ctx.Req.ParseForm()
57+
ctx.Flash.Info("form: "+ctx.Req.Method+" "+ctx.Req.RequestURI+"<br>"+
58+
"Form: "+ctx.Req.Form.Encode()+"<br>"+
59+
"PostForm: "+ctx.Req.PostForm.Encode(),
60+
true,
61+
)
62+
time.Sleep(2 * time.Second)
63+
}
64+
5565
ctx.HTML(http.StatusOK, base.TplName("devtest"+path.Clean("/"+ctx.Params("sub"))))
5666
}

templates/devtest/fomantic-modal.tmpl

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
{{template "base/head" .}}
22
<div class="page-content devtest ui container">
3+
{{template "base/alert" .}}
4+
5+
<button class="show-modal" data-modal="#test-modal-form">show modal form</button>
6+
<div id="test-modal-form" class="ui mini modal">
7+
<div class="header">Form dialog</div>
8+
<form class="content" method="post">
9+
<div class="ui input"><input name="user_input"></div>
10+
{{template "base/modal_actions_confirm" (dict "locale" $.locale "ModalButtonTypes" "confirm")}}
11+
</form>
12+
</div>
13+
14+
<div class="divider"></div>
15+
316
<div class="ui g-modal-confirm modal" id="test-modal-default">
417
<div class="header">{{svg "octicon-file"}} Default dialog <span>title</span></div>
518
<div class="content">

web_src/js/modules/aria/modal.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import $ from 'jquery';
2+
3+
const fomanticModalFn = $.fn.modal;
4+
5+
// use our own `$.fn.modal` to patch Fomantic's modal module
6+
export function initAriaModalPatch() {
7+
if ($.fn.modal === ariaModalFn) throw new Error('initAriaModalPatch could only be called once');
8+
$.fn.modal = ariaModalFn;
9+
ariaModalFn.settings = fomanticModalFn.settings;
10+
}
11+
12+
// the patched `$.fn.modal` modal function
13+
// * it does the one-time attaching on the first call
14+
function ariaModalFn(...args) {
15+
const ret = fomanticModalFn.apply(this, args);
16+
if (args[0] === 'show' || args[0]?.autoShow) {
17+
for (const el of this) {
18+
// If there is a form in the modal, there might be a "cancel" button before "ok" button (all buttons are "type=submit" by default).
19+
// In such case, the "Enter" key will trigger the "cancel" button instead of "ok" button, then the dialog will be closed.
20+
// It breaks the user experience - the "Enter" key should confirm the dialog and submit the form.
21+
// So, all "cancel" buttons without "[type]" must be marked as "type=button".
22+
$(el).find('form button.cancel:not([type])').attr('type', 'button');
23+
}
24+
}
25+
return ret;
26+
}

web_src/js/modules/fomantic.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import $ from 'jquery';
22
import {initAriaCheckboxPatch} from './aria/checkbox.js';
33
import {initAriaDropdownPatch} from './aria/dropdown.js';
4+
import {initAriaModalPatch} from './aria/modal.js';
45
import {svg} from '../svg.js';
56

67
export const fomanticMobileScreen = window.matchMedia('only screen and (max-width: 767.98px)');
@@ -26,6 +27,7 @@ export function initGiteaFomantic() {
2627
// Use the patches to improve accessibility, these patches are designed to be as independent as possible, make it easy to modify or remove in the future.
2728
initAriaCheckboxPatch();
2829
initAriaDropdownPatch();
30+
initAriaModalPatch();
2931
}
3032

3133
function initFomanticApiPatch() {

0 commit comments

Comments
 (0)