File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed
packages/react-events/src Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import type {
1212 ReactResponderContext ,
1313} from 'shared/ReactTypes' ;
1414import { REACT_EVENT_COMPONENT_TYPE } from 'shared/ReactSymbols' ;
15+ import { getEventCurrentTarget } from './utils.js' ;
1516
1617const CAPTURE_PHASE = 2 ;
1718
@@ -120,21 +121,17 @@ const FocusResponder = {
120121 if ( phase === CAPTURE_PHASE ) {
121122 return false ;
122123 }
124+
123125 switch ( type ) {
124126 case 'focus ': {
125127 if ( ! state . isFocused ) {
126128 // Limit focus events to the direct child of the event component.
127129 // 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 ;
134134 }
135- state . focusTarget = currentTarget ;
136- dispatchFocusInEvents ( context , props , state ) ;
137- state . isFocused = true ;
138135 }
139136 break ;
140137 }
Original file line number Diff line number Diff line change @@ -62,14 +62,17 @@ describe('Focus event responder', () => {
6262 } ) ;
6363
6464 describe ( 'onFocus' , ( ) => {
65- let onFocus , ref ;
65+ let onFocus , ref , innerRef ;
6666
6767 beforeEach ( ( ) => {
6868 onFocus = jest . fn ( ) ;
6969 ref = React . createRef ( ) ;
70+ innerRef = React . createRef ( ) ;
7071 const element = (
7172 < Focus onFocus = { onFocus } >
72- < div ref = { ref } />
73+ < div ref = { ref } >
74+ < a ref = { innerRef } />
75+ </ div >
7376 </ Focus >
7477 ) ;
7578 ReactDOM . render ( element , container ) ;
@@ -79,6 +82,12 @@ describe('Focus event responder', () => {
7982 ref . current . dispatchEvent ( createFocusEvent ( 'focus' ) ) ;
8083 expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
8184 } ) ;
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+ } ) ;
8291 } ) ;
8392
8493 describe ( 'onFocusChange' , ( ) => {
You can’t perform that action at this time.
0 commit comments