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

Improve controlled Tabs behaviour #1050

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Ensure correct order when conditionally rendering `Menu.Item`, `Listbox.Option` and `RadioGroup.Option` ([#1045](https://github.com/tailwindlabs/headlessui/pull/1045))
- Improve controlled Tabs behaviour ([#1050](https://github.com/tailwindlabs/headlessui/pull/1050))

## [Unreleased - @headlessui/vue]

Expand Down
6 changes: 6 additions & 0 deletions packages/@headlessui-react/src/components/tabs/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ describe('Rendering', () => {
// test controlled behaviour
await click(getByText('setSelectedIndex'))
assertTabs({ active: 2 })

// test uncontrolled behaviour again
await click(getByText('Tab 2'))
expect(handleChange).toHaveBeenCalledTimes(2)
expect(handleChange).toHaveBeenNthCalledWith(2, 1)
assertTabs({ active: 1 })
})

it('should jump to the nearest tab when the selectedIndex is out of bounds (-2)', async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/@headlessui-react/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ function Tabs<TTag extends ElementType = typeof DEFAULT_TABS_TAG>(
}, [defaultIndex, selectedIndex, state.tabs, state.selectedIndex])

let lastChangedIndex = useRef(state.selectedIndex)
useEffect(() => {
lastChangedIndex.current = state.selectedIndex
}, [state.selectedIndex])

let providerBag = useMemo<ContextType<typeof TabsContext>>(
() => [
state,
Expand Down
6 changes: 6 additions & 0 deletions packages/@headlessui-vue/src/components/tabs/tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ describe('`selectedIndex`', () => {
// test controlled behaviour
await click(getByText('setSelectedIndex'))
assertTabs({ active: 2 })

// test uncontrolled behaviour again
await click(getByText('Tab 2'))
expect(handleChange).toHaveBeenCalledTimes(2)
expect(handleChange).toHaveBeenNthCalledWith(2, 1)
assertTabs({ active: 1 })
})

it('should jump to the nearest tab when the selectedIndex is out of bounds (-2)', async () => {
Expand Down