Skip to content

Commit 251675b

Browse files
committed
fix(pat modal): Fix close-panel with multiple inject forms.
Support close-panel with multiple forms.pat-inject in a modal, for example together with pat-stacks. Previously only the first form used to attach the event handler which listens for the injection success event for closing the modal. In these cases the modal wasn't closed properly.
1 parent 5945e60 commit 251675b

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/pat/modal/modal.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ export default Base.extend({
101101
if (document.activeElement) {
102102
document.activeElement.focus();
103103
}
104+
104105
this._init_handlers();
105106
this.resize();
106107
this.setPosition();
108+
107109
$("body").addClass("modal-active");
108110
this.el.dispatchEvent(
109111
new Event("pat-modal-ready", { bubbles: true, cancelable: true })
@@ -225,9 +227,10 @@ export default Base.extend({
225227
$("body").removeClass("modal-panel");
226228
},
227229

228-
destroy_inject() {
229-
const form = this.el.querySelector("form.pat-inject");
230-
if (form) {
230+
destroy_inject(e) {
231+
const button = e.target;
232+
const form = button.form;
233+
if (form && form.classList.contains("pat-inject")) {
231234
// if the modal contains a for mwith pat-inject, wait for injection
232235
// to be finished and then destroy the modal.
233236
const destroy_handler = () => {
@@ -241,8 +244,7 @@ export default Base.extend({
241244
destroy_handler.bind(this)
242245
);
243246
} else {
244-
// if working without injection, destroy after waiting a tick to let
245-
// eventually registered on-submit handlers kick in first.
247+
// if working without form injection, just destroy.
246248
this.destroy();
247249
}
248250
},

src/pat/modal/modal.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,44 @@ describe("pat-modal", function () {
206206
expect(document.querySelector("#target2").textContent).toBe("there");
207207
});
208208

209+
it("Submit form, do injection and close overlay with multiple forms.", async function () {
210+
await import("../inject/inject");
211+
const registry = (await import("../../core/registry")).default;
212+
213+
jest.spyOn($, "ajax").mockImplementation(() => deferred);
214+
answer(
215+
`<html><body><div id="source">hello.</div><div id="source2">there</div></body></html>`
216+
);
217+
218+
document.body.innerHTML = `
219+
<div class="pat-modal">
220+
<form
221+
class="form1 pat-inject"
222+
action="test.html"
223+
data-pat-inject="source: #source; target: #target">
224+
<button type="submit" class="close-panel">submit</button>
225+
</form>
226+
<form
227+
class="form2 pat-inject"
228+
action="test.html"
229+
data-pat-inject="source: #source; target: #target">
230+
<button type="submit" class="close-panel">submit</button>
231+
</form>
232+
</div>
233+
<div id="target">
234+
</div>
235+
`;
236+
registry.scan(document.body);
237+
await utils.timeout(1); // wait a tick for async to settle.
238+
239+
document.querySelector(".form2 button.close-panel[type=submit]").click();
240+
await utils.timeout(1); // wait a tick for pat-inject to settle.
241+
await utils.timeout(1); // wait a tick for pat-modal destroy to settle.
242+
243+
expect(document.querySelector(".pat-modal")).toBe(null);
244+
expect(document.querySelector("#target").textContent).toBe("hello.");
245+
});
246+
209247
it("Ensure destroy callback isn't called multiple times.", async function () {
210248
document.body.innerHTML = `
211249
<div id="pat-modal" class="pat-modal">

0 commit comments

Comments
 (0)