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

Tree view component steals the focus when the last focused tree item gets removed from the DOM #1436

Closed
alenaksu opened this issue Jul 8, 2023 · 4 comments

Comments

@alenaksu
Copy link
Collaborator

alenaksu commented Jul 8, 2023

Originally posted by @Lukinoh in #1428

I was wondering why do we focus the first available item when we remove the selected item ?

// If the focused item has been removed form the DOM, move the focus to the first focusable item
if (removedNodes.includes(this.lastFocusedItem)) {
this.focusItem(this.getFocusableItems()[0]);
}

Just to give a bit of context, I have a tree with a list of items, and based on a input search, the tree is filtered.
However, the behaviour is quite strange due to those lines.
When the selected item is removed from the list, it focuses the first element, and the focus of the input is lost, which is not very practical.

msedge_vVDGITjdPP

You can find a reproduction here.
To simplify the implementation, I just remove all the items, and add the filtered one, hence the behaviour happens each time.

Best regards.

@claviska
Copy link
Member

This was fixed in #1430, which hasn't been released yet. This decision was made because the logic didn't seem to serve a purpose and, once added, users have no easy way of opting out of it. Instead, we decided to remove it and let users opt in manually if they needed such a feature.

@alenaksu just to make sure I'm understanding correctly, are you advocating for removing the behavior like I did in #1430?

@claviska
Copy link
Member

Update: confirmed from your comment on the original discussion. I should have posted an update there when we fixed this. I'll do that now!

@alenaksu
Copy link
Collaborator Author

Update: confirmed from your comment on the original discussion. I should have posted an update there when we fixed this. I'll do that now!

Yeah sorry I didn't know it was already fixed :S

By the way, I just checked your changes, I think that instead of removing everything, it would be better to check whether the last focused item was removed from the dom, and in that case set lastFocusedItem to null. This is needed because next time the tree gets the focus and the item was removed, it will try to focus a removed item. Furthermore the removed item will not be garbage collected until its reference remains. Does it make sense?

@claviska
Copy link
Member

Good called. Fixed in #1446. Would you mind reviewing when you have a chance, @alenaksu? 🙌

claviska added a commit that referenced this issue Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants