-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkbox: onChange called multiple times in a row #2370
Comments
We had a same problem and we have resolved it using a wrapper component. const CheckBoxComponent = props => { |
Having the same issue with 0.77.1 -- not a problem in 0.76.0 |
I've got a similar problem on Radio (no label). Clicking fires events twice 1) checked=true, 2) checked=false, result in no toggle at all. Reverting to 0.76.0 fixes the problem. |
Added a PR, Hopefully it solves the issue 👍 |
I'm also seeing the change handler being called twice. Thinking that it might be related to the component binding both onChange and onClick to the same handler, I tested focusing the checkbox and toggling with the spacebar. That seems to work; my change handler is only called once per toggle. |
* Fix: [#2370] - onChange triggered twice when passing id attribute Details: The Checkbox Component was rendering a label htmlFor attribute which will bind between the label and the input tag when being clicked. The problem is that the click (change) handler is NOT attached on the input tag but it's attached on the parent <ElementType ... /> component. Below this ElementType we've both the input tag and the label tag (as element children), and once we click on the label the ElementType element will get the click (change) event thanks to Event Bubbling in JavaScript, which therefor will trigger the checkbox state change, but if htmlFor={id} is supplied then clicking on the label tag will trigger the input tag and the ElementType, and input tag click will be bubbled up again into ElementType which will make it trigger the onChange event twice. Since we've the correct behavior of the label being linked to the input because of attaching the event on the parent element then the usage of htmlFor is buggy and already implemented. * Fix lint things * fix(Checkbox): update usage of `id` prop * style(Checkbox): add `id` prop to typings * test(Checkbox): update tests, add new tests
Fixed in #2392 |
I found that the Checkbox element calls its
onChange
event several times each time you check or uncheck the box. It has something to do with the label that is generated alongside the checkbox because when I manually remove the label from the html, the call is only made once.https://codesandbox.io/s/34vrxvlwqp
That link reproduces the issue if you check the console. Notice how the number of times
onChange
is called varies for each checkbox because of its props. For instance, adding an id prop to the Checkbox and clicking its label causes it to call itsonChange
event 3 times, while clicking the actual checkbox causesonChange
to be called twice. I believe this has something to do with the<label for="id">Label</label>
. The number of times called varies based on whether you click the actual checkbox or the label.Seems very inconsistent and you can see that checkbox 3 in the demo cannot even set its value correctly when you click its label because
onChange
is called 3 times.The text was updated successfully, but these errors were encountered: