Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: onFocus/onBlur/onBeforeInput have a matching event type #19561

Merged
merged 8 commits into from Aug 10, 2020
Merged
2 changes: 1 addition & 1 deletion packages/react-dom/src/events/DOMPluginEventSystem.js
Expand Up @@ -709,7 +709,7 @@ export function accumulateSinglePhaseListeners(

let instance = targetFiber;
let lastHostComponent = null;
const targetType = event.type;
const targetType = event.nativeEvent.type;

// Accumulate all instances and listeners via the target -> root path.
while (instance !== null) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/events/ReactSyntheticEventType.js
Expand Up @@ -29,6 +29,7 @@ export type ReactSyntheticEvent = {|
_dispatchListeners?: null | Array<Function> | Function,
_reactName: string,
_targetInst: Fiber,
nativeEvent: Event,
type: string,
currentTarget: null | EventTarget,
|};
@@ -0,0 +1,70 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

describe('SyntheticFocusEvent', () => {
let React;
let ReactDOM;
let container;

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');

container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

test('onFocus events have the focus type', () => {
const log = [];
ReactDOM.render(
<button
onFocus={event => log.push(`onFocus: ${event.type}`)}
onFocusCapture={event => log.push(`onFocusCapture: ${event.type}`)}
/>,
container,
);
const button = container.querySelector('button');

button.dispatchEvent(
new FocusEvent('focusin', {
bubbles: true,
cancelable: false,
}),
);

expect(log).toEqual(['onFocusCapture: focus', 'onFocus: focus']);
});

test('onBlur events have the blur type', () => {
const log = [];
ReactDOM.render(
<button
onBlur={event => log.push(`onBlur: ${event.type}`)}
onBlurCapture={event => log.push(`onBlurCapture: ${event.type}`)}
/>,
container,
);
const button = container.querySelector('button');

button.dispatchEvent(
new FocusEvent('focusout', {
bubbles: true,
cancelable: false,
}),
);

expect(log).toEqual(['onBlurCapture: blur', 'onBlur: blur']);
});
});
7 changes: 7 additions & 0 deletions packages/react-dom/src/events/plugins/SimpleEventPlugin.js
Expand Up @@ -158,6 +158,13 @@ function extractEvents(
EventInterface,
);

if (domEventName === 'focusin') {
gaearon marked this conversation as resolved.
Show resolved Hide resolved
event.type = 'focus';
}
if (domEventName === 'focusout') {
event.type = 'blur';
}

const inCapturePhase = (eventSystemFlags & IS_CAPTURE_PHASE) !== 0;
if (
enableCreateEventHandleAPI &&
Expand Down
Expand Up @@ -36,7 +36,7 @@ function initializeModules(hasPointerEvents) {
const forcePointerEvents = true;
const table = [[forcePointerEvents], [!forcePointerEvents]];

describe.each(table)(`useFocus`, hasPointerEvents => {
describe.each(table)(`useFocus hasPointerEvents=%s`, hasPointerEvents => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was slightly confusing why we had two tests with the same name.

let container;

beforeEach(() => {
Expand Down