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

Multiselect dropdown closes unexpectedly when deselecting last selected item #1634

Open
joshhunt opened this issue Jan 23, 2025 · 0 comments

Comments

@joshhunt
Copy link

We are using useMultipleSelection with useCombobox to create a multiselect dropdown that keeps selected items visible in the menu.

When clicking an item in the menu that's already selected, and that item is last in the selectedItems list, the dropdown unexpectedly closes. This appears to be caused by the focus management logic in useMultipleSelection which seems to be built with the assumption that you can only remove items via the selected item 'pills'?

It looks like the relevant code is

case stateChangeTypes.FunctionRemoveSelectedItem: {
let newActiveIndex = activeIndex
const selectedItemIndex = selectedItems.findIndex(
item => props.itemToKey(item) === props.itemToKey(selectedItem),
)
if (selectedItemIndex < 0) {
break
}
if (selectedItems.length === 1) {
newActiveIndex = -1
} else if (selectedItemIndex === selectedItems.length - 1) {
newActiveIndex = selectedItems.length - 2
}

and

if (activeIndex === -1 && dropdownRef.current) {
dropdownRef.current.focus()
} else if (selectedItemRefs.current[activeIndex]) {
selectedItemRefs.current[activeIndex].focus()
}

I don't yet have a minimal standalone reproduction, but our component is here https://github.com/grafana/grafana/blob/cf631d0c9f5f8cb07f2988bc3de3b713adee1c58/packages/grafana-ui/src/components/Combobox/MultiCombobox.tsx

If a standlone reproduction would help, let me know and ill try and get one up on stackblitz or something.

I'm happy to contribute a fix, but I don't completely understand the downshift code i linked to, or understand exactly what causes the problem, so I would need some pointers.

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

1 participant