Skip to content

Commit

Permalink
Revert focus event PRs (#18655)
Browse files Browse the repository at this point in the history
* Revert "Further cleanup to before/after blur (#18649)"

This reverts commit e2ccbf0.

* Revert "Unify Flare FocusWithin responder with useFocusWithin (#18636)"

This reverts commit f24a9e7.
  • Loading branch information
trueadm committed Apr 17, 2020
1 parent e2ccbf0 commit 58c895e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 56 deletions.
4 changes: 4 additions & 0 deletions packages/react-dom/src/client/ReactDOMComponent.js
Expand Up @@ -1355,6 +1355,10 @@ export function listenToEventResponderEventTypes(
const targetEventType = isPassive
? eventType
: eventType.substring(0, eventType.length - 7);
// We don't listen to this as we actually emulate it in the host config
if (targetEventType === 'beforeblur') {
continue;
}
if (!listenerMap.has(eventKey)) {
if (isPassive) {
const activeKey = targetEventType + '_active';
Expand Down
33 changes: 31 additions & 2 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Expand Up @@ -62,6 +62,7 @@ import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols';
import {
mountEventResponder,
unmountEventResponder,
DEPRECATED_dispatchEventForResponderEventSystem,
} from '../events/DeprecatedDOMEventResponderSystem';
import {retryIfBlockedOn} from '../events/ReactDOMEventReplaying';

Expand All @@ -73,6 +74,8 @@ import {
enableScopeAPI,
} from 'shared/ReactFeatureFlags';
import {
RESPONDER_EVENT_SYSTEM,
IS_PASSIVE,
PLUGIN_EVENT_SYSTEM,
USE_EVENT_SYSTEM,
} from '../events/EventSystemFlags';
Expand Down Expand Up @@ -525,9 +528,22 @@ function createEvent(type: TopLevelType): Event {
}

function dispatchBeforeDetachedBlur(target: HTMLElement): void {
const targetInstance = getClosestInstanceFromNode(target);
((selectionInformation: any): SelectionInformation).activeElementDetached = target;

if (enableDeprecatedFlareAPI || enableUseEventAPI) {
if (enableDeprecatedFlareAPI) {
DEPRECATED_dispatchEventForResponderEventSystem(
'beforeblur',
targetInstance,
({
target,
timeStamp: Date.now(),
}: any),
target,
RESPONDER_EVENT_SYSTEM | IS_PASSIVE,
);
}
if (enableUseEventAPI) {
const event = createEvent(TOP_BEFORE_BLUR);
// Dispatch "beforeblur" directly on the target,
// so it gets picked up by the event system and
Expand All @@ -537,7 +553,20 @@ function dispatchBeforeDetachedBlur(target: HTMLElement): void {
}

function dispatchAfterDetachedBlur(target: HTMLElement): void {
if (enableDeprecatedFlareAPI || enableUseEventAPI) {
if (enableDeprecatedFlareAPI) {
DEPRECATED_dispatchEventForResponderEventSystem(
'blur',
null,
({
isTargetAttached: false,
target,
timeStamp: Date.now(),
}: any),
target,
RESPONDER_EVENT_SYSTEM | IS_PASSIVE,
);
}
if (enableUseEventAPI) {
const event = createEvent(TOP_AFTER_BLUR);
// So we know what was detached, make the relatedTarget the
// detached target on the "afterblur" event.
Expand Down
54 changes: 15 additions & 39 deletions packages/react-interactions/events/src/dom/DeprecatedFocus.js
Expand Up @@ -22,7 +22,7 @@ import {DiscreteEvent} from 'shared/ReactTypes';
*/

type FocusEvent = {|
relatedTarget: null | Element | Document,
isTargetAttached: boolean,
target: Element | Document,
type: FocusEventType | FocusWithinEventType,
pointerType: PointerType,
Expand Down Expand Up @@ -53,7 +53,6 @@ type FocusEventType = 'focus' | 'blur' | 'focuschange' | 'focusvisiblechange';
type FocusWithinProps = {
disabled?: boolean,
onFocusWithin?: (e: FocusEvent) => void,
onAfterBlurWithin?: (e: FocusEvent) => void,
onBeforeBlurWithin?: (e: FocusEvent) => void,
onBlurWithin?: (e: FocusEvent) => void,
onFocusWithinChange?: boolean => void,
Expand All @@ -66,8 +65,7 @@ type FocusWithinEventType =
| 'focuswithinchange'
| 'blurwithin'
| 'focuswithin'
| 'beforeblurwithin'
| 'afterblurwithin';
| 'beforeblurwithin';

/**
* Shared between Focus and FocusWithin
Expand Down Expand Up @@ -118,7 +116,8 @@ const focusVisibleEvents = hasPointerEvents

const targetEventTypes = ['focus', 'blur', 'beforeblur', ...focusVisibleEvents];

const rootEventTypes = ['afterblur'];
// Used only for the blur "detachedTarget" logic
const rootEventTypes = ['blur'];

function addWindowEventListener(types, callback, options) {
types.forEach(type => {
Expand Down Expand Up @@ -193,10 +192,10 @@ function createFocusEvent(
type: FocusEventType | FocusWithinEventType,
target: Element | Document,
pointerType: PointerType,
relatedTarget: null | Element | Document,
isTargetAttached: boolean,
): FocusEvent {
return {
relatedTarget,
isTargetAttached,
target,
type,
pointerType,
Expand Down Expand Up @@ -298,7 +297,7 @@ function dispatchFocusEvents(
'focus',
target,
pointerType,
null,
true,
);
context.dispatchEvent(syntheticEvent, onFocus, DiscreteEvent);
}
Expand All @@ -322,7 +321,7 @@ function dispatchBlurEvents(
'blur',
target,
pointerType,
null,
true,
);
context.dispatchEvent(syntheticEvent, onBlur, DiscreteEvent);
}
Expand All @@ -347,7 +346,7 @@ function dispatchFocusWithinEvents(
'focuswithin',
target,
pointerType,
null,
true,
);
context.dispatchEvent(syntheticEvent, onFocusWithin, DiscreteEvent);
}
Expand All @@ -362,39 +361,19 @@ function dispatchBlurWithinEvents(
const pointerType = state.pointerType;
const target = ((state.focusTarget: any): Element | Document) || event.target;
const onBlurWithin = (props.onBlurWithin: any);
const isTargetAttached = state.detachedTarget === null;
if (isFunction(onBlurWithin)) {
const syntheticEvent = createFocusEvent(
context,
'blurwithin',
target,
pointerType,
null,
isTargetAttached,
);
context.dispatchEvent(syntheticEvent, onBlurWithin, DiscreteEvent);
}
}

function dispatchAfterBlurWithinEvents(
context: ReactDOMResponderContext,
event: ReactDOMResponderEvent,
props: FocusWithinProps,
state: FocusState,
) {
const pointerType = state.pointerType;
const onAfterBlurWithin = (props.onAfterBlurWithin: any);
const relatedTarget = state.detachedTarget;
if (isFunction(onAfterBlurWithin) && relatedTarget !== null) {
const syntheticEvent = createFocusEvent(
context,
'afterblurwithin',
relatedTarget,
pointerType,
relatedTarget,
);
context.dispatchEvent(syntheticEvent, onAfterBlurWithin, DiscreteEvent);
}
}

function dispatchFocusChange(
context: ReactDOMResponderContext,
props: FocusProps,
Expand Down Expand Up @@ -637,7 +616,7 @@ const focusWithinResponderImpl = {
'beforeblurwithin',
event.target,
state.pointerType,
null,
true,
);
state.detachedTarget = event.target;
context.dispatchEvent(
Expand Down Expand Up @@ -681,13 +660,10 @@ const focusWithinResponderImpl = {
props: FocusWithinProps,
state: FocusState,
): void {
if (event.type === 'afterblur') {
if (event.type === 'blur') {
const detachedTarget = state.detachedTarget;
if (
detachedTarget !== null &&
detachedTarget === event.nativeEvent.relatedTarget
) {
dispatchAfterBlurWithinEvents(context, event, props, state);
if (detachedTarget !== null && detachedTarget === event.target) {
dispatchBlurWithinEvents(context, event, props, state);
state.detachedTarget = null;
if (state.addedRootEvents) {
state.addedRootEvents = false;
Expand Down
Expand Up @@ -290,11 +290,11 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
});

describe('onBeforeBlurWithin', () => {
let onBeforeBlurWithin, onAfterBlurWithin, ref, innerRef, innerRef2;
let onBeforeBlurWithin, onBlurWithin, ref, innerRef, innerRef2;

beforeEach(() => {
onBeforeBlurWithin = jest.fn();
onAfterBlurWithin = jest.fn();
onBlurWithin = jest.fn();
ref = React.createRef();
innerRef = React.createRef();
innerRef2 = React.createRef();
Expand All @@ -305,7 +305,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
const Component = ({show}) => {
const listener = useFocusWithin({
onBeforeBlurWithin,
onAfterBlurWithin,
onBlurWithin,
});
return (
<div ref={ref} DEPRECATED_flareListeners={listener}>
Expand All @@ -322,12 +322,12 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
target.keydown({key: 'Tab'});
target.focus();
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
expect(onBlurWithin).toHaveBeenCalledTimes(0);
ReactDOM.render(<Component show={false} />, container);
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
expect(onAfterBlurWithin).toHaveBeenCalledWith(
expect.objectContaining({relatedTarget: inner}),
expect(onBlurWithin).toHaveBeenCalledTimes(1);
expect(onBlurWithin).toHaveBeenCalledWith(
expect.objectContaining({isTargetAttached: false}),
);
});

Expand All @@ -336,7 +336,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
const Component = ({show}) => {
const listener = useFocusWithin({
onBeforeBlurWithin,
onAfterBlurWithin,
onBlurWithin,
});
return (
<div ref={ref} DEPRECATED_flareListeners={listener}>
Expand All @@ -357,12 +357,12 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
target.keydown({key: 'Tab'});
target.focus();
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
expect(onBlurWithin).toHaveBeenCalledTimes(0);
ReactDOM.render(<Component show={false} />, container);
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
expect(onAfterBlurWithin).toHaveBeenCalledWith(
expect.objectContaining({relatedTarget: inner}),
expect(onBlurWithin).toHaveBeenCalledTimes(1);
expect(onBlurWithin).toHaveBeenCalledWith(
expect.objectContaining({isTargetAttached: false}),
);
});

Expand Down Expand Up @@ -418,7 +418,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
const Component = ({show}) => {
const listener = useFocusWithin({
onBeforeBlurWithin,
onAfterBlurWithin,
onBlurWithin,
});

return (
Expand All @@ -444,7 +444,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
target.keydown({key: 'Tab'});
target.focus();
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
expect(onBlurWithin).toHaveBeenCalledTimes(0);

suspend = true;
root.render(<Component />);
Expand All @@ -454,7 +454,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
'<div><input style="display: none;">Loading...</div>',
);
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
expect(onBlurWithin).toHaveBeenCalledTimes(1);
resolve();

document.body.removeChild(container2);
Expand Down

0 comments on commit 58c895e

Please sign in to comment.