material-tailwind icon indicating copy to clipboard operation
material-tailwind copied to clipboard

Tabs: Unable to programmatically change tabs

Open tdanh2k opened this issue 1 year ago • 6 comments

First of all, thank you for an amazing product. Been using it for weeks now without any issues. May I request that Tabs component exposes its state or any thing of sort so that tabs can be programmatically switched? Cause right now what I'm doing is this:

<Tabs key={`${openTab}-tab`} value={openTab}>

which re-renders the entire Tabs component when openTab is changed. Thank you in advance.

tdanh2k avatar Jun 15 '23 06:06 tdanh2k

That's my problem

vuphan0804 avatar Jun 19 '23 07:06 vuphan0804

Facing same issue.

hassaanmasood avatar Jun 28 '23 04:06 hassaanmasood

This would indeed be very helpful. To give context about my use case, Tabs are actually links to different sections. Therefore when users browse back for example using the back button, I would expect the tab indicator to move accordingly to the tab representing the current pathname.

luisanton-io avatar Aug 04 '23 10:08 luisanton-io

Its worked for me. after setting the active Tab

Initialize activeTab const [activeTab, setActiveTab] = React.useState("contact");

useEffect(() => { const tabButton:any = document.querySelector(li[data-value=${activeTab}]); if (tabButton) { tabButton.click(); } }, [activeTab]);

Where ever you want to change the Tab, call setActiveTab with your tabname

johnramesh avatar Aug 14 '23 05:08 johnramesh

This would indeed be very helpful. To give context about my use case, Tabs are actually links to different sections. Therefore when users browse back for example using the back button, I would expect the tab indicator to move accordingly to the tab representing the current pathname.

Came back to share what I got just to realize it is very similar to @johnramesh solution - that was a good call! While it def feels like a hack, this is what I eventually ended up with for my specific use-case:

const pathname = usePathname()

useEffect(() => {
    document.querySelector(`ul[role="tablist"] li[href="${pathname}"]`)?.click()
}, [pathname])

luisanton-io avatar Sep 02 '23 15:09 luisanton-io

I would also appreciate this feature!

In my use case, the first tab is the default, but if it's a mobile device, I need to disable it and switch to the second one.

yuuhn avatar Dec 07 '23 20:12 yuuhn