Combobox + NextJS Link doesn't work with keyboard navigation (Enter) #1821
-
Hi All, I'm using the following workaround this use Combobox with Link from NextJS, which works great when using mouse navigation: https://headlessui.com/react/menu#integrating-with-next-js However, when using keyboard navigation, "Enter" specifically, it selects the correct choice but it doesn't follow the link. Does anyone have a solution for this>
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey! Thank you for your question! Please attach a minimal reproduction repo so that we can take a look. |
Beta Was this translation helpful? Give feedback.
-
It also looks like you are mixing concerns here a bit. A Instead, I would recommend you to redirect to the correct location based on the import { useState } from 'react'
import { useRouter } from 'next/router'
import { Combobox } from '@headlessui/react'
const people = [
'Durward Reynolds',
'Kenton Towne',
'Therese Wunsch',
'Benedict Kessler',
'Katelyn Rohan',
]
function MyCombobox() {
const router = useRouter()
const [selectedPerson, setSelectedPerson] = useState(people[0])
const [query, setQuery] = useState('')
const filteredPeople =
query === ''
? people
: people.filter((person) => {
return person.toLowerCase().includes(query.toLowerCase())
})
return (
<Combobox value={selectedPerson} onChange={(person) => router.push(`/people/${person.id}`)}>
<Combobox.Input onChange={(event) => setQuery(event.target.value)} />
<Combobox.Options>
{filteredPeople.map((person) => (
<Combobox.Option key={person} value={person}>
{person}
</Combobox.Option>
))}
</Combobox.Options>
</Combobox>
)
} |
Beta Was this translation helpful? Give feedback.
It also looks like you are mixing concerns here a bit.
A
Menu
can be used for actions and links, however aCombobox
is used fordata
, that's why you can select a value from the list.Instead, I would recommend you to redirect to the correct location based on the
value
, inside theonChange
function. For Next.js that could look like this: