Skip to content

Commit b7838d8

Browse files
2nikhiltom2nikhiltomheloiselui
authored
fix(20268): contentSwitcher manual activation key fix (#20434)
* fix(20268): contentSwitcher manual activation key fix * test: adds avt test case for manual mode --------- Co-authored-by: 2nikhiltom <nihkiltomar753@gmail.com> Co-authored-by: Heloise Lui <71858203+heloiselui@users.noreply.github.com>
1 parent 48134e2 commit b7838d8

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

e2e/components/ContentSwitcher/ContentSwitcher-test.avt.e2e.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,50 @@ test.describe('@avt ContentSwitcher', () => {
5252
await page.keyboard.press('ArrowRight');
5353
await expect(thirdContentTab).toBeVisible();
5454
});
55+
test('@avt-keyboard-nav ContentSwitcher manual selection mode', async ({
56+
page,
57+
}) => {
58+
await visitStory(page, {
59+
component: 'ContentSwitcher',
60+
id: 'components-contentswitcher--default',
61+
globals: {
62+
theme: 'white',
63+
},
64+
args: {
65+
selectionMode: 'manual',
66+
},
67+
});
68+
69+
const firstContentTab = page.getByRole('tab', { name: 'First section' });
70+
const secondContentTab = page.getByRole('tab', { name: 'Second section' });
71+
72+
await firstContentTab.focus();
73+
74+
await page.keyboard.press('ArrowRight');
75+
await expect(secondContentTab).toBeFocused();
76+
77+
await expect(firstContentTab).toHaveAttribute('aria-selected', 'true');
78+
await expect(secondContentTab).not.toHaveAttribute('aria-selected', 'true');
79+
80+
// test selection via Enter key
81+
await page.keyboard.press('Enter');
82+
await expect(secondContentTab).toHaveAttribute('aria-selected', 'true');
83+
await expect(firstContentTab).not.toHaveAttribute('aria-selected', 'true');
84+
85+
// test selection via Space key
86+
await page.keyboard.press('ArrowLeft');
87+
await expect(firstContentTab).toBeFocused();
88+
await page.keyboard.press(' ');
89+
await expect(firstContentTab).toHaveAttribute('aria-selected', 'true');
90+
await expect(secondContentTab).not.toHaveAttribute('aria-selected', 'true');
91+
92+
// No other keys should trigger selection
93+
await page.keyboard.press('ArrowRight');
94+
await expect(secondContentTab).toBeFocused();
95+
await page.keyboard.press('e');
96+
await expect(firstContentTab).toHaveAttribute('aria-selected', 'true');
97+
await expect(secondContentTab).not.toHaveAttribute('aria-selected', 'true');
98+
});
5599

56100
test('@avt-keyboard-nav ContentSwitcher Icon only', async ({ page }) => {
57101
await visitStory(page, {

packages/react/src/components/ContentSwitcher/ContentSwitcher.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ export const ContentSwitcher = ({
156156
});
157157
}
158158
}
159-
} else if (selectedIndex !== index) {
159+
} else if (
160+
selectedIndex !== index &&
161+
(isKeyboardEvent(event) ? matches(event, [keys.Enter, keys.Space]) : true)
162+
) {
160163
setSelectedIndex(index);
161164
focusSwitch(index);
162165
onChange(event);

0 commit comments

Comments
 (0)