最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React-select - can't stopPropagation on valueRenderer component - Stack Overflow

programmeradmin4浏览0评论

I am trying to display a popover on click over a react-select selected value element as follow :

My issue happens when I click on the popover trigger, the dropdown opens and the popover too... I just want to open the popover, I tried e.nativeElement.stopPropagation, e.stopPropagation, e.preventDefault and so on without success. Here is a sandbox If you want to know how I did this and how to reproduce the issue.

Thanks for you help

I am trying to display a popover on click over a react-select selected value element as follow :

My issue happens when I click on the popover trigger, the dropdown opens and the popover too... I just want to open the popover, I tried e.nativeElement.stopPropagation, e.stopPropagation, e.preventDefault and so on without success. Here is a sandbox If you want to know how I did this and how to reproduce the issue.

https://codesandbox.io/s/pjv7vmlv3j

Thanks for you help

Share Improve this question edited Nov 20, 2017 at 14:52 Patrick Ferreira asked Nov 20, 2017 at 14:10 Patrick FerreiraPatrick Ferreira 2,0532 gold badges16 silver badges33 bronze badges 1
  • Possible duplicate of React-Select onClick on multi tag prevent open dropdown – Mišo Commented Apr 13, 2019 at 9:15
Add a comment  | 

2 Answers 2

Reset to default 13

The react-select opens the dropdown as a reaction to onMouseDown event, not onClick and that's why any onClick={(e) => e.stopPropagation()} can't prevent to open the dropdown. You need to add onMouseDown={(e) => e.stopPropagation()} along with the onClick handler to open only the popover.

In your code the snippet below should do the trick:

    <span onMouseDown={e => e.stopPropagation()} style={styles.root}>
      <span style={styles.label}>{label}</span>
      <OverlayTrigger
        trigger="click"
        rootClose
        placement="bottom"
        overlay={popover}
        animation={false}
      >
        <span style={styles.trigger}>{`${selected} / ${contacts.length}`}</span>
      </OverlayTrigger>
    </span>

Well I found a way to cancel the event. I just add an onValueClick prop to the <Select /> and put the stopPropagation there!

发布评论

评论列表(0)

  1. 暂无评论