-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/react-dom/src/events/__tests__/AuxClickEventPlugin-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |