[TreeView] Keyboard navigation gets broken when focused back with `Shift + Tab` keypress
Duplicates
- [X] I have searched the existing issues
Latest version
- [X] I have tested the latest version
Steps to reproduce 🕹
Link to live example: https://mui.com/material-ui/react-tree-view/
Steps:
- Focus with keyboard on the file system tree view
- Expand the first item, put focus on first child of first item and press spacebar to select it
- Press left arrow twice in order to collapse the father.
- Press tab to get focus out of the tree
- Press Shift+Tab to get focus in the tree again
- Try to navigate it with arrow keys, it will throw error on the console, and the tree wont work
Current behavior 😯
The keyboard navigation gets broken
Expected behavior 🤔
The keyboard navigation to work ok
Context 🔦
No response
Your environment 🌎
The error is in the material UI documentation website
I am not able to shift the focus back to the tree again with Shift + Tab in Windows. @AlexisLeite Can you help me to reproduce?
Well, that is exactly the problem. But in fact, the focus is in the hidden node of the tree. And as the step mui/material-ui#6 says, if you open the browser console you will see some errors thrown by the tree
@AlexisLeite But since I am not able to focus back to the Tree view with Shift + Tab key, I am not able to reproduce the error in console mentioned in point 6 by navigating with the arrow keys.
Edit: Okay, so it focuses back to a hidden node. I got the following error:
Uncaught TypeError: Cannot read properties of undefined (reading 'parentId')
You cannot see the focus but the element is focused anyway. And if you open the console and then go through the mentioned steps, you should come to the following situation: https://vimeo.com/808688486
EDIT: Exactly as you mentioned in your edit
I investigated a bit locally, but I am not aware of the internals of the Tree View component, so I don't know where it focuses back to after the Shift + Tab key operation. And it definitely should not throw a console error. So I am marking it as a bug.
It looks like child nodes are removed when the parent is collapsed. I think the issue is that tabindex=0 is only ever applied to the root UL element and never changes. The tree items never change from tabindex=-1. That root UL doesn't seem to register any key presses, so it can technically be focused but then can't do anything. (And it's an issue in both directions; you can put focus before the tree and tab to it, or shift-tab back to it and still have this issue.)
I'd expect a roving tabindex where the last-focused item (not the last-selected) gets tabindex=0. Then that item is what's in the tab order when you navigate away and back.
@scardamalias Would you like to give it a try to fix it?