[Flare] Remove contextmenu logic from Press (#16322)

This commit is contained in:
Dominic Gannaway
2019-08-08 21:42:01 +01:00
committed by GitHub
parent 12be8938a5
commit d9fdec6cfe
2 changed files with 1 additions and 162 deletions
+1 -39
View File
@@ -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,
@@ -74,8 +72,7 @@ type PressEventType =
| 'pressmove'
| 'pressstart'
| 'pressend'
| 'presschange'
| 'contextmenu';
| 'presschange';
type PressEvent = {|
button: 'primary' | 'auxillary',
@@ -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',
@@ -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();
@@ -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();