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) => {
|
onChange={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const taregtEl = e.target as HTMLElement;
|
const taregtEl = e.target as HTMLElement;
|
||||||
const formEl = taregtEl.closest("form") as HTMLFormElement;
|
const formEl = taregtEl.closest("form") as HTMLFormElement | null;
|
||||||
const formData = new FormData(formEl);
|
const data = formEl
|
||||||
const data = Object.fromEntries(formData.entries()) as T;
|
? (Object.fromEntries(
|
||||||
|
new FormData(formEl).entries(),
|
||||||
|
) as T)
|
||||||
|
: ({} as T);
|
||||||
props.changeHandler?.(e, data);
|
props.changeHandler?.(e, data);
|
||||||
props.onChange?.(e);
|
props.onChange?.(e);
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user