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

Event API: Fix bug where Press root events were not being cleared #15507

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/react-events/src/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type PressProps = {
type PointerType = '' | 'mouse' | 'keyboard' | 'pen' | 'touch';

type PressState = {
addedRootEvents: boolean,
isActivePressed: boolean,
isActivePressStart: boolean,
isLongPressed: boolean,
Expand Down Expand Up @@ -300,6 +301,7 @@ function dispatchPressEndEvents(
deactivate(context, props, state);
}
}
removeRootEventTypes(context, state);
}

function isAnchorTagElement(eventTarget: EventTarget): boolean {
Expand Down Expand Up @@ -394,7 +396,6 @@ function unmountResponder(
): void {
if (state.isPressed) {
dispatchPressEndEvents(context, props, state);
context.removeRootEventTypes(rootEventTypes);
}
}

Expand All @@ -411,15 +412,35 @@ function dispatchCancel(
} else {
state.ignoreEmulatedMouseEvents = false;
dispatchPressEndEvents(context, props, state);
context.removeRootEventTypes(rootEventTypes);
}
}
}

function addRootEventTypes(
context: ReactResponderContext,
state: PressState,
): void {
if (!state.addedRootEvents) {
state.addedRootEvents = true;
context.addRootEventTypes(rootEventTypes);
}
}

function removeRootEventTypes(
context: ReactResponderContext,
state: PressState,
): void {
if (state.addedRootEvents) {
state.addedRootEvents = false;
context.removeRootEventTypes(rootEventTypes);
}
}

const PressResponder = {
targetEventTypes,
createInitialState(): PressState {
return {
addedRootEvents: false,
didDispatchEvent: false,
isActivePressed: false,
isActivePressStart: false,
Expand Down Expand Up @@ -447,7 +468,6 @@ const PressResponder = {

if (props.disabled) {
dispatchPressEndEvents(context, props, state);
context.removeRootEventTypes(rootEventTypes);
state.ignoreEmulatedMouseEvents = false;
return;
}
Expand Down Expand Up @@ -500,7 +520,7 @@ const PressResponder = {
);
state.isPressWithinResponderRegion = true;
dispatchPressStartEvents(context, props, state);
context.addRootEventTypes(rootEventTypes);
addRootEventTypes(context, state);
} else {
// Prevent spacebar press from scrolling the window
if (isValidKeyPress(nativeEvent.key) && nativeEvent.key === ' ') {
Expand Down Expand Up @@ -630,7 +650,6 @@ const PressResponder = {
}
}
}
context.removeRootEventTypes(rootEventTypes);
} else if (type === 'mouseup' && state.ignoreEmulatedMouseEvents) {
state.ignoreEmulatedMouseEvents = false;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/react-events/src/__tests__/Press-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,4 +1428,21 @@ describe('Event responder: Press', () => {
it('expect displayName to show up for event component', () => {
expect(Press.displayName).toBe('Press');
});

it('should not trigger an invariant in addRootEventTypes()', () => {
const ref = React.createRef();
const element = (
<Press>
<button ref={ref} />
</Press>
);
ReactDOM.render(element, container);

ref.current.dispatchEvent(createPointerEvent('pointerdown'));
jest.advanceTimersByTime(DEFAULT_LONG_PRESS_DELAY);
ref.current.dispatchEvent(createPointerEvent('pointermove'));
ref.current.dispatchEvent(createPointerEvent('pointerup'));
ref.current.dispatchEvent(createPointerEvent('pointerdown'));
ReactDOM.render(element, container);
});
});