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

Possibility to load next element via arrow keys without opening? #302

Closed andorfermichael closed 1 year ago

andorfermichael commented 1 year ago

Is there an easy option to select the previous or next element just by using arrow keys without opening the select field?

andorfermichael commented 1 year ago

I solved this using the @keydown event as follows:

function onKeydown($event: any, select$: any) {
    if ($event.key === 'ArrowLeft') {
        const currentIndex = select$.options.findIndex((option: SelectFieldOption) => {
            return select$.isSelected(option)
        })

        let nextIndex: number = currentIndex
        if (nextIndex > 0) {
            nextIndex = currentIndex - 1
        }

        select$.select(select$.options[nextIndex].value)
    }

    if ($event.key === 'ArrowRight') {
        const currentIndex = select$.options.findIndex((option: SelectFieldOption) => {
            return select$.isSelected(option)
        })

        let nextIndex: number = currentIndex
        if (nextIndex + 1 < select$.options.length) {
            nextIndex = currentIndex + 1
        }

        select$.select(select$.options[nextIndex].value)
    }
}