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

[Flare] Remove contextmenu logic from Press #16322

Merged
merged 1 commit into from
Aug 8, 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
40 changes: 1 addition & 39 deletions packages/react-events/src/dom/Press.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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