Don't crash Form's change handler outside a form

Form reads the closest form on every change. A field rendered through a
portal (a Modal) has no form ancestor in the DOM, so FormData(null) threw.
Change handlers now get an empty object instead.
This commit is contained in:
2026-10-22 05:00:30 +01:00
parent 915c9a1be0
commit d07cbb7c84
+6 -3
View File
@@ -37,9 +37,12 @@ export default function Form<
onChange={(e) => {
e.preventDefault();
const taregtEl = e.target as HTMLElement;
const formEl = taregtEl.closest("form") as HTMLFormElement;
const formData = new FormData(formEl);
const data = Object.fromEntries(formData.entries()) as T;
const formEl = taregtEl.closest("form") as HTMLFormElement | null;
const data = formEl
? (Object.fromEntries(
new FormData(formEl).entries(),
) as T)
: ({} as T);
props.changeHandler?.(e, data);
props.onChange?.(e);
}}