Skip to content

Commit b93c55a

Browse files
jquensenhunzaker
authored andcommitted
Add support for auxclick event
1 parent b179bae commit b93c55a

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

packages/react-dom/src/client/ReactDOMClientInjection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as EventPluginHub from 'events/EventPluginHub';
99
import * as EventPluginUtils from 'events/EventPluginUtils';
1010

1111
import * as ReactDOMComponentTree from './ReactDOMComponentTree';
12+
import AuxClickEventPlugin from '../events/AuxClickEventPlugin';
1213
import BeforeInputEventPlugin from '../events/BeforeInputEventPlugin';
1314
import ChangeEventPlugin from '../events/ChangeEventPlugin';
1415
import DOMEventPluginOrder from '../events/DOMEventPluginOrder';
@@ -28,6 +29,7 @@ EventPluginUtils.injection.injectComponentTree(ReactDOMComponentTree);
2829
*/
2930
EventPluginHub.injection.injectEventPluginsByName({
3031
SimpleEventPlugin: SimpleEventPlugin,
32+
AuxClickEventPlugin: AuxClickEventPlugin,
3133
EnterLeaveEventPlugin: EnterLeaveEventPlugin,
3234
ChangeEventPlugin: ChangeEventPlugin,
3335
SelectEventPlugin: SelectEventPlugin,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {accumulateTwoPhaseDispatches} from 'events/EventPropagators';
11+
12+
import SyntheticMouseEvent from './SyntheticMouseEvent';
13+
14+
const eventTypes = {
15+
auxClick: {
16+
phasedRegistrationNames: {
17+
bubbled: 'onAuxClick',
18+
captured: 'onAuxClickCapture',
19+
},
20+
dependencies: ['topAuxClick', 'topClick'],
21+
},
22+
};
23+
24+
const AuxClickEventPlugin = {
25+
eventTypes,
26+
27+
extractEvents(
28+
topLevelType: mixed,
29+
targetInst: mixed,
30+
nativeEvent: MouseEvent,
31+
nativeEventTarget: EventTarget,
32+
) {
33+
if (topLevelType === 'topClick' && nativeEvent.button === 0) {
34+
return null;
35+
}
36+
37+
let event = SyntheticMouseEvent.getPooled(
38+
eventTypes.auxClick,
39+
targetInst,
40+
nativeEvent,
41+
nativeEventTarget,
42+
);
43+
event.type = 'auxclick';
44+
45+
accumulateTwoPhaseDispatches(event);
46+
return event;
47+
},
48+
};
49+
50+
export default AuxClickEventPlugin;

packages/react-dom/src/events/DOMEventPluginOrder.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
const DOMEventPluginOrder = [
1818
'ResponderEventPlugin',
1919
'SimpleEventPlugin',
20+
'AuxClickEventPlugin',
2021
'TapEventPlugin',
2122
'EnterLeaveEventPlugin',
2223
'ChangeEventPlugin',
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
var React;
13+
var ReactDOM;
14+
15+
describe('AuxClickEventPlugin', () => {
16+
var container;
17+
18+
beforeEach(() => {
19+
jest.resetModules();
20+
21+
React = require('react');
22+
ReactDOM = require('react-dom');
23+
24+
// The container has to be attached for events to fire.
25+
container = document.createElement('div');
26+
document.body.appendChild(container);
27+
});
28+
29+
afterEach(() => {
30+
document.body.removeChild(container);
31+
container = null;
32+
});
33+
34+
it('should not fire auxclick on primary mouse button click', () => {
35+
let cb = jest.fn();
36+
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);
37+
38+
node.dispatchEvent(
39+
new MouseEvent('click', {
40+
bubbles: true,
41+
cancelable: true,
42+
button: 0,
43+
}),
44+
);
45+
46+
expect(cb).not.toBeCalled();
47+
});
48+
49+
it('should fire auxclick on secondary mouse button click', () => {
50+
let cb = jest.fn();
51+
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);
52+
53+
node.dispatchEvent(
54+
new MouseEvent('click', {
55+
bubbles: true,
56+
cancelable: true,
57+
button: 1,
58+
}),
59+
);
60+
61+
expect(cb).toBeCalled();
62+
});
63+
64+
it('should respond to native auxclick', () => {
65+
let cb = jest.fn();
66+
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);
67+
68+
node.dispatchEvent(
69+
new MouseEvent('auxclick', {
70+
bubbles: true,
71+
cancelable: true,
72+
button: 1,
73+
}),
74+
);
75+
76+
expect(cb).toBeCalled();
77+
});
78+
});

0 commit comments

Comments
 (0)