-
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): prevent onClick
from being called twice
#3351
fix(Checkbox): prevent onClick
from being called twice
#3351
Conversation
onClick
from being called twice
Codecov Report
@@ Coverage Diff @@
## master #3351 +/- ##
==========================================
- Coverage 99.89% 99.89% -0.01%
==========================================
Files 170 170
Lines 2812 2809 -3
==========================================
- Hits 2809 2806 -3
Misses 3 3
Continue to review full report at Codecov.
|
onClick
from being called twiceonClick
from being called twice
Please check the Case 1
|
Sorry @layershifter where can I find these |
They are in the bottom in Checkbox docs: |
Alright thanks, I'll take a look! |
@layershifter I've found a quite curious solution, which was to remove the By doing that, the interactions still work as I've shown in all those gifs and the However the test |
@layershifter I've created four tests for DOM Comparisons, simulating the change event on:
The However, I had to disable the common test My proposition is that we merge this PR because the bug is fixed and the DOM behavior is conformant, however, we should open a new issue to discuss and tackle the empty handler and disabled test (I could open that and start the discussion). What do you say? :) |
@Fabianopb sorry for delay, I will check it after the New Year 🎄 Have happy holidays! |
Thanks @layershifter and Happy New Year! 🎉 |
@Fabianopb I updated tests to work with DOM deeply. It will be awesome if you will be able to take a look and check them 👍 |
It looks good to me @layershifter, nice set up with the I've added two more tests for |
src/modules/Checkbox/Checkbox.js
Outdated
// The browser calls onClick AFTER onChange since onClick is on the root element. | ||
// Because of this, we call onClick in handleChange to preserve the correct order. | ||
// In that case, we skip calls to onClick here to avoid duplicate calls. | ||
if (onChange) return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this cause issues in case the user has both onClick
and onChange
defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Digging a bit deeper, this change actually breaks the use of onClick
like described in #3348 , I've updated the tests on controlled components to warn us accordingly. You will notice that if you revert this commit, the tests will pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the double checking. I have a couple more updates I'll push, then I think we can get a final on this and merge it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking again, it's a bit misleading to have an empty handler like I had done before, I'll push a new commit removing it completely and adding the explanation close to the onClick invocation.
Some 10 days since the last discussions, just checking, what's the status? :) |
I can't promise anything, but I'm going to check this PR again tomorrow and merge it. I know that @levithomason worked on another solution (may be cleaner), but for now, it's better to fix the issue. |
…React into checkbox-double-onclick
Merged, @Fabianopb thanks for working on it 👍 |
Awesome @layershifter thanks! |
@@ -13,6 +13,7 @@ import hasValidTypings from './hasValidTypings' | |||
* Assert Component conforms to guidelines that are applicable to all components. | |||
* @param {React.Component|Function} Component A component that should conform. | |||
* @param {Object} [options={}] | |||
* @param {String[]} [options.disabledHandlers=[]] An array of listeners that are disabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads up @layershifter we'll need to undo this sometime soon. Ignoring events means we aren't testing that all events work correctly for the end user. I don't think it is high priority, but something to keep on our tech debt list.
Released in |
Alright, I have to say that this was not trivial at all, but now the handlers seem to work for all cases. After plenty of investigation and trying different approaches, the solution was to leave the call of
onClick
to the click handler.Here are gifs showing the component working for three cases (in all of them I'm checking and unchecking with clicks and then with the spacebar):
Simple uncontrolled component
Controlled component with
setState
as an objectControlled component with
setState
as a function (maintaining the functionality implemented for Checkbox: doesn't toggle its state, when using only the keyboard #2587 fixed in fix(Checkbox): match DOM checkbox behavior #2748)Cheers and Merry Xmas! 🎄
(Closes #3348)