nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - Button `onPress` event flaky in modal

Open coratgerl opened this issue 1 year ago • 11 comments

NextUI Version

2.6.8

Describe the bug

I have a modal with a button to submit a form inside the modal. On a playwright test the event onPress is not trigger if I don't wait 500 ms before click on button. This work with onClick event. There is no state update on my side that can flaky my code. My test also check that my button is enabled before click and this step works.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Not sure how to reproduce because it is very specific on this modal, but my modal look like this :

 <Modal
    isOpen={isOpen}
    onOpenChange={(open) => {
      if (!open) onClose()
    }}
  >
    <ModalContent>
      {(onClose) => (
        <>
          <ModalHeader className="flex flex-col gap-1">
            Create
          </ModalHeader>

          <ModalBody>
              <Input
                name="maxRedemptions"
                onBlur={form.handleBlur}
                onChange={form.handleChange}
                type="text"
                variant="faded"
                label="Max usages"
                errorMessage={form.errors.maxRedemptions}
                value={form.values.maxRedemptions}
                isInvalid={
                  form.dirty &&
                  form.touched.maxRedemptions &&
                  !!form.errors.maxRedemptions
                }
              />
          </ModalBody>

          <ModalFooter>
            <Button
              color="default"
              variant="light"
              onPress={onClose}
              className="Cancel"
            >
              Cancel
            </Button>

            <Button
              // OnPress is flaky here
              onPress={() => {
                   console.log("onPress") // Never display on click
                   return form.handleSubmit()
              }}
              isDisabled={!form.isValid}
              isLoading={form.isSubmitting}
              className="SubmitCreatePromotion"
            >
              Create
            </Button>
          </ModalFooter>
        </>
      )}
    </ModalContent>
  </Modal>

Expected behavior

The onPress is call

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

coratgerl avatar Dec 26 '24 19:12 coratgerl

Because of this issue, as well as another problem I encountered in a newer version, I fixed the version used in my production environment to 2.6.5. Apart from a minor issue with the Popover-based animation, I haven't received any other error reports.

In version 2.6.5, I can use onClick. I'm not that focused on accessibility; instead, I prefer the elements on the page to respond correctly to most people's interactions.

Also report in https://github.com/nextui-org/nextui/pull/4322#issuecomment-2533519978 .

AnYiEE avatar Dec 27 '24 04:12 AnYiEE

With the latest version and the deprecation in more components, I have a lot of test that not pass now with onPress. Mainly on submit button in modal.

coratgerl avatar Dec 30 '24 16:12 coratgerl

Please provide a minimal sandbox for your modal setup with the flaky test result.

Peterl561 avatar Jan 01 '25 14:01 Peterl561

@coratgerl not 100% sure is the same issue, but I identified a very similar problem using Button inside forms after migrating to 2.6.10 (also tested 2.6.11).

Here a reproducible example: https://codesandbox.io/p/sandbox/qzmwk4?file=%2Fsrc%2FApp.tsx

Using the Button with onPress makes the form to be submitted

keul avatar Jan 09 '25 14:01 keul

I am not sure that is the same issue, because in my case the form is never submitted, but interesting ! I try to create a minimal sandbox but it is hard because I have a complex environnement for my tests.

coratgerl avatar Jan 09 '25 15:01 coratgerl

@Peterl561 in any case, what I reported in the codesandbox seems an issue to me. Let me know if you prefer I open a new bug report (to not mix with current report)

keul avatar Jan 10 '25 09:01 keul

I am not sure that is the same issue, because in my case the form is never submitted, but interesting ! I try to create a minimal sandbox but it is hard because I have a complex environnement for my tests.

Ah sorry, I saw some "form" reference in your code, I missed you are not using any form tag.

keul avatar Jan 10 '25 09:01 keul

@coratgerl not 100% sure is the same issue, but I identified a very similar problem using Button inside forms after migrating to 2.6.10 (also tested 2.6.11).

Here a reproducible example: https://codesandbox.io/p/sandbox/qzmwk4?file=%2Fsrc%2FApp.tsx

Using the Button with onPress makes the form to be submitted

This is likely unrelated to the current issue. What's happening is that preventDefault is being called in onClick, which prevents the form submission. If you wish to control the form submit while using onPress, you can try passing a handler to form's onSubmit and calling preventDefault on the submission event as needed. PressEvent itself does not have preventDefault because under the hood it's handling multiple events for you (for further reading see this and this).

Peterl561 avatar Jan 10 '25 09:01 Peterl561

@Peterl561 ok, so I misunderstood how it works from react-aria discussion. There are a lot of similar post there. Sorry for the noise then!

keul avatar Jan 10 '25 09:01 keul

Closing in favor of v3. If the issue is reproducible in v3, please create a new one with a minimal reproducible example.

wingkwong avatar Apr 03 '26 13:04 wingkwong