Skip to content

Commit

Permalink
Add support for auxclick event
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Nov 16, 2017
1 parent e0e9131 commit 95dfd73
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/react-dom/src/client/ReactDOMClientInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as EventPluginHub from 'events/EventPluginHub';
import * as EventPluginUtils from 'events/EventPluginUtils';

import * as ReactDOMComponentTree from './ReactDOMComponentTree';
import AuxClickEventPlugin from '../events/AuxClickEventPlugin';
import BeforeInputEventPlugin from '../events/BeforeInputEventPlugin';
import ChangeEventPlugin from '../events/ChangeEventPlugin';
import DOMEventPluginOrder from '../events/DOMEventPluginOrder';
Expand All @@ -32,6 +33,7 @@ EventPluginUtils.injection.injectComponentTree(ReactDOMComponentTree);
*/
EventPluginHub.injection.injectEventPluginsByName({
SimpleEventPlugin: SimpleEventPlugin,
AuxClickEventPlugin: AuxClickEventPlugin,
EnterLeaveEventPlugin: EnterLeaveEventPlugin,
ChangeEventPlugin: ChangeEventPlugin,
SelectEventPlugin: SelectEventPlugin,
Expand Down
50 changes: 50 additions & 0 deletions packages/react-dom/src/events/AuxClickEventPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import {accumulateTwoPhaseDispatches} from 'events/EventPropagators';

import SyntheticMouseEvent from './SyntheticMouseEvent';

const eventTypes = {
auxClick: {
phasedRegistrationNames: {
bubbled: 'onAuxClick',
captured: 'onAuxClickCapture',
},
dependencies: ['topAuxClick', 'topClick'],
},
};

const AuxClickEventPlugin = {
eventTypes,

extractEvents(
topLevelType: mixed,
targetInst: mixed,
nativeEvent: MouseEvent,
nativeEventTarget: EventTarget,
) {
if (topLevelType === 'topClick' && nativeEvent.button === 0) {
return null;
}

let event = SyntheticMouseEvent.getPooled(
eventTypes.auxClick,
targetInst,
nativeEvent,
nativeEventTarget,
);
event.type = 'auxclick';

accumulateTwoPhaseDispatches(event);
return event;
},
};

export default AuxClickEventPlugin;
1 change: 1 addition & 0 deletions packages/react-dom/src/events/BrowserEventConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var topLevelTypes = {
getVendorPrefixedEventName('animationiteration') || 'animationiteration',
topAnimationStart:
getVendorPrefixedEventName('animationstart') || 'animationstart',
topAuxClick: 'auxclick',
topBlur: 'blur',
topCancel: 'cancel',
topCanPlay: 'canplay',
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/events/DOMEventPluginOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
var DOMEventPluginOrder = [
'ResponderEventPlugin',
'SimpleEventPlugin',
'AuxClickEventPlugin',
'TapEventPlugin',
'EnterLeaveEventPlugin',
'ChangeEventPlugin',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

var React;
var ReactDOM;

describe('AuxClickEventPlugin', () => {
var container;

beforeEach(() => {
jest.resetModules();

React = require('react');
ReactDOM = require('react-dom');

// The container has to be attached for events to fire.
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('should not fire auxclick on primary mouse button click', () => {
let cb = jest.fn();
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);

node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
button: 0,
}),
);

expect(cb).not.toBeCalled();
});

it('should fire auxclick on secondary mouse button click', () => {
let cb = jest.fn();
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);

node.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
button: 1,
}),
);

expect(cb).toBeCalled();
});

it('should respond to native auxclick', () => {
let cb = jest.fn();
let node = ReactDOM.render(<button onAuxClick={cb}>foo</button>, container);

node.dispatchEvent(
new MouseEvent('auxclick', {
bubbles: true,
cancelable: true,
button: 1,
}),
);

expect(cb).toBeCalled();
});
});

0 comments on commit 95dfd73

Please sign in to comment.