[BUG] - Form onSubmit event not triggered with Modal component
NextUI Version
2.2.9
Describe the bug
If you wrap the Modal window in a form, the onSubmit event does not work, see on: https://codesandbox.io/p/sandbox/form-in-modal-v747c4
Your Example Website or App
https://codesandbox.io/p/sandbox/form-in-modal-v747c4
Steps to Reproduce the Bug or Issue
Wrap Modal in
Hi there,
I saw your issue and found a workaround that might help you until the bug is fixed. Instead of relying on the standard HTML form submission behavior with the onSubmit attribute, you can manually handle the form submission within the modal.
Here's a modified version of the code that worked:
import React from "react";
import {
Modal,
ModalContent,
ModalHeader,
ModalBody,
ModalFooter,
Button,
useDisclosure,
} from "@nextui-org/react";
export default function App() {
const { isOpen, onOpen, onClose, onOpenChange } = useDisclosure();
const handleSubmit = () => {
alert("Submitted");
onClose();
};
return (
<div>
<Button onClick={onOpen}>Open Modal</Button>
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
<ModalContent>
{(onClose) => (
<>
<form
onSubmit={(e) => {
e.preventDefault();
handleSubmit();
}}
>
<ModalHeader className="flex flex-col gap-1">
Modal Title
</ModalHeader>
<ModalBody>Action button type="submit"</ModalBody>
<ModalFooter>
<Button color="danger" variant="light" onPress={onClose}>
Close
</Button>
<Button color="primary" type="submit">
Action
</Button>
</ModalFooter>
</form>
</>
)}
</ModalContent>
</Modal>
</div>
);
}
Feel free to give it a try and see if it resolves the issue for you. Hopefully, this workaround will help until the bug is officially addressed.
@farukumarx64 👍 Yes, thanks, I found a solution, but that doesn't undo this mistake :)
@antonfrolovsky That's great!
Any update on this? Still doesn't work when using react-hook-form.
I know I can just make the submit Button component run the handleSubmit, but still.
Code snippet for reference
const { isOpen, onOpenChange, onOpen, onClose } = useDisclosure();
const {
control,
handleSubmit,
formState: { errors },
} = useForm<HookFormData>({ mode: "onChange" });
const [isMutationLoading, setMutationLoading] = useState(false);
const onSubmit = (formData: HookFormData) => {
setMutationLoading(true);
console.log(formData);
};
return (
<>
<Button onPress={onOpen}>Open modal</Button>
<form onSubmit={handleSubmit(onSubmit, () => console.log("Invalid form submission"))}>
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
<ModalContent>
<ModalHeader>Run an action</ModalHeader>
<ModalBody>
render={({ field }) => (
<Input
{...field}
isInvalid={!!errors.minDuration}
isRequired={true}
errorMessage={errors.minDuration?.message}
type="numeric"
label="Some numeric input"
/>
)}
/>
</ModalBody>
<ModalFooter>
<Button type="submit" isLoading={isMutationLoading} color="primary">
Action
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</form>
</>
);
@yanuu1337 have you tried to put form inside ModalContent? We're using this pattern in other application and didn't see the issue (also using react-hook-form).
@wingkwong Maybe that's the solution, haven't tried it. Thanks! My temporary solution was to assign the handleSubmit function to the button itself to trigger submission. I'll update the code tomorrow and let you know if it works 😄
I can confirm that the form element needs to be inside of the ModalContent, the handleSubmit works now. Thanks, @wingkwong :)
There's a problem with putting the form element inside ModalContent and scrollBehavior="inside". The form seems to overload the modal and the observed scroll behavior is outside. Children of ModalContent probably should not be wrapped as that breaks the content flexbox.
I can second the issue mentioned by @Archivec. I also tried <ModalContent as="form"> and that results in the same scroll issue as putting the form element inside of the ModelContent. Is there a work around for this?
I encountered the same issue as @Archivec and @willalread. Workaround for me was basically to mimic the classes that were on the
So in my case I just added to my Form inside the ModalContent this className: className="flex h-full w-full flex-col gap-0 overflow-y-auto" and it fixed my layout issues
I can second the issue mentioned by @Archivec. I also tried
<ModalContent as="form">and that results in the same scroll issue as putting the form element inside of the ModelContent. Is there a work around for this?
I solved the body scroll issue when scrollBehavior="inside" with as="form" onSubmit={handleSubmit}.
In this way Header and Footer are sticky but body scroll.
<Modal isOpen={isOpen} onOpenChange={onOpenChange} size="2xl" scrollBehavior="inside" placement="center" backdrop="blur">
<ModalContent as="form" onSubmit={handleSubmit}>
{(onClose) => (
<>
<ModalHeader className="flex flex-col gap-1">
</ModalHeader>
<ModalBody className="gap-6">
{/* A LOT OF INPUT COMPONENT */}
</ModalBody>
<ModalFooter>
{/* BUTTON WITH TYPE SUBMIT */}
</ModalFooter>
</>
)}
</ModalContent>
</Modal>