File tree Expand file tree Collapse file tree 5 files changed +71
-2
lines changed
packages/components/src/components/switch Expand file tree Collapse file tree 5 files changed +71
-2
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
4649export type DBSwitchState = DBSwitchDefaultState &
4750 GlobalState &
Original file line number Diff line number Diff 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' ;
2125import {
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 }
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments