React: How to shift focus using arrow keys? (TreeView)
I have found a solution to move focus within treeView.First of all you should find all your nodes inside your tree.Then you can find focused element using document.activeElement
. After that, you be able to find this item within your array nodes. (document.activeElement == nodes[i]
) and remember index i
. To move focus using arrow keys, just add eventListener
to your node and handle it.
For example, to move upward you can do something like this:
if(arrowUp) { elements[i + 1].focus() }