primereact icon indicating copy to clipboard operation
primereact copied to clipboard

Add closeMode to TabView

Open diogoko opened this issue 3 years ago • 1 comments

This new property fixes the problem described in #2842.

Context

Currently, it's not possible to dynamically create closable TabPanels in TabView because the default behavior of the tab header close button is to hide the tab by index. More precisely, it adds the tab index to the hiddenTabsState array. If the onTabClose is also handled and programmatically removes the tab from the list used to generate the children TabPanel components, then 2 tabs are removed at once (the one that was closed and the one immediately following it).

This is an example of the kind of code that causes this bug:

<TabView onTabClose={(e) => setDynamicTabs([...dynamicTabs.slice(0, e.index), ...dynamicTabs.slice(e.index + 1)])}>
    {dynamicTabs.map((tab) => {
        return (
            <TabPanel key={tab.id} header={tab.name} closable>
                <p>Name: {tab.name}</p>
            </TabPanel>
        );
    })}
</TabView>

Description of changes

This PR adds a new closeMode property to TabView. Its value is "auto" by default; when "manual", clicking the default close button of a closable TabPanel will not add the index to the hiddenTabsState array and it's the programmer's responsibility to update the list used to generate the dynamic TabPanel components.

This is the above example with the new property:

<TabView closeMode="manual" onTabClose={(e) => setDynamicTabs([...dynamicTabs.slice(0, e.index), ...dynamicTabs.slice(e.index + 1)])}>
    {dynamicTabs.map((tab) => {
        return (
            <TabPanel key={tab.id} header={tab.name} closable>
                <p>Name: {tab.name}</p>
            </TabPanel>
        );
    })}
</TabView>

I also updated the docs and TS definitions.

diogoko avatar Sep 08 '22 13:09 diogoko

Thanks for the PR! I assigned Mert to review but looks good to me.

melloware avatar Sep 08 '22 13:09 melloware

Can you rebase this PR there are conflicts.

melloware avatar May 10 '23 20:05 melloware

Can you rebase this PR there are conflicts.

Sure! Just need a couple of days.

diogoko avatar May 15 '23 19:05 diogoko

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

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
primereact ⬜️ Ignored (Inspect) May 30, 2023 2:47pm

vercel[bot] avatar May 29 '23 23:05 vercel[bot]

@melloware I've rebased it from the current master and redone the docs in the 9.x format.

diogoko avatar May 29 '23 23:05 diogoko

I actually think this is fixed by https://github.com/primefaces/primereact/issues/5229

melloware avatar Nov 06 '23 18:11 melloware