Merge pull request #6132 from chicoxyzzy/use_object_is_in_oneof_validate_method

Using Object.is implementation when compare values inside React.PropTypes.oneOf
This commit is contained in:
Paul O’Shannessy
2016-02-29 09:59:09 -08:00
2 changed files with 20 additions and 2 deletions
+18 -1
View File
@@ -85,6 +85,23 @@ var ReactPropTypes = {
shape: createShapeTypeChecker,
};
/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
/*eslint-disable no-self-compare*/
function is(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
}
/*eslint-enable no-self-compare*/
function createChainableTypeChecker(validate) {
function checkType(
isRequired,
@@ -218,7 +235,7 @@ function createEnumTypeChecker(expectedValues) {
function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
for (var i = 0; i < expectedValues.length; i++) {
if (propValue === expectedValues[i]) {
if (is(propValue, expectedValues[i])) {
return null;
}
}
@@ -576,7 +576,7 @@ describe('ReactPropTypes', function() {
);
});
it('should warn for invalid strings', function() {
it('should warn for invalid values', function() {
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
true,
@@ -606,6 +606,7 @@ describe('ReactPropTypes', function() {
it('should not warn for valid values', function() {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
typeCheckPass(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
});
it('should be implicitly optional and not warn without values', function() {