File tree 2 files changed +17
-11
lines changed
packages/react-events/src 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import type {
12
12
ReactResponderContext ,
13
13
} from 'shared/ReactTypes' ;
14
14
import { REACT_EVENT_COMPONENT_TYPE } from 'shared/ReactSymbols' ;
15
+ import { getEventCurrentTarget } from './utils.js' ;
15
16
16
17
const CAPTURE_PHASE = 2 ;
17
18
@@ -120,21 +121,17 @@ const FocusResponder = {
120
121
if ( phase === CAPTURE_PHASE ) {
121
122
return false ;
122
123
}
124
+
123
125
switch ( type ) {
124
126
case 'focus ': {
125
127
if ( ! state . isFocused ) {
126
128
// Limit focus events to the direct child of the event component.
127
129
// Browser focus is not expected to bubble.
128
- let currentTarget = ( target : any ) ;
129
- if (
130
- currentTarget . parentNode &&
131
- context . isTargetWithinEventComponent ( currentTarget . parentNode )
132
- ) {
133
- break ;
130
+ state . focusTarget = getEventCurrentTarget ( event , context ) ;
131
+ if ( state . focusTarget === target ) {
132
+ dispatchFocusInEvents ( context , props , state ) ;
133
+ state . isFocused = true ;
134
134
}
135
- state . focusTarget = currentTarget ;
136
- dispatchFocusInEvents ( context , props , state ) ;
137
- state . isFocused = true ;
138
135
}
139
136
break ;
140
137
}
Original file line number Diff line number Diff line change @@ -62,14 +62,17 @@ describe('Focus event responder', () => {
62
62
} ) ;
63
63
64
64
describe ( 'onFocus' , ( ) => {
65
- let onFocus , ref ;
65
+ let onFocus , ref , innerRef ;
66
66
67
67
beforeEach ( ( ) => {
68
68
onFocus = jest . fn ( ) ;
69
69
ref = React . createRef ( ) ;
70
+ innerRef = React . createRef ( ) ;
70
71
const element = (
71
72
< Focus onFocus = { onFocus } >
72
- < div ref = { ref } />
73
+ < div ref = { ref } >
74
+ < a ref = { innerRef } />
75
+ </ div >
73
76
</ Focus >
74
77
) ;
75
78
ReactDOM . render ( element , container ) ;
@@ -79,6 +82,12 @@ describe('Focus event responder', () => {
79
82
ref . current . dispatchEvent ( createFocusEvent ( 'focus' ) ) ;
80
83
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
81
84
} ) ;
85
+
86
+ it ( 'is not called if descendants of target receive focus' , ( ) => {
87
+ const target = innerRef . current ;
88
+ target . dispatchEvent ( createFocusEvent ( 'focus' ) ) ;
89
+ expect ( onFocus ) . not . toBeCalled ( ) ;
90
+ } ) ;
82
91
} ) ;
83
92
84
93
describe ( 'onFocusChange' , ( ) => {
You can’t perform that action at this time.
0 commit comments