vueform / multiselect

Vue 3 multiselect component with single select, multiselect and tagging options (+Tailwind CSS support).
https://vueform.com
MIT License
808 stars 151 forks source link

How to focus the selected element on open? #301

Closed andorfermichael closed 1 year ago

andorfermichael commented 1 year ago

It would be very helpful if the selected option would be focused on when the dropdown is opened. It always focuses on the first element but, if you have a very long list, then it can be very uncomfortable to go to the selected option again. A use case would be if you navigate with the arrow keys and use the select field to load data, then you simply want to go to the next option via arrow keys.

andorfermichael commented 1 year ago

It seems that

select$.setPointer(value)

e.g.

function onOpen(select$: any) {
    // Focus selected element
    select$.setPointer(value)
}

solved it.

andorfermichael commented 1 year ago

However, it seems there is a bug. If I use keyboard only it works as expected, but if I select a value with mouse first and reopen and select with keyboard and reopen the wrong element is focused.

adamberecz commented 1 year ago

You should use:

onOpen(select$) {
  select$.setPointer(select$.getOption(this.multiselectModelValue))
}

With this I can't reproduce the issue you describe.

andorfermichael commented 1 year ago

For me, it still does not work (using select$.setPointer(select$.getOption(this.multiselectModelValue))). If I use the mouse, and reopen it, the pointer is always set to the "first" selected one, even if I set another one as selected via enter key.

I have the feeling that there are two different behaviors:

If the mouse and arrow are mixed it is mixed up somehow.

adamberecz commented 1 year ago

Closing this because it's not reproducible. If you want to reopen please do a screencast and copy the exact Multiselect config or create a fiddle example with exact steps to reproduce.