Skip to content

Commit eb40287

Browse files
committed
feat(headless/tabs): add tabId to tab
1 parent 4030a24 commit eb40287

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export type TabProps = {
2020
disabled?: boolean;
2121
selected?: boolean;
2222
selectedClassName?: string;
23+
tabId?: string;
2324

2425
/** @deprecated Internal use only */
2526
_extraClass?: QwikIntrinsicElements['div']['class'];
2627
/** @deprecated Internal use only */
27-
_tabId?: string;
2828
} & QwikIntrinsicElements['button'];
2929

3030
export const preventedKeys = [
@@ -39,28 +39,28 @@ export const preventedKeys = [
3939
];
4040

4141
export const Tab = component$(
42-
({ selectedClassName, _extraClass, _tabId, ...props }: TabProps) => {
42+
({ selectedClassName, _extraClass, tabId, ...props }: TabProps) => {
4343
const contextService = useContext(tabsContextId);
4444

4545
const elementRefSig = useSignal<HTMLElement | undefined>();
4646

47-
const fullTabElementId = contextService.tabsPrefix + TAB_ID_PREFIX + _tabId!;
48-
const fullPanelElementId = contextService.tabsPrefix + TAB_PANEL_ID_PREFIX + _tabId!;
47+
const fullTabElementId = contextService.tabsPrefix + TAB_ID_PREFIX + tabId!;
48+
const fullPanelElementId = contextService.tabsPrefix + TAB_PANEL_ID_PREFIX + tabId!;
4949

5050
const selectedClassNameSig = useComputed$(() => {
5151
return selectedClassName || contextService.selectedClassName;
5252
});
5353

5454
const isSelectedSig = useComputed$(() => {
55-
return contextService.selectedTabIdSig.value === _tabId;
55+
return contextService.selectedTabIdSig.value === tabId;
5656
});
5757

5858
useVisibleTask$(function preventDefaultOnKeysVisibleTask({ cleanup }) {
5959
function handler(event: KeyboardEvent) {
6060
if (preventedKeys.includes(event.key as KeyCode)) {
6161
event.preventDefault();
6262
}
63-
contextService.onTabKeyDown$(event.key as KeyCode, _tabId!);
63+
contextService.onTabKeyDown$(event.key as KeyCode, tabId!);
6464
}
6565
// TODO put the listener on TabList
6666
elementRefSig.value?.addEventListener('keydown', handler);
@@ -70,7 +70,7 @@ export const Tab = component$(
7070
});
7171

7272
const selectIfAutomatic$ = $(() => {
73-
contextService.selectIfAutomatic$(_tabId!);
73+
contextService.selectIfAutomatic$(tabId!);
7474
});
7575

7676
const classNamesSig = useComputed$(() => [
@@ -96,7 +96,7 @@ export const Tab = component$(
9696
(props.class as Signal<string>)?.value ?? (props.class as string),
9797
classNamesSig.value
9898
]}
99-
onClick$={[$(() => contextService.selectTab$(_tabId!)), props.onClick$]}
99+
onClick$={[$(() => contextService.selectTab$(tabId!)), props.onClick$]}
100100
>
101101
<Slot />
102102
</button>

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,29 @@ describe('Tabs', () => {
187187
});
188188

189189
describe('Manual Tab Ids', () => {
190-
it(`GIVEN 2 tabs and tab ids are set on both tabs and panels
190+
it(`GIVEN 2 tabs and tab ids are set on tabs
191191
WHEN clicking on the second tab
192-
THEN the second panel should be displayed
192+
THEN the second panel should be displayed
193+
and the selectTabId should match the second tab id
193194
`, () => {
194195
const ManualTabIdsComponent = component$(() => {
196+
const selectedTabIdSig = useSignal<string | undefined>();
195197
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>
198+
<>
199+
<Tabs bind:selectedTabId={selectedTabIdSig}>
200+
<TabList>
201+
<Tab tabId="first">Tab 1</Tab>
202+
<Tab tabId="second">Tab 2</Tab>
203+
</TabList>
204+
<TabPanel>Panel 1</TabPanel>
205+
<TabPanel>Panel 2</TabPanel>
206+
</Tabs>
207+
{selectedTabIdSig.value && (
208+
<div data-testid="selected-tab-id-from-event">
209+
Selected tab id: {selectedTabIdSig.value}
210+
</div>
211+
)}
212+
</>
204213
);
205214
});
206215

@@ -209,6 +218,7 @@ describe('Tabs', () => {
209218
cy.findByRole('tab', { name: /Tab 2/i }).click();
210219

211220
cy.findByRole('tabpanel').should('contain', 'Panel 2');
221+
cy.findByTestId('selected-tab-id-from-event').should('contain', 'second');
212222
});
213223
});
214224

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
157157
tabComponents.forEach((tab, index) => {
158158
const tabId = tabInfoList[index]?.tabId;
159159
tab.key = tabId;
160-
tab.props._tabId = tabId;
160+
tab.props.tabId = tabId;
161161
tab.props._extraClass = tabClass;
162162
tabInfoList[index].tabProps = tab.props;
163163
});

0 commit comments

Comments
 (0)