Skip to content

Commit

Permalink
Event API: Fix bug where Press root events were not being cleared (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Apr 26, 2019
1 parent a14e24e commit 7a482af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
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);
});
});

0 comments on commit 7a482af

Please sign in to comment.