Skip to content
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

Button event propagation #129

Open
mostafaelassar opened this issue Apr 28, 2020 · 3 comments
Open

Button event propagation #129

mostafaelassar opened this issue Apr 28, 2020 · 3 comments

Comments

@mostafaelassar
Copy link

I have used Wrapper alongside other components inside MyComponent where it has its own click event handler.

<MyComponent onClick={this.handleClick}>
    {otherComponents}
    <Wrapper>
        <Button>
          click me
        </Button>
        <Menu className='MyMenuButton-menu'>
          <ul>{menuItems}</ul>
        </Menu>
    </Wrapper>
</MyComponent>

It would seem that a click on Button propagates event to MyComponent & triggers its click event handler.

@shirshendubhowmick
Copy link
Collaborator

You can handle this in your MyComponent ?

@mostafaelassar
Copy link
Author

At first I got around this by using this onClick handler on Button

    handleClick = (e: React.MouseEvent) => {
        const { disabled } = this.props;
        e.stopPropagation();
        if (disabled) {
            return;
        }
        if (document.querySelector(`#${this.wrapperId} > [role="menu"]`)) {
            closeMenu(this.wrapperId);
        } else {
            openMenu(this.wrapperId);
        }
    };

But then I used this one instead on Wrapper since I had other buttons inside Menu itself.

handleClick = (e: React.MouseEvent) => {
    e.stopPropagation();
};

@shirshendubhowmick
Copy link
Collaborator

Is it possible to share a working example ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants