Move defaultProps resolution to the descriptor factory

Moves the defaultProps resolution to the descriptor factory.
This commit is contained in:
Sebastian Markbage
2014-07-18 22:01:36 -07:00
committed by Paul O’Shannessy
parent 3a7dbe6b73
commit c419cce5c9
3 changed files with 17 additions and 19 deletions
+2 -14
View File
@@ -939,25 +939,13 @@ var ReactCompositeComponentMixin = {
* @private
*/
_processProps: function(newProps) {
var defaultProps = this.constructor.defaultProps;
var props;
if (defaultProps) {
props = merge(newProps);
for (var propName in defaultProps) {
if (typeof props[propName] === 'undefined') {
props[propName] = defaultProps[propName];
}
}
} else {
props = newProps;
}
if (__DEV__) {
var propTypes = this.constructor.propTypes;
if (propTypes) {
this._checkPropTypes(propTypes, props, ReactPropTypeLocations.prop);
this._checkPropTypes(propTypes, newProps, ReactPropTypeLocations.prop);
}
}
return props;
return newProps;
},
/**
+13 -3
View File
@@ -158,6 +158,8 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
}
}
var propName;
// Reserved names are extracted
var props = {};
@@ -168,7 +170,7 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
ref = config.ref === undefined ? null : config.ref;
key = config.key === undefined ? null : '' + config.key;
// Remaining properties are added to a new props object
for (var propName in config) {
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
props[propName] = config[propName];
@@ -176,8 +178,6 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
}
}
// TODO: fill in defaultProps here, after transferPropsTo is gone
// Children can be more than one argument, and those are transferred onto
// the newly allocated props object.
var childrenLength = arguments.length - 2;
@@ -191,6 +191,16 @@ ReactDescriptor.createDescriptor = function(type, config, children) {
props.children = childArray;
}
// Resolve default props
if (type.defaultProps) {
var defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (typeof props[propName] === 'undefined') {
props[propName] = defaultProps[propName];
}
}
}
return new ReactDescriptor(
type,
key,
@@ -164,7 +164,7 @@ describe('ReactPropTransferer', function() {
);
});
it('should not use the default when a prop is transfered', function() {
it('uses the default instead of the transferred prop (regress)', function() {
var Child = React.createClass({
@@ -175,7 +175,7 @@ describe('ReactPropTransferer', function() {
},
render: function() {
expect(this.props.x).toBe(5);
expect(this.props.x).toBe(2);
return <div />;
}