Skip to content
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

Correct focus changes based on mouseDown result #192

Merged
merged 2 commits into from Jan 28, 2020

Conversation

ilyamkin
Copy link
Contributor

Because userEvent.click should simulate real browsers' behavior, we need to take into account the result of each event.

In this particular case if mouseDown event has preventDefault, it should stop focus event.

Here is the source code of Google Chrome Blink for reference (others follow the same convention):
https://chromium.googlesource.com/chromium/blink/+/master/Source/core/input/EventHandler.cpp#972

bool swallowEvent = !dispatchMouseEvent(EventTypeNames::mousedown, mev.innerNode(), m_clickCount, mouseEvent);

dispatchMouseEvent return value means "continue default handling", so swallowEvent is true in case of preventDefault usage.

Just below there is call for focus event which is triggered only if swallowEvent === false

swallowEvent = swallowEvent || handleMouseFocus(MouseEventWithHitTestResults(mouseEvent, hitTestResult), sourceCapabilities);

We heavily rely on this case when developing complex components for the design system with focus management.

Another moment is that we probably should use fireEvent.focus() instead of element.focus() to also understand if onFocus was canceled, but that's for another change (related to scrollbar behavior on focus).

@codecov
Copy link

codecov bot commented Dec 12, 2019

Codecov Report

Merging #192 into master will increase coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #192      +/-   ##
==========================================
+ Coverage   97.05%   97.12%   +0.06%     
==========================================
  Files           1        1              
  Lines         136      139       +3     
  Branches       35       36       +1     
==========================================
+ Hits          132      135       +3     
  Misses          4        4
Impacted Files Coverage Δ
src/index.js 97.12% <100%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9ab5b2b...a8d2a5b. Read the comment docs.

@Gpx
Copy link
Member

Gpx commented Dec 19, 2019

Thank you for this PR. Do you mind adding some tests for this use-case?

@ilyamkin
Copy link
Contributor Author

@Gpx Sure thing. Done.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Thank you :D

@kentcdodds kentcdodds merged commit ed59735 into testing-library:master Jan 28, 2020
@Gpx
Copy link
Member

Gpx commented Jan 28, 2020

🎉 This PR is included in version 8.0.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Gpx Gpx added the released label Jan 28, 2020
@ilyamkin
Copy link
Contributor Author

@kentcdodds Thanks for merging this. Would you add me via all contributors bot, please? 🙏

@kentcdodds
Copy link
Member

My bad. Sorry I forgot

@kentcdodds
Copy link
Member

@all-contributors please add @ilyamkin for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @ilyamkin! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants