[BUG?] - Using Modal/Drawer as form hitting Enter closes the modal
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
I couldn't access your sandbox. Please make it public.
I couldn't access your sandbox. Please make it public.
Man sry, it should be public now!
still not able to access
still not able to access
I do not understand what is happening here.. But try again plz: https://codesandbox.io/p/devbox/frosty-bhaskara-j53crw
@wingkwong Codebox worked now right?
@J4v4Scr1pt yes. this needs further investigation as i haven't used as form in modal.
@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 😄.
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>
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! :)
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?
Has anyone found a better solution for this ? Trying to integrate with a context providers for modals does not play well with that solution
Anyone discovered a clean way around this yet, using either <Form> inside the Dialog, or <ModalContent as='form'>?
Anyone discovered a clean way around this yet, using either
<Form>inside theDialog, 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.
