-
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
fix(Checkbox): fix event handling with id
prop
#2392
Conversation
- 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.
Related to: #2370 |
Codecov Report
@@ Coverage Diff @@
## master #2392 +/- ##
==========================================
+ Coverage 99.73% 99.73% +<.01%
==========================================
Files 152 152
Lines 2664 2667 +3
==========================================
+ Hits 2657 2660 +3
Misses 7 7
Continue to review full report at Codecov.
|
This PR reverts removes changes, but it's not a solution. Please see #2274 for content of I'm sure that we will not merge this PR in current condition. |
Hi @layershifter , I don't understand what the htmlFor unless to link between label clicks to inputs? The situation you have which the click in even attached to the input parent make the htmlFor useless. Can you explain to me why htmlFor should be added as a prop in this situation? The goal of htmlFor is to do one thing and your implementation already doing it correctly |
Linked issue has a description, we need In fact, we need a different event handlers for container element and input. |
id
prop
@levithomason I've pushed some changes there. Can you you take attention to them? |
@layershifter Since you're attaching a 2 event handlers, why not keep just one handler at the parent (as it was before) but to extend it using event.target to identify where the click event came from? Also, binding the event on just the input (not the parent container) may solve the issue since the click on the label will also trigger the input thanks to "for" html attribute |
There is different behaviour of |
For clarification, the input is never directly clicked by the user as it is hidden. The box next to the label is actuall a psuedo The other event coming through here is due to the EDIT The solution previously posted was incorrect, will merge the PR as is. Thanks! |
Released in |
onClick seems to be called multiple times on the checkbox. |
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.