Skip to content

Commit 95a7569

Browse files
CopilotGitHub Copilotmfranzke
authored
fix: add ENTER key support to db-switch for ARIA compliance (#5444)
* Initial plan * fix: add ENTER key support to db-switch component for a11y Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> * test: add keyboard interaction tests for db-switch ENTER key Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> * chore: add blob-report to gitignore and remove test artifacts Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> * Apply suggestions from code review * Create wise-walls-pretend.md --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: GitHub Copilot <my.commit@mail.com> Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com>
1 parent 8f53cb0 commit 95a7569

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

.changeset/wise-walls-pretend.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@db-ux/ngx-core-components": patch
3+
"@db-ux/react-core-components": patch
4+
"@db-ux/wc-core-components": patch
5+
"@db-ux/v-core-components": patch
6+
---
7+
8+
refactor(DBSwitch): Also toggle on pressing Enter key, not just Space

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ showcases/patternhub/public/iframe-resizer/*
7676
/packages/agent-cli/test/.amazonq/rules/db-ux.md
7777
/core-web.iml
7878
/build-storybooks/
79+
**/blob-report

packages/components/src/components/switch/model.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
FormProps,
99
FormState,
1010
FromValidState,
11+
GeneralKeyboardEvent,
1112
GlobalProps,
1213
GlobalState,
1314
IconLeadingProps,
@@ -41,7 +42,9 @@ export type DBSwitchProps = GlobalProps &
4142
IconLeadingProps &
4243
DBSwitchDefaultProps;
4344

44-
export type DBSwitchDefaultState = {};
45+
export type DBSwitchDefaultState = {
46+
handleKeyDown: (event: GeneralKeyboardEvent<HTMLInputElement>) => void;
47+
};
4548

4649
export type DBSwitchState = DBSwitchDefaultState &
4750
GlobalState &

packages/components/src/components/switch/switch.lite.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
DEFAULT_VALID_MESSAGE,
1818
DEFAULT_VALID_MESSAGE_ID_SUFFIX
1919
} from '../../shared/constants';
20-
import { ChangeEvent, InteractionEvent } from '../../shared/model';
20+
import {
21+
ChangeEvent,
22+
GeneralKeyboardEvent,
23+
InteractionEvent
24+
} from '../../shared/model';
2125
import {
2226
cls,
2327
delay,
@@ -138,6 +142,16 @@ export default function DBSwitch(props: DBSwitchProps) {
138142
if (props.onFocus) {
139143
props.onFocus(event);
140144
}
145+
},
146+
handleKeyDown: (event: GeneralKeyboardEvent<HTMLInputElement>) => {
147+
// Support ENTER key for toggling the switch (a11y requirement)
148+
if (event.key === 'Enter') {
149+
event.preventDefault();
150+
// Toggle the switch by clicking it programmatically
151+
if (!props.disabled) {
152+
(_ref as HTMLInputElement)?.click();
153+
}
154+
}
141155
}
142156
});
143157

@@ -225,6 +239,9 @@ export default function DBSwitch(props: DBSwitchProps) {
225239
onFocus={(event: InteractionEvent<HTMLInputElement>) =>
226240
state.handleFocus(event)
227241
}
242+
onKeyDown={(
243+
event: GeneralKeyboardEvent<HTMLInputElement>
244+
) => state.handleKeyDown(event)}
228245
/>
229246
<Show when={props.label} else={props.children}>
230247
{props.label}

packages/components/src/components/switch/switch.spec.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,44 @@ test.describe('DBSwitch', () => {
3333

3434
expect(accessibilityScanResults.violations).toEqual([]);
3535
});
36+
37+
test('should toggle on ENTER key press', async ({ mount, page }) => {
38+
const component = await mount(<DBSwitch>Test Switch</DBSwitch>);
39+
const input = component.locator('input[type="checkbox"][role="switch"]');
40+
41+
// Initially unchecked
42+
await expect(input).not.toBeChecked();
43+
44+
// Focus the input
45+
await input.focus();
46+
47+
// Press ENTER key
48+
await page.keyboard.press('Enter');
49+
50+
// Should be checked after ENTER key press
51+
await expect(input).toBeChecked();
52+
53+
// Press ENTER key again
54+
await page.keyboard.press('Enter');
55+
56+
// Should be unchecked after second ENTER key press
57+
await expect(input).not.toBeChecked();
58+
});
59+
60+
test('should toggle on SPACE key press', async ({ mount, page }) => {
61+
const component = await mount(<DBSwitch>Test Switch</DBSwitch>);
62+
const input = component.locator('input[type="checkbox"]');
63+
64+
// Initially unchecked
65+
await expect(input).not.toBeChecked();
66+
67+
// Focus the input
68+
await input.focus();
69+
70+
// Press SPACE key (default checkbox behavior)
71+
await page.keyboard.press('Space');
72+
73+
// Should be checked after SPACE key press
74+
await expect(input).toBeChecked();
75+
});
3676
});

0 commit comments

Comments
 (0)