I'm creating a Dropdown menu using React.
But I've no idea how to implement handleClose().
I want:
- Clicking the body / anything outside the
<Dropdown>=>handleClose() - Clicking any
<ActionMenu>=>handleClose() - Clicking any children inside the
<ActionMenu>=>handleClose()ifpreventDefault()was not called - Clicking any
<ActionMenu enabled={false}>=> do nothing - Clicking any children inside the
<ActionMenu enabled={false}>=> do nothing - Clicking any disabled children inside the
<ActionMenu>=> do nothing - Clicking any
<NonActionMenu>=> do nothing - Clicking any children inside the
<NonActionMenu>=> do nothing <Dropdown>lost focus (onBlur=) because an element outside dropdown got focus =>handleClose()<Dropdown>lost focus (onBlur=) because an element inside dropdown got focus => do nothing
Implementing <Dropdown onBlur={() => handleClose()} ...> works for point 9 but failed for point 10.
Can you suggest your idea?
I've no sandbox because the project is quite complex.
But you can fork & modify here: my project

handleClose()should just render the drop-down list. Nothing more. Any logic to Call the function should be done outside that. For point 10, an onFocus attribute calling the handleClose should be passed to each drop-down item.handleClose()is callingprops.?onClose(), so the user can decide to update/refuse the state:onClose={() => setShow(false)}. I'm stuggling where to spread the event for triggeringhandleClose()onFocusshould not causinghandleClose(). Imagine a user click on an item and suddenly cancel the click (by dragging out -or- holding left click followed by right click -or- by tab key). The item got focus but not clicked.