nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG?] - Using Modal/Drawer as form hitting Enter closes the modal

Open J4v4Scr1pt opened this issue 1 year ago • 14 comments

NextUI Version

2.6.11

Describe the bug

Maybe I'm doing something wrong here using the Modal/Drawer as a form, but it's the most convenient way to wrap both Footer and Body. ModalContent don't accept a action prop so can't use that as the form.

The Modal has inputs that are required and it uses ServerAction for submitting. This is how I always have used it and for some reason this has changed during the latest updates.

Your Example Website or App

https://codesandbox.io/p/devbox/frosty-bhaskara-j53crw

Steps to Reproduce the Bug or Issue

Open CodeSandbox. Open the modal. Click in a Input. Hit Enter.

Expected behavior

Tbh I could be wrong here, but I would Expect it to not close because the inputs have no values and are requierd.

Screenshots or Videos

No response

Operating System Version

Window 11

Browser

Chrome

J4v4Scr1pt avatar Jan 08 '25 08:01 J4v4Scr1pt

I couldn't access your sandbox. Please make it public.

wingkwong avatar Jan 08 '25 09:01 wingkwong

I couldn't access your sandbox. Please make it public.

Man sry, it should be public now!

J4v4Scr1pt avatar Jan 08 '25 09:01 J4v4Scr1pt

still not able to access

image

wingkwong avatar Jan 08 '25 10:01 wingkwong

still not able to access

image

I do not understand what is happening here.. But try again plz: https://codesandbox.io/p/devbox/frosty-bhaskara-j53crw

J4v4Scr1pt avatar Jan 08 '25 11:01 J4v4Scr1pt

@wingkwong Codebox worked now right?

J4v4Scr1pt avatar Jan 09 '25 08:01 J4v4Scr1pt

@J4v4Scr1pt yes. this needs further investigation as i haven't used as form in modal.

wingkwong avatar Jan 09 '25 11:01 wingkwong

@wingkwong I have some problem with this atm. Customers closing the modal accidentally when filling the form and not done yet.

The Form example with a modal in the docs don't use the form element. Do you have a recommended way of having a form inside the modal? The only convenient way I found is removing the Footer component and moving all inside the ModalBody and wrapping that content inside a form-tag. If everything works with codesandbox I added an example there 😄.

J4v4Scr1pt avatar Jan 14 '25 04:01 J4v4Scr1pt

Have you tried something like this?

<Button onPress={onOpenOne}>Open Modal as form</Button>
<Modal
  isOpen={isOpenOne}
  onOpenChange={onOpenChangeOne}
>
  <ModalContent>
    <form className="flex flex-col gap-2" action={formAction}>
      <ModalHeader className="flex flex-col gap-1">
        Modal Title
      </ModalHeader>
      <ModalBody>
        <Input isRequired label="Name" type="text" name="firstName" />
        <Input isRequired label="LastName" type="text" name="lastName" />
      </ModalBody>
      <ModalFooter>
        <Button
          isLoading={isPending}
          color="danger"
          variant="light"
          onPress={onOpenChangeOne}
        >
          Close
        </Button>
        <Button isLoading={isPending} color="primary" type="submit">
          Action
        </Button>
      </ModalFooter>
    </form>
  </ModalContent>
</Modal>

wingkwong avatar Jan 14 '25 16:01 wingkwong

Have you tried something like this?

<Button onPress={onOpenOne}>Open Modal as form</Button>
<Modal
  isOpen={isOpenOne}
  onOpenChange={onOpenChangeOne}
>
  <ModalContent>
    <form className="flex flex-col gap-2" action={formAction}>
      <ModalHeader className="flex flex-col gap-1">
        Modal Title
      </ModalHeader>
      <ModalBody>
        <Input isRequired label="Name" type="text" name="firstName" />
        <Input isRequired label="LastName" type="text" name="lastName" />
      </ModalBody>
      <ModalFooter>
        <Button
          isLoading={isPending}
          color="danger"
          variant="light"
          onPress={onOpenChangeOne}
        >
          Close
        </Button>
        <Button isLoading={isPending} color="primary" type="submit">
          Action
        </Button>
      </ModalFooter>
    </form>
  </ModalContent>
</Modal>

No I have not, will try it thx! :)

J4v4Scr1pt avatar Jan 15 '25 10:01 J4v4Scr1pt

I tested you suggestion @wingkwong and it works. (updated CodeSandbox) Need to manually call onOpenChange after successfully submitting the form to close it though.

Should we close this as the solution or do you want to investigate more using as="form" on the Modal/Drawer?

J4v4Scr1pt avatar Jan 17 '25 06:01 J4v4Scr1pt

Has anyone found a better solution for this ? Trying to integrate with a context providers for modals does not play well with that solution

dl-web-dev avatar Jan 22 '25 22:01 dl-web-dev

Anyone discovered a clean way around this yet, using either <Form> inside the Dialog, or <ModalContent as='form'>?

dsgriffin avatar Feb 05 '25 18:02 dsgriffin

Anyone discovered a clean way around this yet, using either <Form> inside the Dialog, or <ModalContent as='form'>?

I'm still using @wingkwong solution which works for me. But using <Form> inside maybe is a good thing. Never tried it though.

J4v4Scr1pt avatar Feb 05 '25 19:02 J4v4Scr1pt