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

Fix incorrect active option in the Listbox/Combobox component #1264

Merged
merged 3 commits into from
Mar 21, 2022

Conversation

RobinMalfait
Copy link
Member

This pr fixes an incorrect active option

The React code had a bug in the Listbox and Combobox components where it incorrectly made the first selected value the active value.

The first selected option should be the active option when you open the listbox. However when you already had the component in an open state, hovered over a non-selected item and them left the option by moving it to the body then the first selected option became the active one again.

This made sense because we used a useEffect in each option to make it the active one if it was also selected. Since every component re-renders, code got called and the bug arises.

Now, instead we moved the logic to make it the active option to the reducer logic. We will check it when we register an option and doesn't have an active option index yet or when we open the Listbox/Combobox.

This should also solve the strange scrolling behaviour where the options scroll up if you have more options than you display.

The React code had a bug in the Listbox and Combobox components where it
incorrectly made the first selected value the active value.

The first selected option should be the active option when you open the
listbox. However when you already had the component in an `open` state,
hovered over a non-selected item and them left the option by moving it
to the body then the first selected option became the active one again.

This made sense because we used a `useEffect` in each option to make it
the active one if it was also selected. Since every component
re-renders, code got called and the bug arises.

Now, instead we moved the logic to make it the active option to the
reducer logic. We will check it when we register an option and doesn't
have an active option index yet or when we open the Listbox/Combobox.

This should also solve the strange scrolling behaviour where the options
scroll up if you have more options than you display.
@vercel
Copy link

vercel bot commented Mar 21, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

headlessui-react – ./packages/playground-react

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-react/7fNnPwD4FsNtkDZ8kR4mKBpR5k51
✅ Preview: https://headlessui-react-git-fix-incorrect-active-option-tailwindlabs.vercel.app

headlessui-vue – ./packages/playground-vue

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-vue/21cR74QjMSYSgpn8piArTzYmeDUR
✅ Preview: https://headlessui-vue-git-fix-incorrect-active-option-tailwindlabs.vercel.app

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

Successfully merging this pull request may close these issues.

1 participant