-
Notifications
You must be signed in to change notification settings - Fork 47.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2646 from jsfb/getDOMNode-becomes-findDOMNode
Added findDOMNode, as we move toward deprecating getDOMNode
- Loading branch information
Showing
4 changed files
with
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2013-2014, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
"use strict"; | ||
|
||
var React = require('React'); | ||
var ReactTestUtils = require('ReactTestUtils'); | ||
|
||
describe('findDOMNode', function() { | ||
it('findDOMNode should return null if passed null', function() { | ||
expect(React.findDOMNode(null)).toBe(null); | ||
}); | ||
|
||
it('findDOMNode should find dom element', function() { | ||
var MyNode = React.createClass({ | ||
render: function() { | ||
return <div><span>Noise</span></div>; | ||
} | ||
}); | ||
|
||
var myNode = ReactTestUtils.renderIntoDocument(<MyNode />); | ||
var myDiv = React.findDOMNode(myNode); | ||
var mySameDiv = React.findDOMNode(myDiv); | ||
expect(myDiv.tagName).toBe('DIV'); | ||
expect(mySameDiv).toBe(myDiv); | ||
}); | ||
|
||
it('findDOMNode should reject random objects', function() { | ||
expect(function() {React.findDOMNode({foo: 'bar'});}) | ||
.toThrow('Invariant Violation: Element appears to be neither ' + | ||
'ReactComponent nor DOMNode (keys: foo)' | ||
); | ||
}); | ||
|
||
it('findDOMNode should reject unmounted objects with render func', function() { | ||
expect(function() {React.findDOMNode({render: function(){}});}) | ||
.toThrow('Invariant Violation: Component contains `render` ' + | ||
'method but is not mounted in the DOM' | ||
); | ||
}); | ||
|
||
}); | ||
|
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 2013-2014, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule findDOMNode | ||
* @typechecks static-only | ||
*/ | ||
|
||
"use strict"; | ||
|
||
var ReactComponent = require('ReactComponent'); | ||
var ReactInstanceMap = require('ReactInstanceMap'); | ||
var ReactMount = require('ReactMount'); | ||
|
||
var invariant = require('invariant'); | ||
var isNode = require('isNode'); | ||
|
||
/** | ||
* Returns the DOM node rendered by this element. | ||
* | ||
* @param {ReactComponent|DOMElement} element | ||
* @return {DOMElement} The root node of this element. | ||
*/ | ||
function findDOMNode(componentOrElement) { | ||
if (componentOrElement == null) { | ||
return null; | ||
} | ||
if (isNode(componentOrElement)) { | ||
return componentOrElement; | ||
} | ||
if (ReactInstanceMap.has(componentOrElement)) { | ||
return ReactMount.getNodeFromInstance(componentOrElement); | ||
} | ||
invariant( | ||
!(componentOrElement.render != null && typeof(componentOrElement.render) === 'function'), | ||
'Component contains `render` method but is not mounted in the DOM', | ||
Object.keys(componentOrElement) | ||
); | ||
invariant( | ||
false, | ||
'Element appears to be neither ReactComponent nor DOMNode (keys: %s)', | ||
Object.keys(componentOrElement) | ||
); | ||
} | ||
|
||
module.exports = findDOMNode; |
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