-
Notifications
You must be signed in to change notification settings - Fork 46.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate events to parent components when nested
When events are captured, the nearest React-rendered ancestor is found and events are propagated to its tree. This causes issues when components are nested as only the innermost component receives the events. This change modifies this behaviour so events propagate all the way up the DOM hierarchy. To reduce the performance cost, this DOM traversal is only done if the component is actually nested (determined by the `containerIsNested` map which is lazily initialised).
- Loading branch information
Showing
3 changed files
with
142 additions
and
8 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
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,109 @@ | ||
/** | ||
* Copyright 2013 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
require('mock-modules') | ||
.dontMock('ReactEventTopLevelCallback') | ||
.dontMock('ReactMount') | ||
.dontMock('ReactInstanceHandles') | ||
.dontMock('ReactDOM') | ||
.mock('ReactEventEmitter'); | ||
|
||
var EVENT_TARGET_PARAM = 1; | ||
|
||
describe('ReactEventTopLevelCallback', function() { | ||
var ReactEventTopLevelCallback; | ||
var ReactMount; | ||
var ReactDOM; | ||
var ReactEventEmitter; // mocked | ||
|
||
beforeEach(function() { | ||
require('mock-modules').dumpCache(); | ||
ReactEventTopLevelCallback = require('ReactEventTopLevelCallback'); | ||
ReactMount = require('ReactMount'); | ||
ReactDOM = require('ReactDOM'); | ||
ReactEventEmitter = require('ReactEventEmitter'); // mocked | ||
}); | ||
|
||
describe('Propagation', function() { | ||
it('should propagate events one level down', function() { | ||
var childContainer = document.createElement('div'); | ||
var childControl = ReactDOM.div({}, 'Child'); | ||
var parentContainer = document.createElement('div'); | ||
var parentControl = ReactDOM.div({}, 'Parent'); | ||
ReactMount.renderComponent(childControl, childContainer); | ||
ReactMount.renderComponent(parentControl, parentContainer); | ||
parentControl.getDOMNode().appendChild(childContainer); | ||
|
||
var callback = ReactEventTopLevelCallback.createTopLevelCallback('test'); | ||
callback({ | ||
target: childControl.getDOMNode() | ||
}); | ||
|
||
var calls = ReactEventEmitter.handleTopLevel.mock.calls; | ||
expect(calls.length).toBe(2); | ||
expect(calls[0][EVENT_TARGET_PARAM]).toBe(childControl.getDOMNode()); | ||
expect(calls[1][EVENT_TARGET_PARAM]).toBe(parentControl.getDOMNode()); | ||
}); | ||
|
||
it('should propagate events two levels down', function() { | ||
var childContainer = document.createElement('div'); | ||
var childControl = ReactDOM.div({}, 'Child'); | ||
var parentContainer = document.createElement('div'); | ||
var parentControl = ReactDOM.div({}, 'Parent'); | ||
var grandParentContainer = document.createElement('div'); | ||
var grandParentControl = ReactDOM.div({}, 'Parent'); | ||
ReactMount.renderComponent(childControl, childContainer); | ||
ReactMount.renderComponent(parentControl, parentContainer); | ||
ReactMount.renderComponent(grandParentControl, grandParentContainer); | ||
parentControl.getDOMNode().appendChild(childContainer); | ||
grandParentControl.getDOMNode().appendChild(parentContainer); | ||
|
||
var callback = ReactEventTopLevelCallback.createTopLevelCallback('test'); | ||
callback({ | ||
target: childControl.getDOMNode() | ||
}); | ||
|
||
var calls = ReactEventEmitter.handleTopLevel.mock.calls; | ||
expect(calls.length).toBe(3); | ||
expect(calls[0][EVENT_TARGET_PARAM]).toBe(childControl.getDOMNode()); | ||
expect(calls[1][EVENT_TARGET_PARAM]).toBe(parentControl.getDOMNode()); | ||
expect(calls[2][EVENT_TARGET_PARAM]) | ||
.toBe(grandParentControl.getDOMNode()); | ||
}); | ||
}); | ||
|
||
it('should not fire duplicate events for a React DOM tree', function() { | ||
var container = document.createElement('div'); | ||
var inner = ReactDOM.div({}, 'Inner'); | ||
var control = ReactDOM.div({}, [ | ||
ReactDOM.div({id: 'outer'}, inner) | ||
]); | ||
ReactMount.renderComponent(control, container); | ||
|
||
var callback = ReactEventTopLevelCallback.createTopLevelCallback('test'); | ||
callback({ | ||
target: inner.getDOMNode() | ||
}); | ||
|
||
var calls = ReactEventEmitter.handleTopLevel.mock.calls; | ||
expect(calls.length).toBe(1); | ||
expect(calls[0][EVENT_TARGET_PARAM]).toBe(inner.getDOMNode()); | ||
}); | ||
}); |