Skip to content

Commit 2946779

Browse files
committed
Ensure any form submissions are interception
1 parent dcf14cf commit 2946779

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/util/hyperwave.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const setupEventHandlers = (triggerElement) => {
204204

205205
/**
206206
* Attaches hyperwave event handlers to all elements within a given root element.
207+
* Ensures any form submissions with hyperwave'd elements are intercepted.
207208
* @param {HTMLElement} rootElement - The root element to search for trigger elements.
208209
*/
209210
const attachHyperwaveHandlers = (rootElement) => {
@@ -229,4 +230,26 @@ document.addEventListener("DOMContentLoaded", () => {
229230
});
230231

231232
observer.observe(document.body, { childList: true, subtree: true });
233+
234+
const forms = document.querySelectorAll("form[href]");
235+
forms.forEach((form) => {
236+
form.addEventListener("submit", (event) => {
237+
event.preventDefault();
238+
log("log", "Form submission intercepted", form);
239+
240+
const fetchOptions = {
241+
method: form.getAttribute("method") || hyperwaveConfig.defaultMethod,
242+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
243+
body: new URLSearchParams(new FormData(form)).toString(),
244+
};
245+
246+
const url = new URL(form.getAttribute("href"), window.location.origin);
247+
fetchContent(url.toString(), fetchOptions).then((content) => {
248+
const target = document.querySelector(
249+
form.getAttribute("target") || "#trip-details",
250+
);
251+
updateTargetElement(target, content, "replace");
252+
});
253+
});
254+
});
232255
});

0 commit comments

Comments
 (0)