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

[TreeView] Simplify focus logic #22098

Merged
merged 3 commits into from Aug 7, 2020

Conversation

eps1lon
Copy link
Member

@eps1lon eps1lon commented Aug 7, 2020

react@experimental changed how focus events are handled (they're listening to focusin/focusout now internally) which broke some TreeItem tests. It seems like we can simplify this by relying on React's event system which should be more robust long term.

  • remove event.preventDefault which is a no-op since focusin is not cancelable
  • rename nodeRef -> treeitemElement because it is not a ref (a ref has a { current: T | null } shape)
  • only redirect focus if focus is targeted at the treeitem (and not bubble)
  • don't focus the first selected from the TreeView if focus is bubbled

Verified that this PR indeed fixes tests on react@experimental with eps1lon/material-ui@test/fix-react-next...eps1lon:test/fix-react-experimental

@eps1lon eps1lon added bug 🐛 Something doesn't work component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! labels Aug 7, 2020
@eps1lon eps1lon marked this pull request as ready for review August 7, 2020 10:18
React.useEffect(() => {
if (nodeRef && treeId) {
const handleFocusIn = (event) => {
event.preventDefault();
Copy link
Member

Choose a reason for hiding this comment

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

Nice, I feel like I’ve removed this so many times 😂 But I keep somehow adding it back.

@mui-pr-bot
Copy link

Details of bundle changes

Generated by 🚫 dangerJS against 4cbd6a5

function handleFocus(event) {
// DOM focus stays on the tree which manages focus with aria-activedescendant
if (event.target === event.currentTarget) {
ownerDocument(event.target).getElementById(treeId).focus();
Copy link
Member Author

Choose a reason for hiding this comment

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

Just realized that I no longer guard against falsy treeId. What was the intention behind that @joshwooding ?

Copy link
Member Author

Choose a reason for hiding this comment

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

And if (ownerDocument(nodeRef).activeElement !== tree) guarded against tree being null. I'm not so sure anymore if we should crash or issue a console error (which might be missed).

Copy link
Member

Choose a reason for hiding this comment

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

@eps1lon If the context isn't there it would error but probably not worth guarding against. I think it's fine if it crashed, I didn't intend to guard against a null tree.

@eps1lon eps1lon merged commit d551e6e into mui:next Aug 7, 2020
@eps1lon eps1lon deleted the fix/TreeView/experimental-react branch August 7, 2020 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants