ekwonye-richard / react-flags-select

Customizable svg flags select components for React Js
194 stars 121 forks source link

The dropdown menu doesn't close after select #126

Open truonghungit opened 2 years ago

truonghungit commented 2 years ago

Descriptions

I try to use the react-flags-select on reactstrap modal The dropdown menu doesn't auto-close after selecting the item/clicking outside

raresherta commented 2 years ago

Hei @truonghungit I have the same issue, did you manage somehow to solve this ?

andreamazz commented 2 years ago

I'm having the same issue, I noticed that it only happens when the control is mounted inside a label tag.

<label>
  <span>Country</span>
  <ReactFlagsSelect
    selected={selected}
    onSelect={(code) => setSelected(code)}
  />
</label>

It looks like that in this case when you click on an item, two click events are triggered, one in the list item and one in the button (even if it's nowhere near the mouse target). This triggers the correct setIsDropdownOpen(false) but right after the button click handler toggles it opened again. I reckon this happens since the default behaviour when clicking a label is to forward the click to its children. One workaround I found is to prevent the default handler:

<label onClick={e => e.preventDefault()}>
  <span>Country</span>
  <ReactFlagsSelect
    selected={selected}
    onSelect={(code) => setSelected(code)}
  />
</label>
DaywisonSilva commented 1 year ago

@ekwonye-richard can i work on it?

souri84 commented 1 year ago
<label onClick={e => e.preventDefault()}>

Thank yo uvery much ! It works nice !!

maciekgrzela commented 1 year ago

In my case preventDefault() does nothing :( But: <label onClick={e => e.stopPropagation()} completely solves the issue :)