Skip to content

Commit

Permalink
Follow up fix to 19452
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm committed Jul 25, 2020
1 parent 242a50a commit 34baa28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMEventListener-test.js
Expand Up @@ -595,7 +595,8 @@ describe('ReactDOMEventListener', () => {
const container = document.createElement('div');
const innerRef = React.createRef();
const outerRef = React.createRef();
const onPlayCapture = jest.fn();
const onPlayCapture = jest.fn(e => log.push(e.currentTarget));
const log = [];
document.body.appendChild(container);
try {
ReactDOM.render(
Expand All @@ -612,12 +613,23 @@ describe('ReactDOMEventListener', () => {
}),
);
expect(onPlayCapture).toHaveBeenCalledTimes(3);
expect(log).toEqual([
outerRef.current,
outerRef.current.firstChild,
innerRef.current,
]);
outerRef.current.dispatchEvent(
new Event('play', {
bubbles: false,
}),
);
expect(onPlayCapture).toHaveBeenCalledTimes(4);
expect(log).toEqual([
outerRef.current,
outerRef.current.firstChild,
innerRef.current,
outerRef.current,
]);
} finally {
document.body.removeChild(container);
}
Expand Down
3 changes: 0 additions & 3 deletions packages/react-dom/src/events/DOMPluginEventSystem.js
Expand Up @@ -718,9 +718,6 @@ export function accumulateSinglePhaseListeners(
const captured = bubbled !== null ? bubbled + 'Capture' : null;
const listeners: Array<DispatchListener> = [];

// If we are not handling EventTarget only phase, then we're doing the
// usual two phase accumulation using the React fiber tree to pick up
// all relevant useEvent and on* prop events.
let instance = targetFiber;
let lastHostComponent = null;
const targetType = event.type;
Expand Down
8 changes: 8 additions & 0 deletions packages/react-dom/src/events/plugins/SimpleEventPlugin.js
Expand Up @@ -43,6 +43,7 @@ import getEventCharCode from '../getEventCharCode';
import {IS_CAPTURE_PHASE, IS_NON_DELEGATED} from '../EventSystemFlags';

import {enableCreateEventHandleAPI} from 'shared/ReactFeatureFlags';
import {getClosestInstanceFromNode} from '../../client/ReactDOMComponentTree';

function extractEvents(
dispatchQueue: DispatchQueue,
Expand Down Expand Up @@ -174,6 +175,13 @@ function extractEvents(
// TODO: We may also want to re-use the accumulateTargetOnly flag to
// special case bubbling for onScroll/media events at a later point.
const accumulateTargetOnly = inCapturePhase && isNonDelegatedEvent;
// If we are not handling accumulateTargetOnly, then we should traverse
// through all React fiber tree, finding all relevant useEvent and
// on* prop events as we traverse the tree. Otherwise, we should
// only handle the target fiber and stop traversal straight after.
if (accumulateTargetOnly) {
targetInst = getClosestInstanceFromNode(((targetContainer: any): Node));
}

// We traverse only capture or bubble phase listeners
accumulateSinglePhaseListeners(
Expand Down

0 comments on commit 34baa28

Please sign in to comment.