question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toggle component needs an event.stopPropagation();

See original GitHub issue

Problem Description

I’m displaying a list of Toggle-component inside a Tab-component, inside a Dialog. I have 2 event-handlers, one for changing tab and one for changing a Toggle.

_handleColumnToggle (index, e, visible) {
        e.stopPropagation();
        this.props.dispatch(setColumnVisible(visible, index));
}

 _handleTabChange (tabName) {
        const { rawDataSettings, dispatch } = this.props;
        dispatch(setSettingsTab(tabName));
}

_toggleGridColumns () {
        const { rawDataRawQuery } = this.props,
        self = this,
        columns = rawDataRawQuery.get('columnProps');

        if(columns.size > 0) {
            const styles = {
                block: {
                  maxWidth: 250,
                },
                toggle: {
                  marginBottom: 16,
                },
              };

            return columns.toJS().map((column, index) => {
                return (
                    <div key={index} style={styles.block}>
                        <Toggle key={index}
                          label={column.name}
                          style={styles.toggle}
                          toggled={column.visible}
                          onToggle={self._handleColumnToggle.bind(this, index)}
                        />
                    </div>
                );
            });
        }
    }

<Dialog>
     <Tabs value={selectedTab} onChange={this._handleTabChange}>
         <Tab value="My First Tab" value="tab1">
            { this._toggleGridColumns() }
         </Tab>

        <Tab value="My Second Tab" value="tab2">
           <p>Second tab content</p>
        </Tab>
    </Tabs>
</Dialog>

The issue occurs when I switch a toggle. My toggleHandler is called as expected but the change event also triggers the tabChangeHandler as well but without a valid tabName as parameter.

To solve this I had to change my _handleColumnToggle event handler to this:

    _handleColumnToggle (index, e, visible) {
        e.stopPropagation();
        this.props.dispatch(setColumnVisible(visible, index));
    }

The e.stopPropagation() could probably just be added to the Toggle-component to avoid this kind of behaviour? Took my a while to figure this out, would be good if this could be patched.

Versions

  • Material-UI: 0.15.0-alpha.2
  • React: 0.14.8
  • Browser: Chrome 49.0.2623.112 (64-bit)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LucasBadicocommented, Aug 1, 2017

Guys, this still in need! Having the same issue here.

1reaction
lucianosemerinicommented, Apr 22, 2016

In my case, I am trying to do some kind of Wizard with tabs and a Previous/Next set of buttons.

My state has activeTab, showNext and showPrevious.

One function handles the change when clicking a tab, another when clicking on previous/next buttons.

All was working well until I added a textbox in a Tab, and on keyDown it was triggering the onChange of Tabs…

@ssolders workaround helped me, thank you.

Maybe it would be nice to have a wizard component altogether, in my case, at least ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toggle component needs an event.stopPropagation(); #4065
The issue occurs when I switch a toggle. My toggleHandler is called as expected but the change event also triggers the tabChangeHandler as...
Read more >
Event.stopPropagation() - Web APIs | MDN
The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
Read more >
javascript - Material Design Slide Toggle does not have event ...
preventDefault. I need to require the user to save the change, before changing another value on the page, because "business requirements". Is ...
Read more >
The Dangers of Stopping Event Propagation - CSS-Tricks
This bug happens because the Bootstrap code responsible for closing the dropdown menu is listening for click events on the document. But since ......
Read more >
stopPropagation() Event Method - W3Schools
The stopPropagation() method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found