Skip to content

Commit fe9ee1e

Browse files
authored
Merge pull request #1193 from Patternslib/fix-pat-subform
Fix pat subform
2 parents 57c740a + bd19154 commit fe9ee1e

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

src/core/dom.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,12 @@ const element_uuid = (el) => {
569569
const find_form = (el) => {
570570
// Prefer input.form which allows for input outside form elements and fall
571571
// back to search for a parent form.
572-
return (
572+
const form =
573+
el.closest(".pat-subform") || // Special Patternslib subform concept has precedence.
573574
el.form ||
574575
el.querySelector("input, select, textarea, button")?.form ||
575-
el.closest("form")
576-
);
576+
el.closest("form");
577+
return form;
577578
};
578579

579580
const dom = {

src/core/dom.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -1002,4 +1002,18 @@ describe("find_form", function () {
10021002
const form = document.querySelector("#the-form");
10031003
expect(dom.find_form(el)).toBe(form);
10041004
});
1005+
1006+
it("example 9 - subform support", function () {
1007+
// Subform support. Subforms have precedence over forms.
1008+
document.body.innerHTML = `
1009+
<form>
1010+
<div class="pat-subform">
1011+
<button></button>
1012+
</div>
1013+
</form>
1014+
`;
1015+
const el = document.querySelector("button");
1016+
const subform = document.querySelector(".pat-subform");
1017+
expect(dom.find_form(el)).toBe(subform);
1018+
});
10051019
});

src/pat/auto-submit/auto-submit.test.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe("pat-autosubmit", function () {
187187
expect(submit_form_dispatched).toBe(true);
188188
});
189189

190-
it("2.6 - when pat-autosubmit is defined not on aform element", async function () {
190+
it("2.6 - when pat-autosubmit is defined not on a form element", async function () {
191191
document.body.innerHTML = `
192192
<form>
193193
<div
@@ -211,6 +211,40 @@ describe("pat-autosubmit", function () {
211211
await utils.timeout(1);
212212
expect(submit_form_dispatched).toBe(true);
213213
});
214+
215+
it("2.7 - directly on a pat-subform", async function () {
216+
document.body.innerHTML = `
217+
<form>
218+
<div class="pat-subform">
219+
<input
220+
class="pat-autosubmit"
221+
data-pat-autosubmit="delay: 0"
222+
name="q">
223+
</div>
224+
</form>
225+
`;
226+
const input = document.querySelector("input");
227+
const subform = document.querySelector(".pat-subform");
228+
const autosubmit = document.querySelector(".pat-autosubmit");
229+
new Pattern(autosubmit);
230+
231+
// The submit event should be invoked on the subform.
232+
let submit_subform_dispatched = false;
233+
subform.addEventListener("submit", () => {
234+
submit_subform_dispatched = true;
235+
});
236+
237+
// The submit event should also bubble up to the form.
238+
let submit_form_dispatched = false;
239+
document.querySelector("form").addEventListener("submit", () => {
240+
submit_form_dispatched = true;
241+
});
242+
243+
input.dispatchEvent(events.input_event());
244+
await utils.timeout(1);
245+
expect(submit_subform_dispatched).toBe(true);
246+
expect(submit_form_dispatched).toBe(true);
247+
});
214248
});
215249

216250
describe("3 - Parsing of the delay option", function () {

src/pat/subform/subform.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ export default Base.extend({
2828
},
2929

3030
destroy($el) {
31-
$el.off("submit");
31+
events.remove_event_listener($el[0], "pat-subform--submit");
3232
},
3333

3434
scopedSubmit($el) {
35-
var $form = $el.parents("form"),
36-
$exclude = $form.find(":input").filter(function () {
37-
return !$(this).is($el.find("*"));
38-
});
35+
const $form = $el.parents("form");
36+
const $exclude = $form.find(":input").filter(function () {
37+
return !$(this).is($el.find("*"));
38+
});
3939
// make other controls "unsuccessful"
4040
log.debug("Hiding unwanted elements from submission.");
41-
var names = $exclude.map(function () {
42-
var name = $(this).attr("name");
41+
const names = $exclude.map(function () {
42+
const name = $(this).attr("name");
4343
return name ? name : 0;
4444
});
4545
$exclude.each(function () {
@@ -63,8 +63,8 @@ export default Base.extend({
6363

6464
submit(ev) {
6565
ev.stopPropagation();
66-
var $this = $(ev.target),
67-
$button = $this.find("button[type=submit][formaction]").first();
66+
const $this = $(ev.target);
67+
const $button = $this.find("button[type=submit][formaction]").first();
6868
if ($button.length) {
6969
$button.trigger("click");
7070
} else {
@@ -78,7 +78,7 @@ export default Base.extend({
7878
if (ev.keyCode != 13) {
7979
return;
8080
}
81-
var $subform = $(ev.target).parents(".pat-subform");
81+
const $subform = $(ev.target).parents(".pat-subform");
8282
if (!$subform.is(".pat-autosubmit")) {
8383
return;
8484
}
@@ -90,14 +90,14 @@ export default Base.extend({
9090
ev.stopPropagation();
9191
ajax.onClickSubmit(ev); // make sure the submitting button is sent with the form
9292

93-
var $button = $(ev.target),
94-
$sub = $button.parents(".pat-subform").first(),
95-
formaction = $button.attr("formaction");
93+
const $button = $(ev.target);
94+
const $sub = $button.parents(".pat-subform").first();
95+
const formaction = $button.attr("formaction");
9696

9797
if (formaction) {
9898
// override the default action and restore afterwards
9999
if ($sub.is(".pat-inject")) {
100-
var previousValue = $sub.data("pat-inject");
100+
const previousValue = $sub.data("pat-inject");
101101
$sub.data("pat-inject", inject.extractConfig($sub, { url: formaction }));
102102
this.scopedSubmit($sub);
103103
$sub.data("pat-inject", previousValue);

0 commit comments

Comments
 (0)