mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
React events: add a test for focusable descendants (#15457)
This commit is contained in:
Vendored
+6
-9
@@ -12,6 +12,7 @@ import type {
|
||||
ReactResponderContext,
|
||||
} from 'shared/ReactTypes';
|
||||
import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols';
|
||||
import {getEventCurrentTarget} from './utils.js';
|
||||
|
||||
const CAPTURE_PHASE = 2;
|
||||
|
||||
@@ -120,21 +121,17 @@ const FocusResponder = {
|
||||
if (phase === CAPTURE_PHASE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'focus': {
|
||||
if (!state.isFocused) {
|
||||
// Limit focus events to the direct child of the event component.
|
||||
// Browser focus is not expected to bubble.
|
||||
let currentTarget = (target: any);
|
||||
if (
|
||||
currentTarget.parentNode &&
|
||||
context.isTargetWithinEventComponent(currentTarget.parentNode)
|
||||
) {
|
||||
break;
|
||||
state.focusTarget = getEventCurrentTarget(event, context);
|
||||
if (state.focusTarget === target) {
|
||||
dispatchFocusInEvents(context, props, state);
|
||||
state.isFocused = true;
|
||||
}
|
||||
state.focusTarget = currentTarget;
|
||||
dispatchFocusInEvents(context, props, state);
|
||||
state.isFocused = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -62,14 +62,17 @@ describe('Focus event responder', () => {
|
||||
});
|
||||
|
||||
describe('onFocus', () => {
|
||||
let onFocus, ref;
|
||||
let onFocus, ref, innerRef;
|
||||
|
||||
beforeEach(() => {
|
||||
onFocus = jest.fn();
|
||||
ref = React.createRef();
|
||||
innerRef = React.createRef();
|
||||
const element = (
|
||||
<Focus onFocus={onFocus}>
|
||||
<div ref={ref} />
|
||||
<div ref={ref}>
|
||||
<a ref={innerRef} />
|
||||
</div>
|
||||
</Focus>
|
||||
);
|
||||
ReactDOM.render(element, container);
|
||||
@@ -79,6 +82,12 @@ describe('Focus event responder', () => {
|
||||
ref.current.dispatchEvent(createFocusEvent('focus'));
|
||||
expect(onFocus).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('is not called if descendants of target receive focus', () => {
|
||||
const target = innerRef.current;
|
||||
target.dispatchEvent(createFocusEvent('focus'));
|
||||
expect(onFocus).not.toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('onFocusChange', () => {
|
||||
|
||||
Reference in New Issue
Block a user