Semantic-ui-react: Checkbox: onClick doesn't fire when inside a Dropdown

Created on 25 Sep 2017  路  12Comments  路  Source: Semantic-Org/Semantic-UI-React

Steps

Add a checkbox inside a dropdown, either as header or item.

Expected Result

  • onClick fires

Actual Result

  • onClick doesn't fire
  • also note the weird font

Version

0.74.1

Testcase

https://codepen.io/anon/pen/zENBPx

bug good first issue help wanted

Most helpful comment

Looks like the Dropdown is overriding onClick when looping over the options and creating DropdownItems. Seen here.

Workaround

Use onMouseDown instead. Working fork https://codepen.io/levithomason/pen/qPRqRg.

Fix

The DropdownItem.create() factory call needs to no override the onClick handler, but wrap it. An example of this is shown in the same file, Dropdown.js, where handleIconOverrides() is used to wrap the icon handlers and not override them.

All 12 comments

Looks like the Dropdown is overriding onClick when looping over the options and creating DropdownItems. Seen here.

Workaround

Use onMouseDown instead. Working fork https://codepen.io/levithomason/pen/qPRqRg.

Fix

The DropdownItem.create() factory call needs to no override the onClick handler, but wrap it. An example of this is shown in the same file, Dropdown.js, where handleIconOverrides() is used to wrap the icon handlers and not override them.

Note, children are not supported in a Dropdown. Use the options prop instead. The children API will be removed.

I'll have a go at getting it fixed.

@levithomason I don't think that DropdownItem is the issue. The Checkbox is displayed inside a DropdownHeader after all, not inside an Item. handleItemClick is not called.

Adding multiple={true} fixes the problem, but it's something I'd prefer not to do.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

don't close this issue bot

@levithomason or anyone here, what's the update on this issue? any ideas on how to fix?

I noticed that when you click on other Items or Headers, the order of called Dropdown methods are:

image

But when you click on the checkbox its order is:

image

So it appears that the blur event is being called before the mouseDown event which causes the close method of Dropdown to be called without the isMouseDown property being set in the handleMouseDown method. I tried with vanilla html and the order is mouseDown->blur so I am a bit confused. I will keep looking into this.

Hi,

I gave this issue a shot and implemented what was suggested by @levithomason in a previous comment. But this did not work as the onClick handler will never be called. I checked this by adding a debug() output into onClick.

As it was also suggested to not use children, but instead use the options property, I changed the example to do so. But this also did not change anything. The onClick handler on the DropdownItem is simply not called. My guess is, that handleMouseDown() captures the event so it is not being further propagated.

I could hook into handleMouseDown() and propagate onClick, but this kind of feels like a hack. Are there any other ideas?

I tried to replicate the workaround @levithomason shared and it worked for me.

Sandbox

I would like to trigger a simple onClick :

public onClick = () => this.setState({loading: true})

That would display the loading prop of the dropdown object onOpen, but it doesn't seem to work.
onOpen, onMouseDown, onFocus don't work either...

Edit : onOpen is perfectly working, i was too dumb to check my shouldComponentUpdate. Sorry.

Note, I've given a workaround and a proper fix here https://github.com/Semantic-Org/Semantic-UI-React/issues/2121#issuecomment-331966524. I've also included a reference to the proper pattern in another component that can be copied. Any PR implementing this would be much appreciated!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jayphelps picture jayphelps  路  3Comments

hankthewhale picture hankthewhale  路  3Comments

nix1 picture nix1  路  3Comments

levithomason picture levithomason  路  3Comments

laukaichung picture laukaichung  路  3Comments