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

Respect selectedIndex for controlled <Tab/> components #3037

Merged
merged 4 commits into from
Mar 15, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Mar 15, 2024

This PR fixes an issue where the selectedIndex doesn't always match the visibly selected tab, when using a controlled <Tab /> component.

The <Tab /> component can be used in 2 ways:

  • Uncontrolled: where the selectedIndex is managed internally by the component itself.
  • Controlled: where the selectedIndex is managed by the parent component (or in other words, you).

When the <Tab /> component is uncontrolled, and thus the state is managed internally, then we try to make sure that we maintain the active tab. For example, if you have the following tabs:

+---------+---------+---------+
| Tab foo | Tab bar | Tab baz |
|         | ------- |         |
+---------+---------+---------+

Right now Tab bar is the active one, if you now add or remove a tab before Tab bar then we still want Tab bar to be the active one.

When Tab foo is deleted:

+---------+---------+
| Tab bar | Tab baz |
| ------- |         |
+---------+---------+

When Tab qux is added:

+---------+----------+---------+---------+
| Tab foo |  Tab qux | Tab bar | Tab baz |
|         |          | ------- |         |
+---------+----------+---------+---------+

This behaviour makes sense, because otherwise you are constantly changing the active tab, which is typically not what you want.

The problem is that we also did this when the component was controlled, but then the selectedIndex value is not in sync with what's actually visible. This means that the behaviour looks like this:

+---------+---------+---------+
| Tab foo | Tab bar | Tab baz |
|         | ------- |         |
+---------+---------+---------+

- Selected index: 1
- Visually selected: 1

When Tab foo is deleted:

+---------+---------+
| Tab bar | Tab baz |
| ------- |         |
+---------+---------+

- Selected index: 1
- Visually selected: 0

When Tab qux is added:

+---------+----------+---------+---------+
| Tab foo |  Tab qux | Tab bar | Tab baz |
|         |          | ------- |         |
+---------+----------+---------+---------+

- Selected index: 1
- Visually selected: 2

Instead, we will now always use the selectedIndex value to determine the active tab, and not try to maintain the active tab when the component is controlled.

+---------+---------+---------+
| Tab foo | Tab bar | Tab baz |
|         | ------- |         |
+---------+---------+---------+

- Selected index: 1
- Visually selected: 1

When Tab foo is deleted:

+---------+---------+
| Tab bar | Tab baz |
|         | ------- |
+---------+---------+

- Selected index: 1
- Visually selected: 1

When Tab qux is added:

+---------+----------+---------+---------+
| Tab foo |  Tab qux | Tab bar | Tab baz |
|         |  ------- |         |         |
+---------+----------+---------+---------+

- Selected index: 1
- Visually selected: 1

Since it is a controlled component, it is up to you to keep the selectedIndex in sync with what you want if you add or remove tabs.

Fixes: #2989

Copy link

vercel bot commented Mar 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 1:34pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 1:34pm

@RobinMalfait RobinMalfait merged commit 834dbf4 into main Mar 15, 2024
8 checks passed
@RobinMalfait RobinMalfait deleted the fix/issue-2989 branch March 15, 2024 13:37
RobinMalfait added a commit that referenced this pull request Apr 15, 2024
* ensure controlled `<Tab>` components respect the `selectedIndex`

* update changelog

* use older syntax in tests

* run prettier to fix lint step
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.

Tabs: selectedIndex is not selecting the correct tab
2 participants