react-toggle
react-toggle copied to clipboard
Allow a onClick prop so we can stop propagation
I have a use case where the Toggle button is on a list of items which are selectable by a mouse click. So switching the toggle state also propagates a click event and my list item selection gets toggled too and I don't want that :)
All we need to prevent this is to be able to pass an onClick handler as a prop to the Toggle component so we can just do a event.stopPropagation and thus prevent further event handlers to be executed (in my case the container's click event which toggles the item selected state).
For now I need to check the target manually in the parent's click handler.
Cheers
By the way, adding a wrapper around the Toggle and stopping propagation there also works but I like my clean markups ;-)
It would be a great addition !
I'm happy to take a stab at an implementation of this. Adding an actual onClick prop might be confusing, though, since we'd need to explain that onClick is not recommended for use to obtain the status of the toggle.
Perhaps we should add a prop for stopPropagation instead, which would instrument the onClick handler internally.
I could write it for either case, but I'd be curious what @aaronshaf thinks.