-
Notifications
You must be signed in to change notification settings - Fork 29
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
Passing functions to onClick #49
Comments
Yeah currently there isn't a convenient way to handle this. If you need to
If you just need to perform an additional effect when the popup is opened, and you're using a functional component, I would recommend performing an effect when React.useEffect(() => {
if (popupState.isOpen) {
// popup was just opened, do something
// popupState.anchorEl is the element that was clicked
}
}, [popupState.isOpen]) This is better since if you later need to add additional ways the popup can be opened, all of them will trigger this effect, whereas an onClick handler wouldn't respond to the new ways of opening the popup. There are several ways I could make the API support this, none I'm 100% happy with, each one is awkward in some way. Possibility 1{bindTrigger(
popupState,
<Image
alt="Plus"
onClick={handleClick}
/>
)} Possibility 2<Image
alt="Plus"
{...bindTrigger(popupState, {
onClick: handleClick,
})}
/> Possibility 3// at root level:
const TriggerImage = createTrigger(Image)
// somewhere later:
<TriggerImage
popupState={popupState}
alt="Plus"
onClick={handleClick}
/> |
Yes, I found that running handleClick when the menu is open works for what I am trying to achieve! Thank you for your help! |
Okay great! I'm still thinking about maybe releasing some kind of API for merging user event handlers with the injected ones. I haven't decided yet... |
Cool, all the best with that! I'll close the issue now. |
Possibility 2 is exactly what I imagined before checking out this issue, which seems pretty straightforward to me. Could this be implemented somehow? I can work on it if you want some help. |
The main problem with possibility 2 is I can't see any good way to memoize the combined If you combine them yourself you can at least choose to do so in a But I guess we could implement option 2 and just warn in docs that it's always going to return a new |
Actually let me think about it some more. i guess we could store the previous username |
I figured sort of a workaround by augmenting the internal methods in const popupState = usePopupState({ popupId, variant: "popper" });
const modifiedPopupState = {
...popupState,
toggle: (arguments_) => {
// Do stuff
popupState.toggle(arguments_);
},
};
// somewhere later
return (
<div>
<button {...bindToggle(modifiedPopupState)}>Open</button>
<BasePopper {...bindPopover(modifiedPopupState)}>
<div>
<h1>Lorem Ipsum</h1>
</div>
</BasePopper>
</div>
); |
@andrew2764 that wouldn't currently work because |
@ifndefdeadmau5 @andrew2764 I've been thinking about this some more...the main problem with all three possibilities above is, does the custom So now I'm leaning toward an API with
|
🎉 This issue has been resolved in version 5.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Okay I decided to go with an API like
This makes specifying the order of the handlers straightforward and would be useful for combining multiple |
Thanks! I ended up using the approach from the sample code in the README const button = (
<Button
{...bindTrigger(popupState)}
onClick={(e: React.MouseEvent) => {
performCustomAction(e)
bindTrigger(popupState).onClick(e)
}}
>
Open Menu
</Button>
) The only difference is that I needed to perform an action before the |
@andrew2764 yeah personally I still prefer that approach, the control flow would be more straightforward in a debugger |
I have created a dropdown meny with an Image acting like the trigger like so:
I want to pass a function called handleClick to onClick, but if I remove the comment then it overrides the onClick function generated by bindtrigger. I wanted to ask if there is a way to pass handleClick to the generated onClick?
The text was updated successfully, but these errors were encountered: