Skip to content

Commit

Permalink
Drop usage of plain functions to create descriptors
Browse files Browse the repository at this point in the history
Replace plain function calls to legacy factories with createFactory or
createElement. For ReactDOMComponents the type should be replaced with
strings.

Because we don't have easy access to ReactLegacyDescriptor from within
React, we need to use the .type property to extract the real class.
This will go away later and is covered by unit tests.
  • Loading branch information
sebmarkbage authored and zpao committed Aug 20, 2014
1 parent c901b10 commit 989e6b6
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 31 deletions.
8 changes: 6 additions & 2 deletions src/addons/transitions/ReactCSSTransitionGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

var React = require('React');

var ReactTransitionGroup = require('ReactTransitionGroup');
var ReactCSSTransitionGroupChild = require('ReactCSSTransitionGroupChild');
var ReactTransitionGroup = React.createFactory(
require('ReactTransitionGroup')
);
var ReactCSSTransitionGroupChild = React.createFactory(
require('ReactCSSTransitionGroupChild')
);

var merge = require('merge');

Expand Down
6 changes: 5 additions & 1 deletion src/addons/transitions/ReactTransitionGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ var ReactTransitionGroup = React.createClass({
);
}
}
return this.props.component(this.props, childrenToRender);
return React.createElement(
this.props.component,
this.props,
childrenToRender
);
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/browser/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var React = require('React');
var ReactDOM = require('ReactDOM');
var ReactMount = require('ReactMount');
var ReactTestUtils = require('ReactTestUtils');
var div = React.createFactory(ReactDOM.div); // TODO: use string

describe('ReactDOM', function() {
// TODO: uncomment this test once we can run in phantom, which
Expand Down Expand Up @@ -58,15 +59,15 @@ describe('ReactDOM', function() {

it("should allow children to be passed as an argument", function() {
var argDiv = ReactTestUtils.renderIntoDocument(
ReactDOM.div(null, 'child')
div(null, 'child')
);
var argNode = ReactMount.getNode(argDiv._rootNodeID);
expect(argNode.innerHTML).toBe('child');
});

it("should overwrite props.children with children argument", function() {
var conflictDiv = ReactTestUtils.renderIntoDocument(
ReactDOM.div({children: 'fakechild'}, 'child')
div({children: 'fakechild'}, 'child')
);
var conflictNode = ReactMount.getNode(conflictDiv._rootNodeID);
expect(conflictNode.innerHTML).toBe('child');
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
var AutoFocusMixin = require('AutoFocusMixin');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');

var keyMirror = require('keyMirror');

// Store a reference to the <button> `ReactDOMComponent`.
var button = ReactDOM.button;
// Store a reference to the <button> `ReactDOMComponent`. TODO: use string
var button = ReactDescriptor.createFactory(ReactDOM.button.type);

var mouseListenerNames = keyMirror({
onClick: true,
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ var EventConstants = require('EventConstants');
var LocalEventTrapMixin = require('LocalEventTrapMixin');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');

// Store a reference to the <form> `ReactDOMComponent`.
var form = ReactDOM.form;
// Store a reference to the <form> `ReactDOMComponent`. TODO: use string
var form = ReactDescriptor.createFactory(ReactDOM.form.type);

/**
* Since onSubmit doesn't bubble OR capture on the top level in IE8, we need
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ var EventConstants = require('EventConstants');
var LocalEventTrapMixin = require('LocalEventTrapMixin');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');

// Store a reference to the <img> `ReactDOMComponent`.
var img = ReactDOM.img;
// Store a reference to the <img> `ReactDOMComponent`. TODO: use string
var img = ReactDescriptor.createFactory(ReactDOM.img.type);

/**
* Since onLoad doesn't bubble OR capture on the top level in IE8, we need to
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ var DOMPropertyOperations = require('DOMPropertyOperations');
var LinkedValueUtils = require('LinkedValueUtils');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');
var ReactMount = require('ReactMount');
var ReactUpdates = require('ReactUpdates');

var invariant = require('invariant');
var merge = require('merge');

// Store a reference to the <input> `ReactDOMComponent`.
var input = ReactDOM.input;
// Store a reference to the <input> `ReactDOMComponent`. TODO: use string
var input = ReactDescriptor.createFactory(ReactDOM.input.type);

var instancesByReactID = {};

Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');

var warning = require('warning');

// Store a reference to the <option> `ReactDOMComponent`.
var option = ReactDOM.option;
// Store a reference to the <option> `ReactDOMComponent`. TODO: use string
var option = ReactDescriptor.createFactory(ReactDOM.option.type);

/**
* Implements an <option> native component that warns when `selected` is set.
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var AutoFocusMixin = require('AutoFocusMixin');
var LinkedValueUtils = require('LinkedValueUtils');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');
var ReactUpdates = require('ReactUpdates');

var merge = require('merge');

// Store a reference to the <select> `ReactDOMComponent`.
var select = ReactDOM.select;
// Store a reference to the <select> `ReactDOMComponent`. TODO: use string
var select = ReactDescriptor.createFactory(ReactDOM.select.type);

function updateWithPendingValueIfMounted() {
/*jshint validthis:true */
Expand Down
5 changes: 3 additions & 2 deletions src/browser/ui/dom/components/ReactDOMTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations');
var LinkedValueUtils = require('LinkedValueUtils');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactDOM = require('ReactDOM');
var ReactUpdates = require('ReactUpdates');

Expand All @@ -31,8 +32,8 @@ var merge = require('merge');

var warning = require('warning');

// Store a reference to the <textarea> `ReactDOMComponent`.
var textarea = ReactDOM.textarea;
// Store a reference to the <textarea> `ReactDOMComponent`. TODO: use string
var textarea = ReactDescriptor.createFactory(ReactDOM.textarea.type);

function forceUpdateIfMounted() {
/*jshint validthis:true */
Expand Down
5 changes: 4 additions & 1 deletion src/browser/ui/dom/components/createFullPageComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

// Defeat circular references by requiring this directly.
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDescriptor = require('ReactDescriptor');

var invariant = require('invariant');

Expand All @@ -36,6 +37,8 @@ var invariant = require('invariant');
* @return {function} convenience constructor of new component
*/
function createFullPageComponent(componentClass) {
var elementFactory = ReactDescriptor.createFactory(componentClass.type);

var FullPageComponent = ReactCompositeComponent.createClass({
displayName: 'ReactFullPageComponent' + (
componentClass.type.displayName || ''
Expand All @@ -53,7 +56,7 @@ function createFullPageComponent(componentClass) {
},

render: function() {
return componentClass(this.props);
return elementFactory(this.props);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/core/ReactEmptyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

"use strict";

var ReactDescriptor = require('ReactDescriptor');

var invariant = require('invariant');

var component;
Expand All @@ -27,7 +29,7 @@ var nullComponentIdsRegistry = {};

var ReactEmptyComponentInjection = {
injectEmptyComponent: function(emptyComponent) {
component = emptyComponent;
component = ReactDescriptor.createFactory(emptyComponent.type);
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ describe('ReactCompositeComponent', function() {
console.log(this.getDOMNode());
},
render: function() {
return this.state.component ? this.state.component() : null;
var component = this.state.component;
return component ? <component /> : null;
}
});

Expand Down Expand Up @@ -448,7 +449,7 @@ describe('ReactCompositeComponent', function() {
});

var inputProps = {};
var instance1 = Component(inputProps);
var instance1 = <Component {...inputProps} />;
instance1 = ReactTestUtils.renderIntoDocument(instance1);
expect(instance1.props.prop).toBe('testKey');

Expand Down Expand Up @@ -976,7 +977,7 @@ describe('ReactCompositeComponent', function() {
'because the function is expected to return a value.'
);

NamedComponent(); // Shut up lint
<NamedComponent />; // Shut up lint
} finally {
console.warn = warn;
}
Expand Down
10 changes: 6 additions & 4 deletions src/core/__tests__/ReactMultiChildReconcile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ function verifyDomOrderingAccurate(parentInstance, statusDisplays) {
*/
function testPropsSequence(sequence) {
var i;
var parentInstance =
ReactTestUtils.renderIntoDocument(FriendsStatusDisplay(sequence[0]));
var parentInstance = ReactTestUtils.renderIntoDocument(
<FriendsStatusDisplay {...sequence[0]} />
);
var statusDisplays = parentInstance.getStatusDisplays();
var lastInternalStates = getInteralStateByUserName(statusDisplays);
verifyStatuses(statusDisplays, sequence[0]);
Expand Down Expand Up @@ -243,8 +244,9 @@ describe('ReactMultiChildReconcile', function() {
}
};

var parentInstance =
ReactTestUtils.renderIntoDocument(FriendsStatusDisplay(props));
var parentInstance = ReactTestUtils.renderIntoDocument(
<FriendsStatusDisplay {...props} />
);
var statusDisplays = parentInstance.getStatusDisplays();
var startingInternalState = statusDisplays.jcw.getInternalState();

Expand Down
2 changes: 1 addition & 1 deletion src/core/__tests__/ReactMultiChildText-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var testAllPermutations = function(testCases) {

var renderChildren = function(children) {
return ReactTestUtils.renderIntoDocument(
React.DOM.div({children: children})
<div>{children}</div>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/__tests__/ReactTextComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('ReactTextComponent', function() {
it('should escape the rootID', function(){
var ThisThingShouldBeEscaped = '">>> LULZ <<<"';
var ThisThingWasBeEscaped = '&quot;&gt;&gt;&gt; LULZ &lt;&lt;&lt;&quot;';
var thing = React.DOM.div(null, React.DOM.span({key:ThisThingShouldBeEscaped}, ["LULZ"]));
var thing = <div><span key={ThisThingShouldBeEscaped}>LULZ</span></div>;
var html = React.renderComponentToString(thing);
expect(html).not.toContain(ThisThingShouldBeEscaped);
expect(html).toContain(ThisThingWasBeEscaped);
Expand Down
6 changes: 5 additions & 1 deletion src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ var ReactTestUtils = {

var ConvenienceConstructor = React.createClass({
render: function() {
return ReactDOM[mockTagName](null, this.props.children);
return React.createElement(
ReactDOM[mockTagName], // TODO: Replace this with just a string
null,
this.props.children
);
}
});

Expand Down

0 comments on commit 989e6b6

Please sign in to comment.