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:
+6
-3
@@ -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);
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user