Controlled mode for Tabs component in React does not work
See original GitHub issueWhat package within Headless UI are you using?
What version of that package are you using?
v1.6.3
What browser are you using?
Chrome
Reproduction URL
https://codesandbox.io/s/cool-https-tsn49q
Describe your issue
Controlled mode for Tabs component in React does not work, no matter if the state is inside of the component or outside, even if I would put selectedIndex={0}
I still can change the tab.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:6
Top Results From Across the Web
Switching between controlled mode (by using `selectedIndex ...
I ran into this same error while using this library. I was able to resolve it by making sure that the value passed...
Read more >react-tabs - npm
Controlled mode This mode has to be enabled by supplying selectedIndex to the <Tabs /> component. In this mode react-tabs does not handle...
Read more >Uncontrolled Components - React
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by...
Read more >Tabs - Reach UI
Like form inputs, a tab's state can be controlled by the owner. Make sure to include an onChange as well, or else the...
Read more >How to Build a Tabs Component with React - DigitalOcean
Tabs are a common UI component and are important to understand how ... If the project did not open in a browser window,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey! Thank you for your bug report! Much appreciated! 🙏
@ptmdmusique your reproduction helped. This should be fixed by #1680, and will be available in the next release.
I also updated the CodeSandbox with the latest insiders version to see the fix in action: https://codesandbox.io/s/headless-ui-tabs-forked-h8h0lp?file=/src/App.js
You can already try it using:
npm install @headlessui/react@insiders
.npm install @headlessui/vue@insiders
.Hey @RobinMalfait 👋 Thanks a bunch for such a helpful library 🎉
I think the problem OP posted was that we can still change the tab via
Tab
click even if it’s a controlled component and the internal state that’s used to manage the tabs index will be different from the controlled tabHere is the repro sandbox: https://codesandbox.io/s/headless-ui-tabs-hf0lqr?file=/src/App.js
One use case I think this would help solve is when we need to validate something before moving to another tab (e.g: show a notice if there are unsaved changes on the current tab)
But maybe I miss something in the doc. Could you help me take a look ❓