Skip to content

Commit 3153397

Browse files
author
Kubit
committed
Add unMount prop to Tabs
Include the posibilitie to unmount or not the content of the Tabs component
1 parent 32d0f97 commit 3153397

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

src/components/tabs/__tests__/primaryTabs.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { renderProvider } from '@/tests/renderProvider/renderProvider.utility';
99
import { windowMatchMedia } from '@/tests/windowMatchMedia';
1010
import { DeviceBreakpointsType, ROLES } from '@/types';
1111

12+
import { TabsControlled } from '../tabsControlled';
1213
import { TabsUnControlled } from '../tabsUnControlled';
1314
import { ITabsUnControlled, PrimaryTabTabType } from '../types';
1415

@@ -132,4 +133,24 @@ describe('Tabs component', () => {
132133
selectedTab: 1,
133134
});
134135
});
136+
137+
it('does not render content when unMountContent is true and selectedTab is undefined', () => {
138+
renderProvider(<TabsUnControlled {...mockProps} unMountContent={true} />);
139+
expect(screen.queryByText('Tab 1 content')).not.toBeInTheDocument();
140+
expect(screen.queryByText('Tab 2 content')).not.toBeInTheDocument();
141+
expect(screen.queryByText('Tab 3 content')).not.toBeInTheDocument();
142+
});
143+
144+
it('renders all tabs with correct display style when unMountContent is false', () => {
145+
const { container } = renderProvider(
146+
<TabsControlled {...mockProps} selectedTab={1} unMountContent={false} />
147+
);
148+
const tabs = container.querySelectorAll('[role="tabpanel"]');
149+
expect(tabs).toHaveLength(5);
150+
expect(tabs[0]).toHaveStyle('display: none');
151+
expect(tabs[1]).toHaveStyle('display: block');
152+
expect(tabs[2]).toHaveStyle('display: none');
153+
expect(tabs[3]).toHaveStyle('display: none');
154+
expect(tabs[4]).toHaveStyle('display: none');
155+
});
135156
});

src/components/tabs/stories/primaryTabs.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const Tabs: Story = {
3232
variant: Object.values(variantsObject[themeSelected].TabsVariantType || {})[0] as string,
3333
leftIcon: { icon: ICONS.ICON_CHEVRON_UP },
3434
rightIcon: { icon: ICONS.ICON_CHEVRON_DOWN },
35+
unMountContent: true,
3536
content: [
3637
<ReplaceContent key="content-0" width="100%">
3738
Content first tab

src/components/tabs/tabs.styled.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ export const LabelHiddenContainer = styled.span`
167167
display: none;
168168
`;
169169

170-
export const TabsContentStyled = styled.div<StylesType>`
170+
export const TabsContentStyled = styled.div<StylesType & { $display?: string }>`
171171
${({ styles }) => getStyles(styles.contentContainer)}
172+
display: ${({ $display }) => $display};
172173
`;
173174

174175
export const TabsTabListStyled = styled.div<StylesType>`

src/components/tabs/tabsStandAlone.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const TabsStandAloneComponent = (
3636
dataTestId = 'primaryTab',
3737
minTabsInView = MIN_TABS_IN_VIEW,
3838
maxTabsInView = MAX_TABS_IN_VIEW,
39+
unMountContent = true,
3940
...props
4041
}: ITabsStandAlone,
4142
ref: React.ForwardedRef<HTMLDivElement> | undefined | null
@@ -82,19 +83,36 @@ const TabsStandAloneComponent = (
8283
</TabsRightArrowContainerStyled>
8384
);
8485

85-
const buildTabContent = () =>
86-
props.selectedTab !== undefined &&
87-
props.selectedTab !== null && (
88-
<TabsContentStyled
89-
aria-labelledby={`${BASE_ID}-tab-${props.selectedTab}`}
90-
id={TAB_PANEL_ID}
91-
role={ROLES.TABPANEL}
92-
styles={props.styles}
93-
tabIndex={allowFocusTabPanel ? 0 : -1}
94-
>
95-
{props.content?.[props.selectedTab]}
96-
</TabsContentStyled>
97-
);
86+
const buildTabContent = () => {
87+
const commonTokens = {
88+
id: TAB_PANEL_ID,
89+
role: ROLES.TABPANEL,
90+
styles: props.styles,
91+
tabIndex: allowFocusTabPanel ? 0 : -1,
92+
};
93+
94+
return unMountContent
95+
? props.selectedTab !== undefined && props.selectedTab !== null && (
96+
<TabsContentStyled
97+
aria-labelledby={`${BASE_ID}-tab-${props.selectedTab}`}
98+
{...commonTokens}
99+
>
100+
{props.content?.[props.selectedTab]}
101+
</TabsContentStyled>
102+
)
103+
: props.content?.map((cont, index) => {
104+
return (
105+
<TabsContentStyled
106+
key={index}
107+
$display={props.selectedTab === index ? 'block' : 'none'}
108+
aria-labelledby={`${BASE_ID}-tab-${index}`}
109+
{...commonTokens}
110+
>
111+
{cont}
112+
</TabsContentStyled>
113+
);
114+
});
115+
};
98116

99117
return (
100118
<TabsContainerStyled ref={ref} styles={props.styles}>

src/components/tabs/types/tabs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ITabsStandAlone {
2727
hideLabelForSingleTab?: boolean;
2828
dataTestId?: string;
2929
onSelectTab?: (tab: number) => void;
30+
unMountContent?: boolean;
3031
}
3132

3233
export interface ITabsControlled<V = undefined extends string ? unknown : string>

0 commit comments

Comments
 (0)