@@ -228,7 +228,7 @@ export function usePress(props: PressHookProps): PressResult {
228
228
229
229
let pressProps : DOMAttributes = {
230
230
onKeyDown ( e ) {
231
- if ( isValidKeyboardEvent ( e . nativeEvent ) && e . currentTarget . contains ( e . target as Element ) ) {
231
+ if ( isValidKeyboardEvent ( e . nativeEvent , e . currentTarget ) && e . currentTarget . contains ( e . target as Element ) ) {
232
232
if ( shouldPreventDefaultKeyboard ( e . target as Element ) ) {
233
233
e . preventDefault ( ) ;
234
234
}
@@ -246,10 +246,15 @@ export function usePress(props: PressHookProps): PressResult {
246
246
// instead of the same element where the key down event occurred.
247
247
addGlobalListener ( document , 'keyup' , onKeyUp , false ) ;
248
248
}
249
+ } else if ( e . key === 'Enter' && isHTMLAnchorLink ( e . currentTarget ) ) {
250
+ // If the target is a link, we won't have handled this above because we want the default
251
+ // browser behavior to open the link when pressing Enter. But we still need to prevent
252
+ // default so that elements above do not also handle it (e.g. table row).
253
+ e . stopPropagation ( ) ;
249
254
}
250
255
} ,
251
256
onKeyUp ( e ) {
252
- if ( isValidKeyboardEvent ( e . nativeEvent ) && ! e . repeat && e . currentTarget . contains ( e . target as Element ) ) {
257
+ if ( isValidKeyboardEvent ( e . nativeEvent , e . currentTarget ) && ! e . repeat && e . currentTarget . contains ( e . target as Element ) ) {
253
258
triggerPressUp ( createEvent ( state . target , e ) , 'keyboard' ) ;
254
259
}
255
260
} ,
@@ -284,7 +289,7 @@ export function usePress(props: PressHookProps): PressResult {
284
289
} ;
285
290
286
291
let onKeyUp = ( e : KeyboardEvent ) => {
287
- if ( state . isPressed && isValidKeyboardEvent ( e ) ) {
292
+ if ( state . isPressed && isValidKeyboardEvent ( e , state . target ) ) {
288
293
if ( shouldPreventDefaultKeyboard ( e . target as Element ) ) {
289
294
e . preventDefault ( ) ;
290
295
}
@@ -297,7 +302,7 @@ export function usePress(props: PressHookProps): PressResult {
297
302
298
303
// If the target is a link, trigger the click method to open the URL,
299
304
// but defer triggering pressEnd until onClick event handler.
300
- if ( state . target instanceof HTMLElement && ( state . target . contains ( target ) && isHTMLAnchorLink ( state . target ) || state . target . getAttribute ( 'role' ) === 'link' ) ) {
305
+ if ( state . target instanceof HTMLElement && state . target . contains ( target ) && ( isHTMLAnchorLink ( state . target ) || state . target . getAttribute ( 'role' ) === 'link' ) ) {
301
306
state . target . click ( ) ;
302
307
}
303
308
}
@@ -662,13 +667,13 @@ export function usePress(props: PressHookProps): PressResult {
662
667
} ;
663
668
}
664
669
665
- function isHTMLAnchorLink ( target : HTMLElement ) : boolean {
670
+ function isHTMLAnchorLink ( target : Element ) : boolean {
666
671
return target . tagName === 'A' && target . hasAttribute ( 'href' ) ;
667
672
}
668
673
669
- function isValidKeyboardEvent ( event : KeyboardEvent ) : boolean {
670
- const { key, code, target } = event ;
671
- const element = target as HTMLElement ;
674
+ function isValidKeyboardEvent ( event : KeyboardEvent , currentTarget : Element ) : boolean {
675
+ const { key, code} = event ;
676
+ const element = currentTarget as HTMLElement ;
672
677
const { tagName, isContentEditable} = element ;
673
678
const role = element . getAttribute ( 'role' ) ;
674
679
// Accessibility for keyboards. Space and Enter only.
0 commit comments