diff --git a/src/isomorphic/classic/types/ReactPropTypes.js b/src/isomorphic/classic/types/ReactPropTypes.js index dfa034a807..99c036496a 100644 --- a/src/isomorphic/classic/types/ReactPropTypes.js +++ b/src/isomorphic/classic/types/ReactPropTypes.js @@ -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; } } diff --git a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js index 185095752e..abfa976b00 100644 --- a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js +++ b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js @@ -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() {