nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - Form onSubmit event not triggered with Modal component

Open antonfrolovsky opened this issue 2 years ago • 11 comments

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

Set type="submit" to Button in footer Try to sent form

Expected behavior

I expect that the onSubmit event of the form will start

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

antonfrolovsky avatar Jan 18 '24 14:01 antonfrolovsky

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 avatar Jan 21 '24 23:01 farukumarx64

@farukumarx64 👍 Yes, thanks, I found a solution, but that doesn't undo this mistake :)

antonfrolovsky avatar Jan 21 '24 23:01 antonfrolovsky

@antonfrolovsky That's great!

farukumarx64 avatar Jan 21 '24 23:01 farukumarx64

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 avatar Aug 17 '24 15:08 yanuu1337

@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 avatar Aug 22 '24 07:08 wingkwong

@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 😄

yanuu1337 avatar Aug 24 '24 17:08 yanuu1337

I can confirm that the form element needs to be inside of the ModalContent, the handleSubmit works now. Thanks, @wingkwong :)

yanuu1337 avatar Aug 26 '24 19:08 yanuu1337

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.

Archivec avatar Dec 29 '24 01:12 Archivec

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?

willalread avatar Jan 25 '25 20:01 willalread

I encountered the same issue as @Archivec and @willalread. Workaround for me was basically to mimic the classes that were on the

(the modal wrapper). When you move an element inside, it should carry the same structure, so I applied those same classes to ensure layout and height behave correctly.

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

Micueb avatar Jul 08 '25 08:07 Micueb

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>

roberto-ingenito avatar Oct 20 '25 22:10 roberto-ingenito