feat: add support for partials on forms (#1930)
This commit is contained in:
@@ -2,7 +2,8 @@ let ws: WebSocket;
|
|||||||
|
|
||||||
let reconnectTimer: number;
|
let reconnectTimer: number;
|
||||||
const backoff = [
|
const backoff = [
|
||||||
0,
|
// Wait 100ms initially, because we could also be
|
||||||
|
// disconnected because of a form submit.
|
||||||
100,
|
100,
|
||||||
150,
|
150,
|
||||||
200,
|
200,
|
||||||
@@ -50,6 +51,11 @@ function connect(forceReload?: boolean) {
|
|||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
addEventListener("unload", () => {
|
||||||
|
// Prevent form submits from triggering a reconnect
|
||||||
|
clearTimeout(reconnectTimer);
|
||||||
|
});
|
||||||
|
|
||||||
ws.addEventListener("open", () => {
|
ws.addEventListener("open", () => {
|
||||||
if (forceReload) {
|
if (forceReload) {
|
||||||
location.reload();
|
location.reload();
|
||||||
|
|||||||
@@ -977,7 +977,10 @@ document.addEventListener("click", async (e) => {
|
|||||||
button = button.closest("button");
|
button = button.closest("button");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (button !== null && button instanceof HTMLButtonElement) {
|
if (
|
||||||
|
button !== null && button instanceof HTMLButtonElement &&
|
||||||
|
(button.type !== "submit" || button.form === null)
|
||||||
|
) {
|
||||||
const partial = button.getAttribute(PARTIAL_ATTR);
|
const partial = button.getAttribute(PARTIAL_ATTR);
|
||||||
|
|
||||||
// Check if the user opted out of client side navigation.
|
// Check if the user opted out of client side navigation.
|
||||||
@@ -1049,12 +1052,47 @@ addEventListener("popstate", async (e) => {
|
|||||||
document.addEventListener("submit", async (e) => {
|
document.addEventListener("submit", async (e) => {
|
||||||
const el = e.target;
|
const el = e.target;
|
||||||
if (el !== null && el instanceof HTMLFormElement && !e.defaultPrevented) {
|
if (el !== null && el instanceof HTMLFormElement && !e.defaultPrevented) {
|
||||||
const partial = el.getAttribute(PARTIAL_ATTR);
|
if (
|
||||||
if (partial !== null) {
|
// Check if form has client nav enabled
|
||||||
|
!checkClientNavEnabled(el) ||
|
||||||
|
// Bail out if submitter is set and client nav is disabled
|
||||||
|
(e.submitter !== null && !checkClientNavEnabled(e.submitter))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lowerMethod =
|
||||||
|
e.submitter?.getAttribute("formmethod")?.toLowerCase() ??
|
||||||
|
el.method.toLowerCase();
|
||||||
|
if (
|
||||||
|
lowerMethod !== "get" && lowerMethod !== "post" &&
|
||||||
|
lowerMethod !== "dialog"
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const action = e.submitter?.getAttribute(PARTIAL_ATTR) ??
|
||||||
|
e.submitter?.getAttribute("formaction") ??
|
||||||
|
el.getAttribute(PARTIAL_ATTR) ?? el.action;
|
||||||
|
|
||||||
|
if (action !== "") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const url = new URL(partial, location.href);
|
const url = new URL(action, location.href);
|
||||||
await fetchPartials(url);
|
|
||||||
|
let init: RequestInit | undefined;
|
||||||
|
|
||||||
|
// GET method appends form data via url search params
|
||||||
|
if (lowerMethod === "get") {
|
||||||
|
// TODO: Looks like constructor type for URLSearchParam is wrong
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
const qs = new URLSearchParams(new FormData(el) as any);
|
||||||
|
qs.forEach((value, key) => url.searchParams.set(key, value));
|
||||||
|
} else {
|
||||||
|
init = { body: new FormData(el), method: lowerMethod };
|
||||||
|
}
|
||||||
|
|
||||||
|
await fetchPartials(url, init);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+128
-118
@@ -37,65 +37,70 @@ import * as $31 from "./routes/deep_partial/update.tsx";
|
|||||||
import * as $32 from "./routes/form/index.tsx";
|
import * as $32 from "./routes/form/index.tsx";
|
||||||
import * as $33 from "./routes/form/injected.tsx";
|
import * as $33 from "./routes/form/injected.tsx";
|
||||||
import * as $34 from "./routes/form/update.tsx";
|
import * as $34 from "./routes/form/update.tsx";
|
||||||
import * as $35 from "./routes/fragment_nav.tsx";
|
import * as $35 from "./routes/form_get/index.tsx";
|
||||||
import * as $36 from "./routes/fragment_nav_scroll.tsx";
|
import * as $36 from "./routes/form_post/index.tsx";
|
||||||
import * as $37 from "./routes/head_merge/duplicate.tsx";
|
import * as $37 from "./routes/form_submitter/index.tsx";
|
||||||
import * as $38 from "./routes/head_merge/index.tsx";
|
import * as $38 from "./routes/form_submitter_partial/index.tsx";
|
||||||
import * as $39 from "./routes/head_merge/injected.tsx";
|
import * as $39 from "./routes/form_submitter_partial_no_client_nav/index.tsx";
|
||||||
import * as $40 from "./routes/head_merge/update.tsx";
|
import * as $40 from "./routes/fragment_nav.tsx";
|
||||||
import * as $41 from "./routes/index.tsx";
|
import * as $41 from "./routes/fragment_nav_scroll.tsx";
|
||||||
import * as $42 from "./routes/island_instance/index.tsx";
|
import * as $42 from "./routes/head_merge/duplicate.tsx";
|
||||||
import * as $43 from "./routes/island_instance/injected.tsx";
|
import * as $43 from "./routes/head_merge/index.tsx";
|
||||||
import * as $44 from "./routes/island_instance/partial.tsx";
|
import * as $44 from "./routes/head_merge/injected.tsx";
|
||||||
import * as $45 from "./routes/island_instance/partial_remove.tsx";
|
import * as $45 from "./routes/head_merge/update.tsx";
|
||||||
import * as $46 from "./routes/island_instance/partial_replace.tsx";
|
import * as $46 from "./routes/index.tsx";
|
||||||
import * as $47 from "./routes/island_instance_multiple/index.tsx";
|
import * as $47 from "./routes/island_instance/index.tsx";
|
||||||
import * as $48 from "./routes/island_instance_multiple/injected.tsx";
|
import * as $48 from "./routes/island_instance/injected.tsx";
|
||||||
import * as $49 from "./routes/island_instance_multiple/partial.tsx";
|
import * as $49 from "./routes/island_instance/partial.tsx";
|
||||||
import * as $50 from "./routes/island_instance_multiple/partial_both.tsx";
|
import * as $50 from "./routes/island_instance/partial_remove.tsx";
|
||||||
import * as $51 from "./routes/island_instance_nested/index.tsx";
|
import * as $51 from "./routes/island_instance/partial_replace.tsx";
|
||||||
import * as $52 from "./routes/island_instance_nested/injected.tsx";
|
import * as $52 from "./routes/island_instance_multiple/index.tsx";
|
||||||
import * as $53 from "./routes/island_instance_nested/partial.tsx";
|
import * as $53 from "./routes/island_instance_multiple/injected.tsx";
|
||||||
import * as $54 from "./routes/island_instance_nested/replace.tsx";
|
import * as $54 from "./routes/island_instance_multiple/partial.tsx";
|
||||||
import * as $55 from "./routes/island_props/index.tsx";
|
import * as $55 from "./routes/island_instance_multiple/partial_both.tsx";
|
||||||
import * as $56 from "./routes/island_props/injected.tsx";
|
import * as $56 from "./routes/island_instance_nested/index.tsx";
|
||||||
import * as $57 from "./routes/island_props/partial.tsx";
|
import * as $57 from "./routes/island_instance_nested/injected.tsx";
|
||||||
import * as $58 from "./routes/island_props_signals/index.tsx";
|
import * as $58 from "./routes/island_instance_nested/partial.tsx";
|
||||||
import * as $59 from "./routes/island_props_signals/injected.tsx";
|
import * as $59 from "./routes/island_instance_nested/replace.tsx";
|
||||||
import * as $60 from "./routes/island_props_signals/partial.tsx";
|
import * as $60 from "./routes/island_props/index.tsx";
|
||||||
import * as $61 from "./routes/keys/index.tsx";
|
import * as $61 from "./routes/island_props/injected.tsx";
|
||||||
import * as $62 from "./routes/keys/injected.tsx";
|
import * as $62 from "./routes/island_props/partial.tsx";
|
||||||
import * as $63 from "./routes/keys/swap.tsx";
|
import * as $63 from "./routes/island_props_signals/index.tsx";
|
||||||
import * as $64 from "./routes/keys_components/index.tsx";
|
import * as $64 from "./routes/island_props_signals/injected.tsx";
|
||||||
import * as $65 from "./routes/keys_components/injected.tsx";
|
import * as $65 from "./routes/island_props_signals/partial.tsx";
|
||||||
import * as $66 from "./routes/keys_components/swap.tsx";
|
import * as $66 from "./routes/keys/index.tsx";
|
||||||
import * as $67 from "./routes/keys_confusion/index.tsx";
|
import * as $67 from "./routes/keys/injected.tsx";
|
||||||
import * as $68 from "./routes/keys_dom/index.tsx";
|
import * as $68 from "./routes/keys/swap.tsx";
|
||||||
import * as $69 from "./routes/keys_dom/injected.tsx";
|
import * as $69 from "./routes/keys_components/index.tsx";
|
||||||
import * as $70 from "./routes/keys_dom/swap.tsx";
|
import * as $70 from "./routes/keys_components/injected.tsx";
|
||||||
import * as $71 from "./routes/keys_outside/index.tsx";
|
import * as $71 from "./routes/keys_components/swap.tsx";
|
||||||
import * as $72 from "./routes/loading/index.tsx";
|
import * as $72 from "./routes/keys_confusion/index.tsx";
|
||||||
import * as $73 from "./routes/loading/injected.tsx";
|
import * as $73 from "./routes/keys_dom/index.tsx";
|
||||||
import * as $74 from "./routes/loading/update.tsx";
|
import * as $74 from "./routes/keys_dom/injected.tsx";
|
||||||
import * as $75 from "./routes/missing_partial/index.tsx";
|
import * as $75 from "./routes/keys_dom/swap.tsx";
|
||||||
import * as $76 from "./routes/missing_partial/injected.tsx";
|
import * as $76 from "./routes/keys_outside/index.tsx";
|
||||||
import * as $77 from "./routes/missing_partial/update.tsx";
|
import * as $77 from "./routes/loading/index.tsx";
|
||||||
import * as $78 from "./routes/mode/append.tsx";
|
import * as $78 from "./routes/loading/injected.tsx";
|
||||||
import * as $79 from "./routes/mode/index.tsx";
|
import * as $79 from "./routes/loading/update.tsx";
|
||||||
import * as $80 from "./routes/mode/injected.tsx";
|
import * as $80 from "./routes/missing_partial/index.tsx";
|
||||||
import * as $81 from "./routes/mode/prepend.tsx";
|
import * as $81 from "./routes/missing_partial/injected.tsx";
|
||||||
import * as $82 from "./routes/mode/replace.tsx";
|
import * as $82 from "./routes/missing_partial/update.tsx";
|
||||||
import * as $83 from "./routes/no_islands/index.tsx";
|
import * as $83 from "./routes/mode/append.tsx";
|
||||||
import * as $84 from "./routes/no_islands/injected.tsx";
|
import * as $84 from "./routes/mode/index.tsx";
|
||||||
import * as $85 from "./routes/no_islands/update.tsx";
|
import * as $85 from "./routes/mode/injected.tsx";
|
||||||
import * as $86 from "./routes/no_partial_response/index.tsx";
|
import * as $86 from "./routes/mode/prepend.tsx";
|
||||||
import * as $87 from "./routes/no_partial_response/injected.tsx";
|
import * as $87 from "./routes/mode/replace.tsx";
|
||||||
import * as $88 from "./routes/no_partial_response/update.tsx";
|
import * as $88 from "./routes/no_islands/index.tsx";
|
||||||
import * as $89 from "./routes/partial_slot_inside_island.tsx";
|
import * as $89 from "./routes/no_islands/injected.tsx";
|
||||||
import * as $90 from "./routes/relative_link/index.tsx";
|
import * as $90 from "./routes/no_islands/update.tsx";
|
||||||
import * as $91 from "./routes/scroll_restoration/index.tsx";
|
import * as $91 from "./routes/no_partial_response/index.tsx";
|
||||||
import * as $92 from "./routes/scroll_restoration/injected.tsx";
|
import * as $92 from "./routes/no_partial_response/injected.tsx";
|
||||||
import * as $93 from "./routes/scroll_restoration/update.tsx";
|
import * as $93 from "./routes/no_partial_response/update.tsx";
|
||||||
|
import * as $94 from "./routes/partial_slot_inside_island.tsx";
|
||||||
|
import * as $95 from "./routes/relative_link/index.tsx";
|
||||||
|
import * as $96 from "./routes/scroll_restoration/index.tsx";
|
||||||
|
import * as $97 from "./routes/scroll_restoration/injected.tsx";
|
||||||
|
import * as $98 from "./routes/scroll_restoration/update.tsx";
|
||||||
import * as $$0 from "./islands/Counter.tsx";
|
import * as $$0 from "./islands/Counter.tsx";
|
||||||
import * as $$1 from "./islands/CounterA.tsx";
|
import * as $$1 from "./islands/CounterA.tsx";
|
||||||
import * as $$2 from "./islands/CounterB.tsx";
|
import * as $$2 from "./islands/CounterB.tsx";
|
||||||
@@ -149,65 +154,70 @@ const manifest = {
|
|||||||
"./routes/form/index.tsx": $32,
|
"./routes/form/index.tsx": $32,
|
||||||
"./routes/form/injected.tsx": $33,
|
"./routes/form/injected.tsx": $33,
|
||||||
"./routes/form/update.tsx": $34,
|
"./routes/form/update.tsx": $34,
|
||||||
"./routes/fragment_nav.tsx": $35,
|
"./routes/form_get/index.tsx": $35,
|
||||||
"./routes/fragment_nav_scroll.tsx": $36,
|
"./routes/form_post/index.tsx": $36,
|
||||||
"./routes/head_merge/duplicate.tsx": $37,
|
"./routes/form_submitter/index.tsx": $37,
|
||||||
"./routes/head_merge/index.tsx": $38,
|
"./routes/form_submitter_partial/index.tsx": $38,
|
||||||
"./routes/head_merge/injected.tsx": $39,
|
"./routes/form_submitter_partial_no_client_nav/index.tsx": $39,
|
||||||
"./routes/head_merge/update.tsx": $40,
|
"./routes/fragment_nav.tsx": $40,
|
||||||
"./routes/index.tsx": $41,
|
"./routes/fragment_nav_scroll.tsx": $41,
|
||||||
"./routes/island_instance/index.tsx": $42,
|
"./routes/head_merge/duplicate.tsx": $42,
|
||||||
"./routes/island_instance/injected.tsx": $43,
|
"./routes/head_merge/index.tsx": $43,
|
||||||
"./routes/island_instance/partial.tsx": $44,
|
"./routes/head_merge/injected.tsx": $44,
|
||||||
"./routes/island_instance/partial_remove.tsx": $45,
|
"./routes/head_merge/update.tsx": $45,
|
||||||
"./routes/island_instance/partial_replace.tsx": $46,
|
"./routes/index.tsx": $46,
|
||||||
"./routes/island_instance_multiple/index.tsx": $47,
|
"./routes/island_instance/index.tsx": $47,
|
||||||
"./routes/island_instance_multiple/injected.tsx": $48,
|
"./routes/island_instance/injected.tsx": $48,
|
||||||
"./routes/island_instance_multiple/partial.tsx": $49,
|
"./routes/island_instance/partial.tsx": $49,
|
||||||
"./routes/island_instance_multiple/partial_both.tsx": $50,
|
"./routes/island_instance/partial_remove.tsx": $50,
|
||||||
"./routes/island_instance_nested/index.tsx": $51,
|
"./routes/island_instance/partial_replace.tsx": $51,
|
||||||
"./routes/island_instance_nested/injected.tsx": $52,
|
"./routes/island_instance_multiple/index.tsx": $52,
|
||||||
"./routes/island_instance_nested/partial.tsx": $53,
|
"./routes/island_instance_multiple/injected.tsx": $53,
|
||||||
"./routes/island_instance_nested/replace.tsx": $54,
|
"./routes/island_instance_multiple/partial.tsx": $54,
|
||||||
"./routes/island_props/index.tsx": $55,
|
"./routes/island_instance_multiple/partial_both.tsx": $55,
|
||||||
"./routes/island_props/injected.tsx": $56,
|
"./routes/island_instance_nested/index.tsx": $56,
|
||||||
"./routes/island_props/partial.tsx": $57,
|
"./routes/island_instance_nested/injected.tsx": $57,
|
||||||
"./routes/island_props_signals/index.tsx": $58,
|
"./routes/island_instance_nested/partial.tsx": $58,
|
||||||
"./routes/island_props_signals/injected.tsx": $59,
|
"./routes/island_instance_nested/replace.tsx": $59,
|
||||||
"./routes/island_props_signals/partial.tsx": $60,
|
"./routes/island_props/index.tsx": $60,
|
||||||
"./routes/keys/index.tsx": $61,
|
"./routes/island_props/injected.tsx": $61,
|
||||||
"./routes/keys/injected.tsx": $62,
|
"./routes/island_props/partial.tsx": $62,
|
||||||
"./routes/keys/swap.tsx": $63,
|
"./routes/island_props_signals/index.tsx": $63,
|
||||||
"./routes/keys_components/index.tsx": $64,
|
"./routes/island_props_signals/injected.tsx": $64,
|
||||||
"./routes/keys_components/injected.tsx": $65,
|
"./routes/island_props_signals/partial.tsx": $65,
|
||||||
"./routes/keys_components/swap.tsx": $66,
|
"./routes/keys/index.tsx": $66,
|
||||||
"./routes/keys_confusion/index.tsx": $67,
|
"./routes/keys/injected.tsx": $67,
|
||||||
"./routes/keys_dom/index.tsx": $68,
|
"./routes/keys/swap.tsx": $68,
|
||||||
"./routes/keys_dom/injected.tsx": $69,
|
"./routes/keys_components/index.tsx": $69,
|
||||||
"./routes/keys_dom/swap.tsx": $70,
|
"./routes/keys_components/injected.tsx": $70,
|
||||||
"./routes/keys_outside/index.tsx": $71,
|
"./routes/keys_components/swap.tsx": $71,
|
||||||
"./routes/loading/index.tsx": $72,
|
"./routes/keys_confusion/index.tsx": $72,
|
||||||
"./routes/loading/injected.tsx": $73,
|
"./routes/keys_dom/index.tsx": $73,
|
||||||
"./routes/loading/update.tsx": $74,
|
"./routes/keys_dom/injected.tsx": $74,
|
||||||
"./routes/missing_partial/index.tsx": $75,
|
"./routes/keys_dom/swap.tsx": $75,
|
||||||
"./routes/missing_partial/injected.tsx": $76,
|
"./routes/keys_outside/index.tsx": $76,
|
||||||
"./routes/missing_partial/update.tsx": $77,
|
"./routes/loading/index.tsx": $77,
|
||||||
"./routes/mode/append.tsx": $78,
|
"./routes/loading/injected.tsx": $78,
|
||||||
"./routes/mode/index.tsx": $79,
|
"./routes/loading/update.tsx": $79,
|
||||||
"./routes/mode/injected.tsx": $80,
|
"./routes/missing_partial/index.tsx": $80,
|
||||||
"./routes/mode/prepend.tsx": $81,
|
"./routes/missing_partial/injected.tsx": $81,
|
||||||
"./routes/mode/replace.tsx": $82,
|
"./routes/missing_partial/update.tsx": $82,
|
||||||
"./routes/no_islands/index.tsx": $83,
|
"./routes/mode/append.tsx": $83,
|
||||||
"./routes/no_islands/injected.tsx": $84,
|
"./routes/mode/index.tsx": $84,
|
||||||
"./routes/no_islands/update.tsx": $85,
|
"./routes/mode/injected.tsx": $85,
|
||||||
"./routes/no_partial_response/index.tsx": $86,
|
"./routes/mode/prepend.tsx": $86,
|
||||||
"./routes/no_partial_response/injected.tsx": $87,
|
"./routes/mode/replace.tsx": $87,
|
||||||
"./routes/no_partial_response/update.tsx": $88,
|
"./routes/no_islands/index.tsx": $88,
|
||||||
"./routes/partial_slot_inside_island.tsx": $89,
|
"./routes/no_islands/injected.tsx": $89,
|
||||||
"./routes/relative_link/index.tsx": $90,
|
"./routes/no_islands/update.tsx": $90,
|
||||||
"./routes/scroll_restoration/index.tsx": $91,
|
"./routes/no_partial_response/index.tsx": $91,
|
||||||
"./routes/scroll_restoration/injected.tsx": $92,
|
"./routes/no_partial_response/injected.tsx": $92,
|
||||||
"./routes/scroll_restoration/update.tsx": $93,
|
"./routes/no_partial_response/update.tsx": $93,
|
||||||
|
"./routes/partial_slot_inside_island.tsx": $94,
|
||||||
|
"./routes/relative_link/index.tsx": $95,
|
||||||
|
"./routes/scroll_restoration/index.tsx": $96,
|
||||||
|
"./routes/scroll_restoration/injected.tsx": $97,
|
||||||
|
"./routes/scroll_restoration/update.tsx": $98,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Counter.tsx": $$0,
|
"./islands/Counter.tsx": $$0,
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
|
import { PageProps } from "$fresh/server.ts";
|
||||||
|
import { Logger } from "../../islands/Logger.tsx";
|
||||||
|
|
||||||
|
export default function SlotDemo(props: PageProps) {
|
||||||
|
let value = props.url.searchParams.get("name") ?? "";
|
||||||
|
|
||||||
|
value += value ? "_foo" : "";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form action="/form_get">
|
||||||
|
<Partial name="slot-1">
|
||||||
|
<p class="status">Default content</p>
|
||||||
|
<p>
|
||||||
|
<input type="text" value={value} name="name" />
|
||||||
|
</p>
|
||||||
|
<Logger name="Form" />
|
||||||
|
<p class="url">{props.url.toString()}</p>
|
||||||
|
</Partial>
|
||||||
|
<button type="submit" class="submit">
|
||||||
|
submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<pre id="logs" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
|
import { defineRoute, Handlers } from "$fresh/server.ts";
|
||||||
|
import { Logger } from "../../islands/Logger.tsx";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
POST(req, ctx) {
|
||||||
|
return ctx.render();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineRoute(async (req, ctx) => {
|
||||||
|
let value = "";
|
||||||
|
|
||||||
|
if (req.body !== null) {
|
||||||
|
const data = await req.formData();
|
||||||
|
value += data.has("name") ? data.get("name") + "_foo" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form action="/form_post" method="post">
|
||||||
|
<Partial name="slot-1">
|
||||||
|
<p class="status">Default content</p>
|
||||||
|
<p>
|
||||||
|
<input type="text" value={value} name="name" />
|
||||||
|
</p>
|
||||||
|
<Logger name="Form" />
|
||||||
|
<p class="url">{ctx.url.toString()}</p>
|
||||||
|
</Partial>
|
||||||
|
<button type="submit" class="submit">
|
||||||
|
submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<pre id="logs" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
|
import { defineRoute, Handlers } from "$fresh/server.ts";
|
||||||
|
import { Logger } from "../../islands/Logger.tsx";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
POST(req, ctx) {
|
||||||
|
return ctx.render();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineRoute(async (req, ctx) => {
|
||||||
|
let value = "";
|
||||||
|
|
||||||
|
if (req.body !== null) {
|
||||||
|
const data = await req.formData();
|
||||||
|
value += data.has("name") ? data.get("name") + "_foo" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form id="foo">
|
||||||
|
<Partial name="slot-1">
|
||||||
|
<p class="status">Default content</p>
|
||||||
|
<p>
|
||||||
|
<input type="text" value={value} name="name" />
|
||||||
|
</p>
|
||||||
|
<Logger name="Form" />
|
||||||
|
<p class="url">{ctx.url.toString()}</p>
|
||||||
|
</Partial>
|
||||||
|
</form>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="submit"
|
||||||
|
form="foo"
|
||||||
|
formaction="/form_submitter"
|
||||||
|
formmethod="POST"
|
||||||
|
>
|
||||||
|
submit
|
||||||
|
</button>
|
||||||
|
<pre id="logs" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
|
import { defineRoute, Handlers } from "$fresh/server.ts";
|
||||||
|
import { Logger } from "../../islands/Logger.tsx";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
POST(req, ctx) {
|
||||||
|
return ctx.render();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineRoute(async (req, ctx) => {
|
||||||
|
let value = "";
|
||||||
|
|
||||||
|
if (req.body !== null) {
|
||||||
|
const data = await req.formData();
|
||||||
|
value += data.has("name") ? data.get("name") + "_foo" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form id="foo">
|
||||||
|
<Partial name="slot-1">
|
||||||
|
<p class="status">Default content</p>
|
||||||
|
<p>
|
||||||
|
<input type="text" value={value} name="name" />
|
||||||
|
</p>
|
||||||
|
<Logger name="Form" />
|
||||||
|
<p class="url">{ctx.url.toString()}</p>
|
||||||
|
</Partial>
|
||||||
|
</form>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="submit"
|
||||||
|
form="foo"
|
||||||
|
formaction="/form_submitter_partial_invalid"
|
||||||
|
f-partial="/form_submitter_partial"
|
||||||
|
formmethod="POST"
|
||||||
|
>
|
||||||
|
submit
|
||||||
|
</button>
|
||||||
|
<pre id="logs" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
|
import { defineRoute, Handlers } from "$fresh/server.ts";
|
||||||
|
import { Logger } from "../../islands/Logger.tsx";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
POST(req, ctx) {
|
||||||
|
return ctx.render();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineRoute(async (req, ctx) => {
|
||||||
|
let value = "";
|
||||||
|
|
||||||
|
if (req.body !== null) {
|
||||||
|
const data = await req.formData();
|
||||||
|
value += data.has("name") ? data.get("name") + "_foo" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<form
|
||||||
|
id="foo"
|
||||||
|
action="/form_submitter_partial_no_client_nav"
|
||||||
|
method="post"
|
||||||
|
>
|
||||||
|
<Partial name="slot-1">
|
||||||
|
<p class="status">Default content</p>
|
||||||
|
<p>
|
||||||
|
<input type="text" value={value} name="name" />
|
||||||
|
</p>
|
||||||
|
<Logger name="Form" />
|
||||||
|
<p class="url">{ctx.url.toString()}</p>
|
||||||
|
</Partial>
|
||||||
|
</form>
|
||||||
|
<div f-client-nav={false}>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="submit"
|
||||||
|
form="foo"
|
||||||
|
>
|
||||||
|
submit
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<pre id="logs" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -1002,6 +1002,165 @@ Deno.test("submit form", async () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("submit form GET", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_partials/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
const initialUrl = `${address}/form_get`;
|
||||||
|
await page.goto(initialUrl);
|
||||||
|
await page.waitForSelector(".status");
|
||||||
|
|
||||||
|
await page.type("input", "foobar");
|
||||||
|
|
||||||
|
await page.click(".submit");
|
||||||
|
await waitFor(async () => {
|
||||||
|
const logEl = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
return /update Form/.test(logEl);
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = await page.$eval(".url", (el) => el.textContent);
|
||||||
|
assertEquals(url, `${address}/form_get?name=foobar&fresh-partial=true`);
|
||||||
|
|
||||||
|
// Server can update form value
|
||||||
|
const value = await page.$eval("input", (el) => el.value);
|
||||||
|
assertEquals(value, "foobar_foo");
|
||||||
|
|
||||||
|
const logs = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
assertEquals(logs.split(/\n/).filter(Boolean), [
|
||||||
|
"mount Form",
|
||||||
|
"update Form",
|
||||||
|
]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("submit form POST", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_partials/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/form_post`);
|
||||||
|
await page.waitForSelector(".status");
|
||||||
|
|
||||||
|
await page.type("input", "foobar");
|
||||||
|
|
||||||
|
await page.click(".submit");
|
||||||
|
await waitFor(async () => {
|
||||||
|
const logEl = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
return /update Form/.test(logEl);
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = await page.$eval(".url", (el) => el.textContent);
|
||||||
|
assertEquals(url, `${address}/form_post?fresh-partial=true`);
|
||||||
|
|
||||||
|
const logs = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
assertEquals(logs.split(/\n/).filter(Boolean), [
|
||||||
|
"mount Form",
|
||||||
|
"update Form",
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Server can update form value
|
||||||
|
const value = await page.$eval("input", (el) => el.value);
|
||||||
|
assertEquals(value, "foobar_foo");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("pull values from event.submitter if set", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_partials/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/form_submitter`);
|
||||||
|
await page.waitForSelector(".status");
|
||||||
|
|
||||||
|
await page.type("input", "foobar");
|
||||||
|
|
||||||
|
await page.click(".submit");
|
||||||
|
await waitFor(async () => {
|
||||||
|
const logEl = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
return /update Form/.test(logEl);
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = await page.$eval(".url", (el) => el.textContent);
|
||||||
|
assertEquals(url, `${address}/form_submitter?fresh-partial=true`);
|
||||||
|
|
||||||
|
const logs = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
assertEquals(logs.split(/\n/).filter(Boolean), [
|
||||||
|
"mount Form",
|
||||||
|
"update Form",
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Server can update form value
|
||||||
|
const value = await page.$eval("input", (el) => el.value);
|
||||||
|
assertEquals(value, "foobar_foo");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("pull values from event.submitter if set with f-partial", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_partials/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/form_submitter_partial`);
|
||||||
|
await page.waitForSelector(".status");
|
||||||
|
|
||||||
|
await page.type("input", "foobar");
|
||||||
|
|
||||||
|
await page.click(".submit");
|
||||||
|
await waitFor(async () => {
|
||||||
|
const logEl = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
return /update Form/.test(logEl);
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = await page.$eval(".url", (el) => el.textContent);
|
||||||
|
assertEquals(url, `${address}/form_submitter_partial?fresh-partial=true`);
|
||||||
|
|
||||||
|
const logs = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
assertEquals(logs.split(/\n/).filter(Boolean), [
|
||||||
|
"mount Form",
|
||||||
|
"update Form",
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Server can update form value
|
||||||
|
const value = await page.$eval("input", (el) => el.value);
|
||||||
|
assertEquals(value, "foobar_foo");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("should apply partials if submitter parent has no client nav", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_partials/main.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
await page.goto(`${address}/form_submitter_partial_no_client_nav`);
|
||||||
|
await page.waitForSelector(".status");
|
||||||
|
|
||||||
|
await page.type("input", "foobar");
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
page.waitForNavigation(),
|
||||||
|
page.click(".submit"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await page.waitForSelector(".url");
|
||||||
|
|
||||||
|
const url = await page.$eval(".url", (el) => el.textContent);
|
||||||
|
assertEquals(
|
||||||
|
url,
|
||||||
|
`${address}/form_submitter_partial_no_client_nav`,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(async () => {
|
||||||
|
const logEl = await page.$eval("#logs", (el) => el.textContent);
|
||||||
|
return /mount Form/.test(logEl);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Server can update form value
|
||||||
|
const value = await page.$eval("input", (el) => el.value);
|
||||||
|
assertEquals(value, "foobar_foo");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test("fragment navigation should not cause loop", async () => {
|
Deno.test("fragment navigation should not cause loop", async () => {
|
||||||
await withPageName(
|
await withPageName(
|
||||||
"./tests/fixture_partials/main.ts",
|
"./tests/fixture_partials/main.ts",
|
||||||
|
|||||||
Reference in New Issue
Block a user