Skip to content

Commit 4030a24

Browse files
committed
WIP: add tabId
1 parent c921b04 commit 4030a24

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

packages/kit-headless/src/components/tabs/tabs.spec.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ describe('Tabs', () => {
186186
);
187187
});
188188

189+
describe('Manual Tab Ids', () => {
190+
it(`GIVEN 2 tabs and tab ids are set on both tabs and panels
191+
WHEN clicking on the second tab
192+
THEN the second panel should be displayed
193+
`, () => {
194+
const ManualTabIdsComponent = component$(() => {
195+
return (
196+
<Tabs>
197+
<TabList>
198+
<Tab tabId="first">Tab 1</Tab>
199+
<Tab tabId="second">Tab 2</Tab>
200+
</TabList>
201+
<TabPanel tabId="first">Panel 1</TabPanel>
202+
<TabPanel tabId="second">Panel 2</TabPanel>
203+
</Tabs>
204+
);
205+
});
206+
207+
cy.mount(<ManualTabIdsComponent />);
208+
209+
cy.findByRole('tab', { name: /Tab 2/i }).click();
210+
211+
cy.findByRole('tabpanel').should('contain', 'Panel 2');
212+
});
213+
});
214+
189215
describe('Tabs inside of tabs', () => {
190216
it(`GIVEN tabs inside of tabs
191217
WHEN clicking on the root second tab

packages/kit-headless/src/components/tabs/tabs.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
7373
const childrenToProcess = (
7474
Array.isArray(myChildren) ? [...myChildren] : [myChildren]
7575
) as JSXNode[];
76-
let tabListElement: JSXNode | undefined;
76+
let tabListComponent: JSXNode | undefined;
7777
const tabComponents: JSXNode[] = [];
7878
const panelComponents: JSXNode[] = [];
7979
const tabInfoList: TabInfo[] = [];
@@ -93,7 +93,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
9393

9494
switch (child.type) {
9595
case TabList: {
96-
tabListElement = child;
96+
tabListComponent = child;
9797
const tabListChildren = Array.isArray(child.props.children)
9898
? child.props.children
9999
: [child.props.children];
@@ -103,7 +103,8 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
103103
}
104104
case Tab: {
105105
if (child.props.selected) {
106-
selectedIndex = tabComponents.length;
106+
const currentTabIndex = tabComponents.length;
107+
selectedIndex = currentTabIndex;
107108
child.props.selected = undefined;
108109
}
109110
tabComponents.push(child);
@@ -112,7 +113,9 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
112113
case TabPanel: {
113114
const { label, selected } = child.props;
114115
// The consumer must provide a key if they change the order
115-
const tabIdFromTabMaybe = tabComponents[panelIndex]?.key;
116+
const matchedTabComponent = tabComponents[panelIndex];
117+
const tabIdFromTabMaybe =
118+
matchedTabComponent?.props.tabId || matchedTabComponent?.key;
116119
const tabId = tabIdFromTabMaybe || child.key || `${panelIndex}`;
117120

118121
if (label) {
@@ -159,12 +162,12 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
159162
tabInfoList[index].tabProps = tab.props;
160163
});
161164

162-
if (tabListElement) {
163-
tabListElement.children = tabComponents;
164-
tabListElement.props.children = tabComponents;
165+
if (tabListComponent) {
166+
tabListComponent.children = tabComponents;
167+
tabListComponent.props.children = tabComponents;
165168
} else {
166169
// Creating it as <TabList /> and adding children later doesn't work
167-
tabListElement = <TabList>{tabComponents}</TabList>;
170+
tabListComponent = <TabList>{tabComponents}</TabList>;
168171
}
169172

170173
if (typeof selectedIndex === 'number') {
@@ -173,7 +176,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
173176

174177
return (
175178
<TabsImpl tabInfoList={tabInfoList} {...rest}>
176-
{tabListElement}
179+
{tabListComponent}
177180
{panelComponents}
178181
</TabsImpl>
179182
);

0 commit comments

Comments
 (0)