Skip to content

Commit 11ec2a8

Browse files
authored
fix #1784 - dialog checks if underlying form has method dialog (#1785)
* fix #1784 - dialog checks if underlying form has method dialog * tweaks from coderabbit * nullish value handling
1 parent 09de923 commit 11ec2a8

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

src/lib/dialog/Dialog.svelte

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<script lang="ts">
2-
import { onDestroy } from "svelte";
32
import type { DialogProps, ParamsType } from "$lib";
43
import { trapFocus } from "$lib/utils/actions";
5-
import { dialog } from "./theme";
64
import CloseButton from "$lib/utils/CloseButton.svelte";
75
import { createDismissableContext } from "$lib/utils/dismissable";
86
import clsx from "clsx";
97
import { sineIn } from "svelte/easing";
108
import { fade } from "svelte/transition";
9+
import { dialog } from "./theme";
1110
1211
let { children, onaction = () => true, oncancel, onsubmit, ontoggle, form = false, modal = true, autoclose = false, focustrap = false, open = $bindable(false), permanent = false, dismissable = true, outsideclose = true, class: className, classes, transition = fade, transitionParams, count, ...restProps }: DialogProps = $props();
1312
@@ -56,25 +55,32 @@
5655
}
5756
5857
function _onsubmit(ev: SubmitEvent & { currentTarget: HTMLDialogElement }) {
59-
// When dialog contains the <form method="dialog"> and when child with type="submit" was pressed
60-
6158
onsubmit?.(ev);
6259
if (ev.defaultPrevented) return;
6360
61+
// When dialog contains the <form method="dialog"> and when child with type="submit" was pressed
62+
if (!(ev.target instanceof HTMLFormElement) || ev.target.method !== "dialog") {
63+
return;
64+
}
65+
6466
ev.preventDefault(); // stop dialog.close()
6567
6668
const dlg: HTMLDialogElement = ev.currentTarget;
6769
68-
if (ev.submitter instanceof HTMLButtonElement || ev.submitter instanceof HTMLInputElement) {
69-
dlg.returnValue = ev.submitter.value;
70+
if (ev.submitter && "value" in ev.submitter) {
71+
// this is done by the system but after the submit event
72+
dlg.returnValue = String(ev.submitter.value ?? "");
7073
}
7174
7275
if (!dlg.returnValue) {
7376
return cancel(dlg); // if no action - treat that as cancel
7477
}
7578
76-
// explicit false from onaction blocks the form closing
77-
if (typeof onaction === "function" && onaction({ action: dlg.returnValue, data: new FormData(ev.target as HTMLFormElement) }) === false) return;
79+
if (typeof onaction === "function") {
80+
const result = onaction({ action: dlg.returnValue, data: new FormData(ev.target) });
81+
// explicit false from onaction blocks the form closing
82+
if (result === false) return;
83+
}
7884
7985
close(dlg);
8086
}
@@ -116,31 +122,6 @@
116122
}
117123
118124
createDismissableContext(close_handler);
119-
120-
let escHandler: ((e: KeyboardEvent) => void) | null = null;
121-
122-
$effect(() => {
123-
if (escHandler) {
124-
window.removeEventListener("keydown", escHandler);
125-
escHandler = null;
126-
}
127-
128-
if (open && typeof count === "number" && count > 0) {
129-
escHandler = (e: KeyboardEvent) => {
130-
if (e.key === "Escape") {
131-
e.preventDefault();
132-
e.stopPropagation();
133-
}
134-
};
135-
window.addEventListener("keydown", escHandler);
136-
}
137-
});
138-
139-
onDestroy(() => {
140-
if (escHandler) {
141-
window.removeEventListener("keydown", escHandler);
142-
}
143-
});
144125
</script>
145126

146127
{#snippet content()}

0 commit comments

Comments
 (0)