Skip to content

Commit

Permalink
[Flare] Remove contextmenu logic from Press (#16322)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm committed Aug 8, 2019
1 parent 12be893 commit d9fdec6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 162 deletions.
40 changes: 1 addition & 39 deletions packages/react-events/src/dom/Press.js
Expand Up @@ -28,10 +28,8 @@ type PressProps = {|
bottom: number,
left: number,
},
preventContextMenu: boolean,
preventDefault: boolean,
stopPropagation: boolean,
onContextMenu: (e: PressEvent) => void,
onPress: (e: PressEvent) => void,
onPressChange: boolean => void,
onPressEnd: (e: PressEvent) => void,
Expand Down Expand Up @@ -74,8 +72,7 @@ type PressEventType =
| 'pressmove'
| 'pressstart'
| 'pressend'
| 'presschange'
| 'contextmenu';
| 'presschange';

type PressEvent = {|
button: 'primary' | 'auxillary',
Expand Down Expand Up @@ -111,7 +108,6 @@ const DEFAULT_PRESS_RETENTION_OFFSET = {

const targetEventTypes = [
'keydown_active',
'contextmenu_active',
// We need to preventDefault on pointerdown for mouse/pen events
// that are in hit target area but not the element area.
'pointerdown_active',
Expand Down Expand Up @@ -615,40 +611,6 @@ const pressResponderImpl = {
break;
}

case 'contextmenu': {
const preventContextMenu = props.preventContextMenu;
if (preventContextMenu === true) {
// Skip dispatching of onContextMenu below
nativeEvent.preventDefault();
}
if (isPressed) {
const preventDefault = props.preventDefault;
if (preventDefault !== false && !nativeEvent.defaultPrevented) {
// Skip dispatching of onContextMenu below
nativeEvent.preventDefault();
return;
}
dispatchCancel(event, context, props, state);
}
const onContextMenu = props.onContextMenu;
if (isFunction(onContextMenu)) {
dispatchEvent(
event,
onContextMenu,
context,
state,
'contextmenu',
DiscreteEvent,
);
}
// Click won't occur, so we need to remove root events
removeRootEventTypes(context, state);
break;
}

case 'click': {
if (state.shouldPreventClick) {
nativeEvent.preventDefault();
Expand Down
123 changes: 0 additions & 123 deletions packages/react-events/src/dom/__tests__/Press-test.internal.js
Expand Up @@ -2454,129 +2454,6 @@ describe('Event responder: Press', () => {
},
);

describe('onContextMenu', () => {
it('is called after a right mouse click', () => {
const onContextMenu = jest.fn();
const ref = React.createRef();
const Component = () => {
const listener = usePressResponder({onContextMenu});

return <div ref={ref} listeners={listener} />;
};
ReactDOM.render(<Component />, container);

ref.current.dispatchEvent(
createEvent('pointerdown', {pointerType: 'mouse', button: 2}),
);
ref.current.dispatchEvent(createEvent('contextmenu'));
expect(onContextMenu).toHaveBeenCalledTimes(1);
expect(onContextMenu).toHaveBeenCalledWith(
expect.objectContaining({pointerType: 'mouse', type: 'contextmenu'}),
);
});

it('is called after a left mouse click + ctrl key on Mac', () => {
jest.resetModules();
const platformGetter = jest.spyOn(global.navigator, 'platform', 'get');
platformGetter.mockReturnValue('MacIntel');
init();

const onContextMenu = jest.fn();
const ref = React.createRef();

const Component = () => {
const listener = usePressResponder({onContextMenu});

return <div ref={ref} listeners={listener} />;
};
ReactDOM.render(<Component />, container);

ref.current.dispatchEvent(
createEvent('pointerdown', {
pointerType: 'mouse',
button: 0,
ctrlKey: true,
}),
);
ref.current.dispatchEvent(createEvent('contextmenu'));
expect(onContextMenu).toHaveBeenCalledTimes(1);
expect(onContextMenu).toHaveBeenCalledWith(
expect.objectContaining({pointerType: 'mouse', type: 'contextmenu'}),
);
platformGetter.mockClear();
});

it('is not called after a left mouse click + ctrl key on Windows', () => {
jest.resetModules();
const platformGetter = jest.spyOn(global.navigator, 'platform', 'get');
platformGetter.mockReturnValue('Win32');
init();

const onContextMenu = jest.fn();
const ref = React.createRef();

const Component = () => {
const listener = usePressResponder({onContextMenu});

return <div ref={ref} listeners={listener} />;
};
ReactDOM.render(<Component />, container);

ref.current.dispatchEvent(
createEvent('pointerdown', {
pointerType: 'mouse',
button: 0,
ctrlKey: true,
}),
);
ref.current.dispatchEvent(createEvent('contextmenu'));
expect(onContextMenu).toHaveBeenCalledTimes(0);
platformGetter.mockClear();
});

it('is not called after a right mouse click occurs during an active press', () => {
const onContextMenu = jest.fn();
const ref = React.createRef();

const Component = () => {
const listener = usePressResponder({onContextMenu});

return <div ref={ref} listeners={listener} />;
};
ReactDOM.render(<Component />, container);

ref.current.dispatchEvent(
createEvent('pointerdown', {pointerType: 'mouse', button: 0}),
);
ref.current.dispatchEvent(createEvent('contextmenu'));
expect(onContextMenu).toHaveBeenCalledTimes(0);
});

it('is still called if "preventContextMenu" is true', () => {
const onContextMenu = jest.fn();
const ref = React.createRef();

const Component = () => {
const listener = usePressResponder({
onContextMenu,
preventContextMenu: true,
});

return <div ref={ref} listeners={listener} />;
};
ReactDOM.render(<Component />, container);

ref.current.dispatchEvent(
createEvent('pointerdown', {pointerType: 'mouse', button: 2}),
);
ref.current.dispatchEvent(createEvent('contextmenu'));
expect(onContextMenu).toHaveBeenCalledTimes(1);
expect(onContextMenu).toHaveBeenCalledWith(
expect.objectContaining({defaultPrevented: true}),
);
});
});

it('should work correctly with stopPropagation set to true', () => {
const ref = React.createRef();
const pointerDownEvent = jest.fn();
Expand Down

0 comments on commit d9fdec6

Please sign in to comment.