Merge pull request #4139 from spicyj/dom-refs

DOM components as refs
This commit is contained in:
Ben Alpert committed 2015-06-22 16:58:01 -07:00
commit c5fb3ff987
15 files changed
+304 -189

No files matched your search

@@ -23,7 +23,6 @@ var ReactFragment = require('ReactFragment');
var ReactPropTypeLocations = require('ReactPropTypeLocations');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactNativeComponent = require('ReactNativeComponent');
var getIteratorFn = require('getIteratorFn');
var invariant = require('invariant');
@@ -293,18 +292,10 @@ function checkPropTypes(componentName, propTypes, props, location) {
* @param {ReactElement} element
*/
function validatePropTypes(element) {
if (!(typeof element.type === 'string' ||
typeof element.type === 'function')) {
// This has already warned. Don't throw.
var componentClass = element.type;
if (typeof componentClass !== 'function') {
return;
}
// Extract the component class from the element. Converts string types
// to a composite class which may have propTypes.
// TODO: Validating a string's propTypes is not decoupled from the
// rendering target which is problematic.
var componentClass = ReactNativeComponent.getComponentClassForElement(
element
);
var name = componentClass.displayName || componentClass.name;
if (componentClass.propTypes) {
checkPropTypes(
+26 -6
View File
@@ -274,7 +274,7 @@ function mountComponentIntoNode(
var markup = ReactReconciler.mountComponent(
componentInstance, rootID, transaction, context
);
componentInstance._isTopLevel = true;
componentInstance._renderedComponent._topLevelWrapper = componentInstance;
ReactMount._mountImageIntoNode(markup, container, shouldReuseMarkup);
}
@@ -328,6 +328,17 @@ function unmountComponentFromNode(instance, container) {
}
}
/**
* Temporary (?) hack so that we can store all top-level pending updates on
* composites instead of having to worry about different types of components
* here.
*/
var TopLevelWrapper = function() {};
TopLevelWrapper.prototype.render = function() {
// this.props is actually a ReactElement
return this.props;
};
/**
* Mounting is the process of initializing a React component by creating its
* representative DOM elements and inserting them into a supplied `container`.
@@ -522,17 +533,26 @@ var ReactMount = {
'for your app.'
);
var nextWrappedElement = new ReactElement(
TopLevelWrapper,
null,
null,
null,
nextElement
);
var prevComponent = instancesByReactRootID[getReactRootID(container)];
if (prevComponent) {
var prevElement = prevComponent._currentElement;
var prevWrappedElement = prevComponent._currentElement;
var prevElement = prevWrappedElement.props;
if (shouldUpdateReactComponent(prevElement, nextElement)) {
return ReactMount._updateRootComponent(
prevComponent,
nextElement,
nextWrappedElement,
container,
callback
).getPublicInstance();
)._renderedComponent.getPublicInstance();
} else {
ReactMount.unmountComponentAtNode(container);
}
@@ -563,7 +583,7 @@ var ReactMount = {
var shouldReuseMarkup = containerHasReactMarkup && !prevComponent;
var component = ReactMount._renderNewRootComponent(
nextElement,
nextWrappedElement,
container,
shouldReuseMarkup,
parentComponent != null ?
@@ -571,7 +591,7 @@ var ReactMount = {
parentComponent._reactInternalInstance._context
) :
emptyObject
).getPublicInstance();
)._renderedComponent.getPublicInstance();
if (callback) {
callback.call(component);
}
+1 -2
View File
@@ -17,7 +17,6 @@ var ReactInstanceMap = require('ReactInstanceMap');
var ReactMount = require('ReactMount');
var invariant = require('invariant');
var isNode = require('isNode');
var warning = require('warning');
/**
@@ -45,7 +44,7 @@ function findDOMNode(componentOrElement) {
if (componentOrElement == null) {
return null;
}
if (isNode(componentOrElement)) {
if (componentOrElement.nodeType === 1) {
return componentOrElement;
}
if (ReactInstanceMap.has(componentOrElement)) {
@@ -30,6 +30,7 @@ var ReactDOMTextarea = require('ReactDOMTextarea');
var ReactMount = require('ReactMount');
var ReactMultiChild = require('ReactMultiChild');
var ReactPerf = require('ReactPerf');
var ReactUpdateQueue = require('ReactUpdateQueue');
var assign = require('Object.assign');
var escapeTextContentForBrowser = require('escapeTextContentForBrowser');
@@ -51,6 +52,122 @@ var STYLE = keyOf({style: null});
var ELEMENT_NODE_TYPE = 1;
var canDefineProperty = false;
try {
Object.defineProperty({}, 'test', {get: function() {}});
canDefineProperty = true;
} catch (e) {
}
function getDeclarationErrorAddendum(internalInstance) {
if (internalInstance) {
var owner = internalInstance._currentElement._owner || null;
if (owner) {
var name = owner.getName();
if (name) {
return ' This DOM node was rendered by `' + name + '`.';
}
}
}
return '';
}
var legacyPropsDescriptor;
if (__DEV__) {
legacyPropsDescriptor = {
props: {
enumerable: false,
get: function() {
var component = this._reactInternalComponent;
warning(
false,
'ReactDOMComponent: Do not access .props of a DOM node; instead, ' +
'recreate the props as `render` did originally or read the DOM ' +
'properties/attributes directly from this node (e.g., ' +
'this.refs.box.className).%s',
getDeclarationErrorAddendum(component)
);
return component._currentElement.props;
},
},
};
}
function legacyGetDOMNode() {
if (__DEV__) {
var component = this._reactInternalComponent;
warning(
false,
'ReactDOMComponent: Do not access .getDOMNode() of a DOM node; ' +
'instead, use the node directly.%s',
getDeclarationErrorAddendum(component)
);
}
return this;
}
function legacyIsMounted() {
var component = this._reactInternalComponent;
if (__DEV__) {
warning(
false,
'ReactDOMComponent: Do not access .isMounted() of a DOM node.%s',
getDeclarationErrorAddendum(component)
);
}
return !!component;
}
function legacySetStateEtc() {
if (__DEV__) {
var component = this._reactInternalComponent;
warning(
false,
'ReactDOMComponent: Do not access .setState(), .replaceState(), or ' +
'.forceUpdate() of a DOM node. This is a no-op.%s',
getDeclarationErrorAddendum(component)
);
}
}
function legacySetProps(partialProps, callback) {
var component = this._reactInternalComponent;
if (__DEV__) {
warning(
false,
'ReactDOMComponent: Do not access .setProps() of a DOM node. ' +
'Instead, call React.render again at the top level.%s',
getDeclarationErrorAddendum(component)
);
}
if (!component) {
return;
}
ReactUpdateQueue.enqueueSetPropsInternal(component, partialProps);
if (callback) {
ReactUpdateQueue.enqueueCallbackInternal(component, callback);
}
}
function legacyReplaceProps(partialProps, callback) {
var component = this._reactInternalComponent;
if (__DEV__) {
warning(
false,
'ReactDOMComponent: Do not access .replaceProps() of a DOM node. ' +
'Instead, call React.render again at the top level.%s',
getDeclarationErrorAddendum(component)
);
}
if (!component) {
return;
}
ReactUpdateQueue.enqueueReplacePropsInternal(component, partialProps);
if (callback) {
ReactUpdateQueue.enqueueCallbackInternal(component, callback);
}
}
var styleMutationWarning = {};
function checkAndWarnForMutatedStyle(style1, style2, component) {
@@ -327,6 +444,8 @@ function ReactDOMComponent(tag) {
this._previousStyleCopy = null;
this._rootNodeID = null;
this._wrapperState = null;
this._topLevelWrapper = null;
this._nodeWithLegacyProperties = null;
}
ReactDOMComponent.displayName = 'ReactDOMComponent';
@@ -589,6 +708,10 @@ ReactDOMComponent.Mixin = {
processChildContext(context, this)
);
if (!canDefineProperty && this._nodeWithLegacyProperties) {
this._nodeWithLegacyProperties.props = nextProps;
}
if (this._tag === 'select') {
// <select> value update needs to occur after <option> children
// reconciliation
@@ -818,6 +941,41 @@ ReactDOMComponent.Mixin = {
ReactComponentBrowserEnvironment.unmountIDFromEnvironment(this._rootNodeID);
this._rootNodeID = null;
this._wrapperState = null;
if (this._nodeWithLegacyProperties) {
var node = this._nodeWithLegacyProperties;
node._reactInternalComponent = null;
this._nodeWithLegacyProperties = null;
}
},
getPublicInstance: function() {
if (!this._nodeWithLegacyProperties) {
var node = ReactMount.getNode(this._rootNodeID);
node._reactInternalComponent = this;
node.getDOMNode = legacyGetDOMNode;
node.isMounted = legacyIsMounted;
node.setState = legacySetStateEtc;
node.replaceState = legacySetStateEtc;
node.forceUpdate = legacySetStateEtc;
node.setProps = legacySetProps;
node.replaceProps = legacyReplaceProps;
if (__DEV__) {
if (canDefineProperty) {
Object.defineProperties(node, legacyPropsDescriptor);
} else {
// updateComponent will update this property on subsequent renders
node.props = this._currentElement.props;
}
} else {
// updateComponent will update this property on subsequent renders
node.props = this._currentElement.props;
}
this._nodeWithLegacyProperties = node;
}
return this._nodeWithLegacyProperties;
},
};
@@ -19,18 +19,15 @@ var EnterLeaveEventPlugin = require('EnterLeaveEventPlugin');
var ExecutionEnvironment = require('ExecutionEnvironment');
var HTMLDOMPropertyConfig = require('HTMLDOMPropertyConfig');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactClass = require('ReactClass');
var ReactComponentBrowserEnvironment =
require('ReactComponentBrowserEnvironment');
var ReactDefaultBatchingStrategy = require('ReactDefaultBatchingStrategy');
var ReactDOMComponent = require('ReactDOMComponent');
var ReactDOMIDOperations = require('ReactDOMIDOperations');
var ReactDOMTextComponent = require('ReactDOMTextComponent');
var ReactElement = require('ReactElement');
var ReactEventListener = require('ReactEventListener');
var ReactInjection = require('ReactInjection');
var ReactInstanceHandles = require('ReactInstanceHandles');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactMount = require('ReactMount');
var ReactReconcileTransaction = require('ReactReconcileTransaction');
var SelectEventPlugin = require('SelectEventPlugin');
@@ -38,91 +35,6 @@ var ServerReactRootIndex = require('ServerReactRootIndex');
var SimpleEventPlugin = require('SimpleEventPlugin');
var SVGDOMPropertyConfig = require('SVGDOMPropertyConfig');
var warning = require('warning');
var canDefineProperty = false;
try {
Object.defineProperty({}, 'test', {get: function() {}});
canDefineProperty = true;
} catch (e) {
}
var deprecatedDOMMethods = [
'isMounted', 'replaceProps', 'replaceState', 'setProps', 'setState',
'forceUpdate',
];
function getDeclarationErrorAddendum(domWrapperClass) {
var internalInstance = ReactInstanceMap.get(domWrapperClass);
var owner = internalInstance._currentElement._owner || null;
if (owner) {
var name = owner.getName();
if (name) {
return ' This DOM component was rendered by `' + name + '`.';
}
}
return '';
}
function autoGenerateWrapperClass(type) {
var wrapperClass = ReactClass.createClass({
tagName: type.toUpperCase(),
render: function() {
// Copy owner down for debugging info
var internalInstance = ReactInstanceMap.get(this);
return new ReactElement(
type,
null, // key
null, // ref
internalInstance._currentElement._owner, // owner
this._internalProps || this.props
);
},
});
if (__DEV__) {
if (canDefineProperty) {
Object.defineProperty(wrapperClass.prototype, 'props', {
enumerable: true,
set: function(props) {
this._internalProps = props;
},
get: function() {
warning(
false,
'ReactDOMComponent.props: Do not access .props of a DOM ' +
'component directly; instead, recreate the props as `render` ' +
'did originally or use React.findDOMNode and read the DOM ' +
'properties/attributes directly.%s',
getDeclarationErrorAddendum(this)
);
return this._internalProps;
},
});
deprecatedDOMMethods.forEach(function(method) {
var old = wrapperClass.prototype[method];
Object.defineProperty(wrapperClass.prototype, method, {
enumerable: true,
get: function() {
warning(
false,
'ReactDOMComponent.%s(): Do not access .%s() of a DOM ' +
'component.%s',
method,
method,
getDeclarationErrorAddendum(this)
);
return old;
},
});
});
}
}
return wrapperClass;
}
var alreadyInjected = false;
function inject() {
@@ -165,10 +77,6 @@ function inject() {
ReactDOMTextComponent
);
ReactInjection.NativeComponent.injectAutoWrapper(
autoGenerateWrapperClass
);
ReactInjection.Class.injectMixin(ReactBrowserComponentMixin);
ReactInjection.DOMProperty.injectDOMPropertyConfig(HTMLDOMPropertyConfig);
@@ -805,12 +805,12 @@ describe('ReactDOMComponent', function() {
it('warns on invalid nesting at root', () => {
spyOn(console, 'error');
var p = document.createElement('p');
React.render(<tr />, p);
React.render(<span><p /></span>, p);
expect(console.error.calls.length).toBe(1);
expect(console.error.calls[0].args[0]).toBe(
'Warning: validateDOMNesting(...): <tr> cannot appear as a child of ' +
'<p>. See p > tr.'
'Warning: validateDOMNesting(...): <p> cannot appear as a descendant ' +
'of <p>. See p > ... > p.'
);
});
@@ -921,30 +921,77 @@ describe('ReactDOMComponent', function() {
it('warns when accessing properties on DOM components', function() {
spyOn(console, 'error');
var innerDiv;
var Animal = React.createClass({
render: function() {
return <div ref="div">iguana</div>;
},
componentDidMount: function() {
innerDiv = this.refs.div;
void this.refs.div.props;
void this.refs.div.setProps;
this.refs.div.setState();
expect(this.refs.div.getDOMNode()).toBe(this.refs.div);
expect(this.refs.div.isMounted()).toBe(true);
},
});
ReactTestUtils.renderIntoDocument(<Animal />);
var container = document.createElement('div');
React.render(<Animal />, container);
React.unmountComponentAtNode(container);
expect(innerDiv.isMounted()).toBe(false);
expect(console.error.calls.length).toBe(5);
expect(console.error.calls[0].args[0]).toBe(
'Warning: ReactDOMComponent: Do not access .props of a DOM ' +
'node; instead, recreate the props as `render` did originally or ' +
'read the DOM properties/attributes directly from this node (e.g., ' +
'this.refs.box.className). This DOM node was rendered by `Animal`.'
);
expect(console.error.calls[1].args[0]).toBe(
'Warning: ReactDOMComponent: Do not access .setState(), ' +
'.replaceState(), or .forceUpdate() of a DOM node. This is a no-op. ' +
'This DOM node was rendered by `Animal`.'
);
expect(console.error.calls[2].args[0]).toBe(
'Warning: ReactDOMComponent: Do not access .getDOMNode() of a DOM ' +
'node; instead, use the node directly. This DOM node was ' +
'rendered by `Animal`.'
);
expect(console.error.calls[3].args[0]).toBe(
'Warning: ReactDOMComponent: Do not access .isMounted() of a DOM ' +
'node. This DOM node was rendered by `Animal`.'
);
expect(console.error.calls[4].args[0]).toContain('isMounted');
});
it('handles legacy setProps and replaceProps', function() {
spyOn(console, 'error');
var node = ReactTestUtils.renderIntoDocument(<div>rhinoceros</div>);
node.setProps({className: 'herbiverous'});
expect(node.className).toBe('herbiverous');
expect(node.textContent).toBe('rhinoceros');
node.replaceProps({className: 'invisible rhino'});
expect(node.className).toBe('invisible rhino');
expect(node.textContent).toBe('');
expect(console.error.calls.length).toBe(2);
expect(console.error.calls[0].args[0]).toBe(
'Warning: ReactDOMComponent.props: Do not access .props of a DOM ' +
'component directly; instead, recreate the props as `render` did ' +
'originally or use React.findDOMNode and read the DOM ' +
'properties/attributes directly. This DOM component was rendered ' +
'by `Animal`.'
'Warning: ReactDOMComponent: Do not access .setProps() of a DOM node. ' +
'Instead, call React.render again at the top level.'
);
expect(console.error.calls[1].args[0]).toBe(
'Warning: ReactDOMComponent.setProps(): Do not access .setProps() of ' +
'a DOM component. This DOM component was rendered by `Animal`.'
'Warning: ReactDOMComponent: Do not access .replaceProps() of a DOM ' +
'node. Instead, call React.render again at the top level.'
);
});
it('does not touch ref-less nodes', function() {
var node = ReactTestUtils.renderIntoDocument(<div><span /></div>);
expect(typeof node.getDOMNode).toBe('function');
expect(typeof node.firstChild.getDOMNode).toBe('undefined');
});
});
});
@@ -15,7 +15,6 @@ var ReactComponentEnvironment = require('ReactComponentEnvironment');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactElement = require('ReactElement');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactNativeComponent = require('ReactNativeComponent');
var ReactPerf = require('ReactPerf');
var ReactPropTypeLocations = require('ReactPropTypeLocations');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
@@ -100,7 +99,7 @@ var ReactCompositeComponentMixin = {
this._context = null;
this._mountOrder = 0;
this._isTopLevel = false;
this._topLevelWrapper = null;
// See ReactUpdates and ReactUpdateQueue.
this._pendingCallbacks = null;
@@ -123,9 +122,7 @@ var ReactCompositeComponentMixin = {
var publicProps = this._processProps(this._currentElement.props);
var publicContext = this._processContext(context);
var Component = ReactNativeComponent.getComponentClassForElement(
this._currentElement
);
var Component = this._currentElement.type;
// Initialize the public class
var inst = new Component(publicProps, publicContext);
@@ -235,8 +232,7 @@ var ReactCompositeComponentMixin = {
var renderedElement = this._renderValidatedComponent();
this._renderedComponent = this._instantiateReactComponent(
renderedElement,
this._currentElement.type // The wrapping type
renderedElement
);
var markup = ReactReconciler.mountComponent(
@@ -281,6 +277,7 @@ var ReactCompositeComponentMixin = {
// longer accessible.
this._context = null;
this._rootNodeID = null;
this._topLevelWrapper = null;
// Delete the reference from the instance to this internal representation
// which allow the internals to be properly cleaned up even if the user
@@ -304,9 +301,7 @@ var ReactCompositeComponentMixin = {
*/
_maskContext: function(context) {
var maskedContext = null;
var Component = ReactNativeComponent.getComponentClassForElement(
this._currentElement
);
var Component = this._currentElement.type;
var contextTypes = Component.contextTypes;
if (!contextTypes) {
return emptyObject;
@@ -329,9 +324,7 @@ var ReactCompositeComponentMixin = {
_processContext: function(context) {
var maskedContext = this._maskContext(context);
if (__DEV__) {
var Component = ReactNativeComponent.getComponentClassForElement(
this._currentElement
);
var Component = this._currentElement.type;
if (Component.contextTypes) {
this._checkPropTypes(
Component.contextTypes,
@@ -349,9 +342,7 @@ var ReactCompositeComponentMixin = {
* @private
*/
_processChildContext: function(currentContext) {
var Component = ReactNativeComponent.getComponentClassForElement(
this._currentElement
);
var Component = this._currentElement.type;
var inst = this._instance;
var childContext = inst.getChildContext && inst.getChildContext();
if (childContext) {
@@ -392,9 +383,7 @@ var ReactCompositeComponentMixin = {
*/
_processProps: function(newProps) {
if (__DEV__) {
var Component = ReactNativeComponent.getComponentClassForElement(
this._currentElement
);
var Component = this._currentElement.type;
if (Component.propTypes) {
this._checkPropTypes(
Component.propTypes,
@@ -695,8 +684,7 @@ var ReactCompositeComponentMixin = {
ReactReconciler.unmountComponent(prevComponentInstance);
this._renderedComponent = this._instantiateReactComponent(
nextRenderedElement,
this._currentElement.type
nextRenderedElement
);
var nextMarkup = ReactReconciler.mountComponent(
this._renderedComponent,
@@ -36,11 +36,6 @@ var ReactNativeComponentInjection = {
injectComponentClasses: function(componentClasses) {
assign(tagToComponentClass, componentClasses);
},
// Temporary hack since we expect DOM refs to behave like composites,
// for this release.
injectAutoWrapper: function(wrapperFactory) {
autoGenerateWrapperClass = wrapperFactory;
},
};
/**
@@ -245,13 +245,16 @@ var ReactUpdateQueue = {
publicInstance,
'setProps'
);
if (!internalInstance) {
return;
}
ReactUpdateQueue.enqueueSetPropsInternal(internalInstance, partialProps);
},
enqueueSetPropsInternal: function(internalInstance, partialProps) {
var topLevelWrapper = internalInstance._topLevelWrapper;
invariant(
internalInstance._isTopLevel,
topLevelWrapper,
'setProps(...): You called `setProps` on a ' +
'component with a parent. This is an anti-pattern since props will ' +
'get reactively updated when rendered. Instead, change the owner\'s ' +
@@ -261,15 +264,16 @@ var ReactUpdateQueue = {
// Merge with the pending element if it exists, otherwise with existing
// element props.
var element = internalInstance._pendingElement ||
internalInstance._currentElement;
var wrapElement = topLevelWrapper._pendingElement ||
topLevelWrapper._currentElement;
var element = wrapElement.props;
var props = assign({}, element.props, partialProps);
internalInstance._pendingElement = ReactElement.cloneAndReplaceProps(
element,
props
topLevelWrapper._pendingElement = ReactElement.cloneAndReplaceProps(
wrapElement,
ReactElement.cloneAndReplaceProps(element, props)
);
enqueueUpdate(internalInstance);
enqueueUpdate(topLevelWrapper);
},
/**
@@ -284,13 +288,16 @@ var ReactUpdateQueue = {
publicInstance,
'replaceProps'
);
if (!internalInstance) {
return;
}
ReactUpdateQueue.enqueueReplacePropsInternal(internalInstance, props);
},
enqueueReplacePropsInternal: function(internalInstance, props) {
var topLevelWrapper = internalInstance._topLevelWrapper;
invariant(
internalInstance._isTopLevel,
topLevelWrapper,
'replaceProps(...): You called `replaceProps` on a ' +
'component with a parent. This is an anti-pattern since props will ' +
'get reactively updated when rendered. Instead, change the owner\'s ' +
@@ -300,14 +307,15 @@ var ReactUpdateQueue = {
// Merge with the pending element if it exists, otherwise with existing
// element props.
var element = internalInstance._pendingElement ||
internalInstance._currentElement;
internalInstance._pendingElement = ReactElement.cloneAndReplaceProps(
element,
props
var wrapElement = topLevelWrapper._pendingElement ||
topLevelWrapper._currentElement;
var element = wrapElement.props;
topLevelWrapper._pendingElement = ReactElement.cloneAndReplaceProps(
wrapElement,
ReactElement.cloneAndReplaceProps(element, props)
);
enqueueUpdate(internalInstance);
enqueueUpdate(topLevelWrapper);
},
enqueueElementInternal: function(internalInstance, newElement) {
@@ -267,14 +267,19 @@ describe('ReactComponent', function() {
it('warns when calling getDOMNode', function() {
spyOn(console, 'error');
var Potato = React.createClass({
render: function() {
return <div />;
},
});
var container = document.createElement('div');
var instance = React.render(<div />, container);
var instance = React.render(<Potato />, container);
instance.getDOMNode();
expect(console.error.calls.length).toBe(1);
expect(console.error.calls[0].args[0]).toContain(
'DIV.getDOMNode(...) is deprecated. Please use ' +
'Potato.getDOMNode(...) is deprecated. Please use ' +
'React.findDOMNode(instance) instead.'
);
});
@@ -12,7 +12,6 @@
'use strict';
var React = require('React');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactTestUtils = require('ReactTestUtils');
var ReactMount = require('ReactMount');
@@ -63,11 +62,11 @@ describe('ReactInstanceHandles', function() {
});
}
function getNodeID(instance) {
if (instance === null) {
function getNodeID(el) {
if (el === null) {
return '';
}
return ReactInstanceMap.get(instance)._rootNodeID;
return el.getAttribute('data-reactid');
}
beforeEach(function() {
@@ -389,8 +388,8 @@ describe('ReactInstanceHandles', function() {
// Common ancestor with grandparent is the grandparent.
{
one: parent.refs.P_P1_C1.refs.DIV_1,
two: parent.refs.P_P1_C1,
com: parent.refs.P_P1_C1,
two: parent.refs.P_P1,
com: parent.refs.P_P1,
},
// Grantparent across subcomponent boundaries.
{
@@ -89,7 +89,6 @@ var FriendsStatusDisplay = React.createClass({
var statusDisplays =
ReactInstanceMap.get(this)
._renderedComponent
._renderedComponent
._renderedChildren;
for (name in statusDisplays) {
var child = statusDisplays[name];
@@ -60,11 +60,10 @@ function isInternalComponentType(type) {
* Given a ReactNode, create an instance that will actually be mounted.
*
* @param {ReactNode} node
* @param {*} parentCompositeType The composite type that resolved this.
* @return {object} A new instance of the element's constructor.
* @protected
*/
function instantiateReactComponent(node, parentCompositeType) {
function instantiateReactComponent(node) {
var instance;
if (node === null || node === false) {
@@ -83,12 +82,8 @@ function instantiateReactComponent(node, parentCompositeType) {
);
// Special case string values
if (parentCompositeType === element.type &&
typeof element.type === 'string') {
// Avoid recursion if the wrapper renders itself.
if (typeof element.type === 'string') {
instance = ReactNativeComponent.createInternalComponent(element);
// All native components are currently wrapped in a composite so we're
// safe to assume that this is what we should instantiate.
} else if (isInternalComponentType(element.type)) {
// This is temporarily available for custom components that are not string
// represenations. I.e. ART. Once those are updated to use the string
+8 -5
View File
@@ -45,7 +45,7 @@ function findAllInRenderedTreeInternal(inst, test) {
var publicInst = inst.getPublicInstance()
var ret = test(publicInst) ? [publicInst] : [];
if (ReactTestUtils.isDOMComponent(publicInst)) {
var renderedChildren = inst._renderedComponent._renderedChildren;
var renderedChildren = inst._renderedChildren;
var key;
for (key in renderedChildren) {
if (!renderedChildren.hasOwnProperty(key)) {
@@ -96,7 +96,7 @@ var ReactTestUtils = {
isDOMComponent: function(inst) {
// TODO: Fix this heuristic. It's just here because composites can currently
// pretend to be DOM components.
return !!(inst && inst.tagName && inst.getDOMNode);
return !!(inst && inst.nodeType === 1 && inst.tagName);
},
isDOMComponentElement: function(inst) {
@@ -116,13 +116,15 @@ var ReactTestUtils = {
},
isCompositeComponentWithType: function(inst, type) {
if (!ReactTestUtils.isCompositeComponent(inst)) {
return false;
}
var internalInstance = ReactInstanceMap.get(inst);
var constructor = internalInstance
._currentElement
.type;
return !!(ReactTestUtils.isCompositeComponent(inst) &&
(constructor === type));
return (constructor === type);
},
isCompositeComponentElement: function(inst) {
@@ -256,7 +258,8 @@ var ReactTestUtils = {
);
if (all.length !== 1) {
throw new Error(
'Did not find exactly one match for componentType:' + componentType
'Did not find exactly one match for componentType:' + componentType +
' (found ' + all.length + ')'
);
}
return all[0];
+3 -3
View File
@@ -85,7 +85,7 @@ assign(reactComponentExpectInternal.prototype, {
// change soon.
this.toBeDOMComponent();
var renderedChildren =
this._instance._renderedComponent._renderedChildren || {};
this._instance._renderedChildren || {};
for (var name in renderedChildren) {
if (!renderedChildren.hasOwnProperty(name)) {
continue;
@@ -101,7 +101,7 @@ assign(reactComponentExpectInternal.prototype, {
toBeDOMComponentWithChildCount: function(count) {
this.toBeDOMComponent();
var renderedChildren = this._instance._renderedComponent._renderedChildren;
var renderedChildren = this._instance._renderedChildren;
expect(renderedChildren).toBeTruthy();
expect(Object.keys(renderedChildren).length).toBe(count);
return this;
@@ -109,7 +109,7 @@ assign(reactComponentExpectInternal.prototype, {
toBeDOMComponentWithNoChildren: function() {
this.toBeDOMComponent();
expect(this._instance._renderedComponent._renderedChildren).toBeFalsy();
expect(this._instance._renderedChildren).toBeFalsy();
return this;
},