-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(modal): onOpenChange triggering when modal opens #3902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: canary
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@nextui-org/modal": patch | ||
"@nextui-org/use-disclosure": patch | ||
--- | ||
|
||
Added useEffect in useModal to fire onOpenChange and in useDisclosure, onOpenChange now accepts onOpen as parameter(#3887) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import {render, fireEvent} from "@testing-library/react"; | |
import userEvent from "@testing-library/user-event"; | ||
|
||
import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter} from "../src"; | ||
import {Button} from "../../button/src"; | ||
|
||
// e.g. console.error Warning: Function components cannot be given refs. | ||
// Attempts to access this ref will fail. Did you mean to use React.forwardRef()? | ||
|
@@ -91,6 +92,33 @@ describe("Modal", () => { | |
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
test("should fire 'onOpenChange' callback when open button is clicked", async () => { | ||
sanuj21 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const onOpen = jest.fn(); | ||
|
||
const {getByLabelText} = render( | ||
<> | ||
<Button aria-label="Open" onClick={onOpen}> | ||
Open Modal | ||
</Button> | ||
<Modal isOpen> | ||
<ModalContent> | ||
<ModalHeader>Modal header</ModalHeader> | ||
<ModalBody>Modal body</ModalBody> | ||
<ModalFooter>Modal footer</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</>, | ||
); | ||
|
||
const openButton = getByLabelText("Open"); | ||
|
||
const user = userEvent.setup(); | ||
|
||
await user.click(openButton); | ||
|
||
expect(onOpen).toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it("should hide the modal when pressing the escape key", () => { | ||
const onClose = jest.fn(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import type {HTMLMotionProps} from "framer-motion"; | |
|
||
import {AriaModalOverlayProps} from "@react-aria/overlays"; | ||
import {useAriaModalOverlay} from "@nextui-org/use-aria-modal-overlay"; | ||
import {useCallback, useId, useRef, useState, useMemo, ReactNode} from "react"; | ||
import {useCallback, useId, useRef, useState, useMemo, ReactNode, useEffect} from "react"; | ||
import {modal} from "@nextui-org/theme"; | ||
import { | ||
HTMLNextUIProps, | ||
|
@@ -129,6 +129,12 @@ export function useModal(originalProps: UseModalProps) { | |
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
onOpenChange?.(isOpen); | ||
} | ||
}, [isOpen]); | ||
ryo-manba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Comment on lines
+132
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came across this interesting resource on the React docs (https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes) that suggests alternative approaches to In the example provided, they achieve a similar outcome by storing information from previous renders. I took a stab at adapting it here (untested): const [prevIsOpen, setPrevIsOpen] = useState(isOpen);
if (isOpen !== prevIsOpen) {
setPrevIsOpen(isOpen);
if (isOpen) {
onOpenChange?.(isOpen);
}
} What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, It seems a very nice idea to avoid useEffect. I'll test and see if it works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested, actually the onOpenChange is eventually changing the isOpen state. And we shouldn't directly update parent state inside child without useEffect. I tried though and got the warning : "Cannot update a component ( Let me know if I am missing something. |
||
const {modalProps, underlayProps} = useAriaModalOverlay( | ||
{ | ||
isDismissable, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,14 @@ export function useDisclosure(props: UseDisclosureProps = {}) { | |
onOpenPropCallbackRef?.(); | ||
}, [isControlled, onOpenPropCallbackRef]); | ||
|
||
const onOpenChange = useCallback(() => { | ||
const action = isOpen ? onClose : onOpen; | ||
const onOpenChange = useCallback( | ||
(isOpen: boolean) => { | ||
const action = isOpen ? onOpen : onClose; | ||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in this case its needed. Because when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that the expected behavior?
Accepting an argument would be a breaking change, so it might be better to leave it as is for backward compatibility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you clarify in what cases the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ya, i thought that it might be a breaking change, but we are also passing state while calling onOpenChange from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, so the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think? @jrgarciadev |
||
|
||
action(); | ||
}, [isOpen, onOpen, onClose]); | ||
action(); | ||
}, | ||
[isOpen, onOpen, onClose], | ||
); | ||
|
||
return { | ||
isOpen: !!isOpen, | ||
|
Uh oh!
There was an error while loading. Please reload this page.