Skip to content

Commit 1284b1e

Browse files
committed
feat(headless/tabs): add right arrow support
1 parent 4ea7b49 commit 1284b1e

File tree

10 files changed

+141
-22
lines changed

10 files changed

+141
-22
lines changed

cypress.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'cypress';
2+
3+
export default defineConfig({
4+
e2e: {
5+
setupNodeEvents(on, config) {
6+
// implement node event listeners here
7+
},
8+
},
9+
});

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/support/commands.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

cypress/support/e2e.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/e2e.ts is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands';
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export const Tab = component$((props: TabProps) => {
2222
const uniqueId = useId();
2323

2424
useTask$(({ cleanup }) => {
25-
contextService.tabsChanged$();
25+
contextService.onTabsChanged$();
2626

2727
cleanup(() => {
28-
contextService.tabsChanged$();
28+
contextService.onTabsChanged$();
2929
});
3030
});
3131

@@ -50,6 +50,7 @@ export const Tab = component$((props: TabProps) => {
5050
});
5151

5252
const selectTab$ = $(() => {
53+
// TODO: try to move this to the Tabs component
5354
contextService.selectedIndex.value =
5455
contextService.tabsMap[uniqueId]?.index || 0;
5556

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Signal, QRL } from '@builder.io/qwik';
22
import { Behavior } from './behavior.type';
33
import { TabInfo } from './tabs';
4+
import { KeyCode } from '../../utils/key-code.type';
45

56
export interface TabsContext {
67
selectedIndex: Signal<number>;
78
selectedTabId: Signal<string>;
89
selectTab$: QRL<(tabId: string) => void>;
910
showTabs$: QRL<() => void>;
10-
tabsChanged$: QRL<() => void>;
11+
onTabsChanged$: QRL<() => void>;
1112
tabsMap: { [key: string]: TabInfo };
1213
tabPanelsMap: { [key: string]: TabInfo };
1314
behavior: Behavior;
15+
onTabKeyDown$: QRL<(key: KeyCode, tabId: string) => void>;
1416
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export const TabPanel = component$(({ ...props }: TabPanelProps) => {
2121
);
2222

2323
useTask$(({ cleanup }) => {
24-
contextService.tabsChanged$();
24+
contextService.onTabsChanged$();
2525

2626
cleanup(() => {
27-
contextService.tabsChanged$();
27+
contextService.onTabsChanged$();
2828
});
2929
});
3030

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { component$, useSignal, useStore, useTask$ } from '@builder.io/qwik';
1+
import { component$, useSignal, useStore } from '@builder.io/qwik';
22
import { Tab } from './tab';
33
import { Tabs } from './tabs';
44
import { TabList } from './tabs-list';
@@ -110,7 +110,7 @@ describe('Tabs', () => {
110110

111111
cy.checkA11yForComponent();
112112
});
113-
it(`GIVEN 3 tabs
113+
it(`GIVEN 3 tabs
114114
WHEN clicking the middle one
115115
THEN render the middle panel`, () => {
116116
cy.mount(<ThreeTabsComponent />);
@@ -120,7 +120,7 @@ describe('Tabs', () => {
120120
cy.findByRole('tabpanel').should('contain', 'Panel 2');
121121
});
122122

123-
it(`GIVEN 3 tabs
123+
it(`GIVEN 3 tabs
124124
WHEN changing the selected index programmatically to the middle
125125
THEN render the middle panel`, () => {
126126
cy.mount(<DynamicTabsComponent tabsLength={3} changeIndexTo={1} />);
@@ -132,7 +132,7 @@ describe('Tabs', () => {
132132
cy.findByRole('tabpanel').should('contain', 'Dynamic Tab 2 Panel');
133133
});
134134

135-
it(`GIVEN 3 tabs,
135+
it(`GIVEN 3 tabs,
136136
WHEN removing the last one dynamically
137137
THEN only 2 should remain`, () => {
138138
cy.mount(<DynamicTabsComponent tabsLength={3} tabIndexToDelete={2} />);
@@ -142,7 +142,7 @@ describe('Tabs', () => {
142142
cy.findAllByRole('tab').should('have.length', 2);
143143
});
144144

145-
it(`GIVEN 3 tabs
145+
it(`GIVEN 3 tabs
146146
WHEN clicking on the last one and then removing it
147147
THEN tab 2 should be shown`, () => {
148148
cy.mount(<DynamicTabsComponent tabsLength={3} tabIndexToDelete={2} />);
@@ -155,7 +155,7 @@ describe('Tabs', () => {
155155
cy.findByRole('tabpanel').should('contain', 'Dynamic Tab 2 Panel');
156156
});
157157

158-
it(`GIVEN 4 tabs
158+
it(`GIVEN 4 tabs
159159
WHEN clicking on the last one and then removing the 3rd
160160
THEN tab 4 should be shown`, () => {
161161
cy.mount(<DynamicTabsComponent tabsLength={4} tabIndexToDelete={2} />);
@@ -165,7 +165,7 @@ describe('Tabs', () => {
165165
cy.findByRole('tabpanel').should('contain', 'Dynamic Tab 4 Panel');
166166
});
167167

168-
it(`GIVEN 4 tabs
168+
it(`GIVEN 4 tabs
169169
WHEN selecting the 3rd one and adding a tab at the start
170170
THEN the correct tab should be displayed`, () => {
171171
cy.mount(<DynamicTabsComponent tabsLength={4} tabIndexToAdd={1} />);
@@ -196,4 +196,14 @@ describe('Tabs', () => {
196196

197197
cy.findAllByRole('tabpanel').eq(1).should('contain', 'Child Panel 2');
198198
});
199+
200+
it(`GIVEN 3 tabs and the focus is on the first,
201+
WHEN triggering the right arrow key
202+
THEN the focus should be on the next tab`, () => {
203+
cy.mount(<ThreeTabsComponent />);
204+
205+
cy.findByRole('tab', { name: /Tab 1/i }).type('{rightarrow}');
206+
207+
cy.findByRole('tab', { name: /Tab 2/i }).should('have.focus');
208+
});
199209
});

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ import {
1111
import { tabsContextId } from './tabs-context-id';
1212
import { TabsContext } from './tabs-context.type';
1313
import { Behavior } from './behavior.type';
14+
import { KeyCode } from '../../utils/key-code.type';
1415

1516
/**
1617
* TABS TODOs
17-
*
18-
* onSelectedIndexChange
19-
* - Orientation
18+
*
19+
* - onSelectedIndexChange
20+
* - Orientation
2021
* - keyboard interactions (arrowDown, ARrowRight, ArrowUp, ArrowLeft, Home, End, PageUp, PageDown)
2122
* Support Loop
22-
* - onValueChange
2323
* POST V1:
2424
* - RTL
25+
* NOTE: scrolling support? or multiple lines? (probably not for headless but for tailwind / material )
26+
* Add ability to close tabs with an ❌ icon (and keyboard support)
2527
2628
*
2729
* TAB
2830
* Disable
29-
* NOTE: radix / headlessui: expose data-state data-disable data-orientation
3031
* NOTE: Headless UI: explorer the render props
31-
* NOTE: remove tab, switch position
32-
* NOTE: scrolling support? or multiple lines? (probably not for headless but for tailwind / material )
32+
* NOTE: switch position
33+
3334
*
3435
* PANEL
3536
*
@@ -58,6 +59,7 @@ export interface TabInfo {
5859
export const Tabs = component$((props: TabsProps) => {
5960
const behavior = props.behavior ?? 'manual';
6061

62+
const ref = useSignal<HTMLElement | undefined>();
6163
const selectedIndex = useSignal(0);
6264

6365
useTask$(({ track }) => {
@@ -74,7 +76,7 @@ export const Tabs = component$((props: TabsProps) => {
7476

7577
const tabPanelsMap = useStore<{ [key: string]: TabInfo }>({});
7678

77-
const tabsChanged$ = $(() => {
79+
const onTabsChanged$ = $(() => {
7880
reIndexTabs.value = true;
7981
});
8082

@@ -86,20 +88,39 @@ export const Tabs = component$((props: TabsProps) => {
8688
showTabsSignal.value = true;
8789
});
8890

91+
const onTabKeyDown$ = $((key: KeyCode, tabId: string) => {
92+
const tabsRootElement = ref.value;
93+
if (key === KeyCode.ArrowRight) {
94+
const currentFocusedTabIndex = tabsMap[tabId].index!;
95+
96+
// TODO: add disabled support
97+
if (currentFocusedTabIndex < tabPairs.length - 1) {
98+
const nextTabId = tabPairs[currentFocusedTabIndex + 1].tabId;
99+
tabsRootElement
100+
?.querySelector<HTMLElement>(`[data-tab-id='${nextTabId}']`)
101+
?.focus();
102+
} else {
103+
tabsRootElement
104+
?.querySelector<HTMLElement>(`[data-tab-id='${tabPairs[0].tabId}']`)
105+
?.focus();
106+
}
107+
}
108+
});
109+
89110
const contextService: TabsContext = {
90111
tabsMap,
91112
tabPanelsMap,
92113
selectedIndex,
93114
selectTab$,
94115
showTabs$,
95-
tabsChanged$,
116+
onTabsChanged$,
96117
behavior,
97118
selectedTabId,
119+
onTabKeyDown$,
98120
};
99121

100122
useContextProvider(tabsContextId, contextService);
101123

102-
const ref = useSignal<HTMLElement | undefined>();
103124
// useVisibleTask$(({ track }) => {
104125
// track(() => selectedIndex.value);
105126
// if (tabsIdsByIndex[selectedIndex.value]) {
@@ -201,6 +222,12 @@ export const Tabs = component$((props: TabsProps) => {
201222
<div
202223
ref={ref}
203224
{...props}
225+
onKeyDown$={(e) => {
226+
onTabKeyDown$(
227+
e.key as KeyCode,
228+
(e.target as any).getAttribute('data-tab-id')
229+
);
230+
}}
204231
style={'visibility:' + (showTabsSignal.value ? 'visible' : 'hidden')}
205232
>
206233
<Slot />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export enum KeyCode {
2+
ArrowLeft = 'ArrowLeft',
3+
ArrowRight = 'ArrowRight',
4+
ArrowUp = 'ArrowUp',
5+
ArrowDown = 'ArrowDown',
6+
Home = 'Home',
7+
End = 'End',
8+
}

0 commit comments

Comments
 (0)