react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Click event "goes through" and clicks on elements behind list

Open adamscybot opened this issue 11 years ago • 43 comments

I have a problem whereby the click to select an option seems to go through to elements underneath the list. The selection itself works, but I dont want things underneath to get clicked on. How can I stop this?

adamscybot avatar Oct 22 '15 15:10 adamscybot

I think this issue is actually related to react-tap-event-plugin which unfortunately is a requirement of the very popular material-ui. This library must do something with the events which isn't compatible in some way. Any ideas?

Relevant discussion: https://github.com/facebook/react/issues/2061

I believe react-tap-event-plugin will eventually be built into react by the time 1.0 arrives -- so it might be useful to tackle this now.

adamscybot avatar Oct 22 '15 17:10 adamscybot

+1

Did you find a solution ?

otroboe avatar Oct 30 '15 15:10 otroboe

I have a hack in place that listens on the react-select events to set pointer-events: none on everything while the select box is open -- pretty horrible.

adamscybot avatar Oct 31 '15 00:10 adamscybot

Horrible things can save me :p How do you catch when the select box is open ?

otroboe avatar Nov 03 '15 13:11 otroboe

I have bound these attributes to the select

onFocus={this.disableClick.bind(this, true)} onBlur={this.disableClick.bind(this, false)}

disableClick does something like:

disableClick(disable) {
    this.setState({ disableClick: disable });
    return true;
}

And then in my render I have this on an element which wraps both the select and everything that could potentially be underneath it:

style={{pointerEvents: this.state.disableClick ? 'none' : 'all'}}

Note that this wont fix browsers that don't support pointer events which I think is < IE 11.

adamscybot avatar Nov 03 '15 17:11 adamscybot

Does this have something to do with react-select triggering on mousedown rather than on click?

joearasin avatar Dec 17 '15 21:12 joearasin

I am also running into this issue, and can confirm that is is related to https://github.com/zilverline/react-tap-event-plugin. Anything behind the dropdown listening to onTouchTap events will trigger through the dropdown's container.

Has anyone tried using 1.0's CustomComponents and onTouchDown events, similar to this?

t1mmen avatar Dec 24 '15 22:12 t1mmen

+1 Also ran into this problem.

I am using react select in a dialogue box. I made the options overflow the dialogue box. Every time an overflowed option is closed, the dialogue box automatically closes, as if the area below the options and outside of the dialogue is clicked.

It seems that this problem has been fixed in the 1.0.0 version of react-tap-event-plugin. Reference: https://github.com/facebook/react/issues/2061

However, the bug still exists in my project.

injune1123 avatar Jun 07 '16 21:06 injune1123

+1 same problem.

woniesong92 avatar Sep 01 '16 08:09 woniesong92

I also ran into a variation of this problem so I thought this might help.

I am using the Checkbox component from material-ui and the select menu goes on top of the checkbox, but whenever I selected the option right on top of the checkbox, it would get checked/unchecked. After two days of scanning through the libraries to find why that happens I saw that the checkbox uses the z-index: 2 which causes the click to propagate to the checkbox first, so I managed to fix that by setting the z-index to a really high value on the Select-menu-outer class.

luciandragomir avatar Oct 05 '16 15:10 luciandragomir

So what is the solution for this problem?

I'm currently using "react-select": "1.0.0-rc.2" and "react-tap-event-plugin": "2.0.0"

anvk avatar Jan 17 '17 21:01 anvk

My solution to this was to stop using react-tap-event-plugin.

This might not work for everyone but the 300ms delay is history as of iOS 10 (and recent updates of iOS 9) it's also easily worked around in most "recent" versions of Android. Even iOS 8 does not wait 300ms if you use a "long touch".

I hope that helps!

Stenerson avatar Jan 17 '17 21:01 Stenerson

Unfortunately, I don't use react-tap-event and the problem still occurs? @adamscybot solution seems a bit hackish for my taste ...

oserban avatar Feb 03 '17 15:02 oserban

See prateekjahead's comment: https://github.com/JedWatson/react-select/issues/1010 "HubSpot's react-select-plus takes in dropdownComponent as a prop which solves the issue."

Can some one try react-select-plus and see if it fixes the issue?

turnerniles avatar Mar 05 '17 18:03 turnerniles

I can confirm @luciandragomir's answer works for MaterialUI users - adding this solved the problem for me:

  .Select-menu-outer {
    z-index: 3;
  }

cchanningallen avatar Mar 21 '17 21:03 cchanningallen

Seems like the issue is that it hides the options on a Mouse down, now if there are elements behind it, they are triggered by the Mouse up event. At least that was the issue in my case. Changed the element below to trigger on mouse up and it fixed it for me. It has been mentioned before in : https://github.com/JedWatson/react-select/issues/432

heartz avatar Apr 05 '17 13:04 heartz

I did so: injectTapEventPlugin({ shouldRejectClick: () => document.body.querySelector('.Select-menu-outer') });

sfilippenko avatar Jul 03 '17 13:07 sfilippenko

+1 I see this problem on mobile devices and no solution mentioned seems to fix it. I think it is specifically because of the touch events. I am not using Material UI and not using react-tap-event-plugin.

camlough avatar Jul 26 '17 20:07 camlough

A custom option component, where mouse-down handlers were replaced by mouse-up handlers, worked for me:

import React from 'react'
import createClass from 'create-react-class'
import PropTypes from 'prop-types'

const CustomReactSelectOption = createClass({
  propTypes: {
    children: PropTypes.node,
    className: PropTypes.string,
    isDisabled: PropTypes.bool,
    isFocused: PropTypes.bool,
    isSelected: PropTypes.bool,
    onFocus: PropTypes.func,
    onSelect: PropTypes.func,
    option: PropTypes.object.isRequired,
  },
  handleMouseUp(event) {
    event.preventDefault()
    event.stopPropagation()
    this.props.onSelect(this.props.option, event)
  },
  handleMouseEnter(event) {
    this.props.onFocus(this.props.option, event)
  },
  handleMouseMove(event) {
    if (this.props.isFocused) return
    this.props.onFocus(this.props.option, event)
  },
  render() {
    return (
      <div
        className={this.props.className}
        onMouseUp={this.handleMouseUp} // Mouse Up instead of Mouse Down
        onMouseEnter={this.handleMouseEnter}
        onMouseMove={this.handleMouseMove}
        title={this.props.option.title}
        role="listitem"
      >
        {this.props.children}
      </div>
    )
  },
})

export default CustomReactSelectOption

and then

import CustomReactSelectOption from './CustomReactSelectOption'
...
          <Select
            ...
            optionComponent={CustomReactSelectOption}
            ...
          />

adamlibusa avatar Apr 26 '18 16:04 adamlibusa

I did this wild thing where I added onClick={e => e.preventDefault()} to a parent div of all the react-select components and then the click-through bug stopped.

Does this work for anyone else?

peterboyer avatar Jun 21 '18 06:06 peterboyer