Merge pull request #4513 from jimfb/fix-global-events

Fixed global events (like enter/leave events) when event has path.
This commit is contained in:
Jim
2015-07-29 16:59:31 -07:00
2 changed files with 31 additions and 0 deletions
@@ -104,6 +104,7 @@ function handleTopLevelWithoutPath(bookKeeping) {
function handleTopLevelWithPath(bookKeeping) {
var path = bookKeeping.nativeEvent.path;
var currentNativeTarget = path[0];
var eventsFired = 0;
for (var i = 0; i < path.length; i++) {
var currentPathElement = path[i];
var currentPathElementID = ReactMount.getID(currentPathElement);
@@ -117,6 +118,7 @@ function handleTopLevelWithPath(bookKeeping) {
bookKeeping.ancestors.push(currentPathElement);
var topLevelTargetID = ReactMount.getID(currentPathElement) || '';
eventsFired++;
ReactEventListener._handleTopLevel(
bookKeeping.topLevelType,
currentPathElement,
@@ -133,6 +135,15 @@ function handleTopLevelWithPath(bookKeeping) {
}
}
}
if (eventsFired === 0) {
ReactEventListener._handleTopLevel(
bookKeeping.topLevelType,
window,
'',
bookKeeping.nativeEvent,
getEventTarget(bookKeeping.nativeEvent)
);
}
}
function scrollValueMonitor(cb) {
@@ -36,6 +36,26 @@ describe('ReactEventListener', function() {
ReactEventListener._handleTopLevel = handleTopLevel;
});
it('should dispatch events from outside React tree', function() {
var otherNode = document.createElement('h1');
var component = ReactMount.render(<div />, document.createElement('div'));
expect(handleTopLevel.mock.calls.length).toBe(0);
ReactEventListener.dispatchEvent(
'topMouseOut',
{
type: 'mouseout',
fromElement: otherNode,
target: otherNode,
srcElement: otherNode,
toElement: React.findDOMNode(component),
relatedTarget: React.findDOMNode(component),
view: window,
path: [otherNode, otherNode],
},
);
expect(handleTopLevel.mock.calls.length).toBe(1);
});
describe('Propagation', function() {
it('should propagate events one level down', function() {
var childContainer = document.createElement('div');