Fix the issue where the Menu will open when clicking anywhere on the screen other than the target #4114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When clicking anywhere on the screen that is not a target, the Menu also performs opening and closing.
https://codesandbox.io/s/nifty-curran-fy1nlu?file=/src/App.tsx
Based on my humble opinion, it may be caused by two reasons:
The
dropdown
is only hidden and has not been completely uninstalled, resulting in its lifecycle methods and other methods still existing.clickOutside
is not controlled based on theopened
state, so both the open and closed states will triggeronClose
.In version 6.0.8, in order for
Menu
to be properly controlled and simultaneously callonClose
andonOpen
, I madeclickOutside
call thetoggleDropdown
method ofMenu
, resulting in a situation where clicking on an external button can also control the switch state.Here are the fixes I made:
clickOutside
only executes theonClose
method whenopened: true
.At the same time, I also modified the test file for
closeOnClickOutside
inPopover
:Finally, I have added detailed testing files for
Menu
in both controlled and uncontrolled states. Feel free to rework or adjust them~