Using Object.is implementation when compare values inside React.PropTypes.oneOf
chico committed
Feb 27, 2016 at 01:13 UTC
03925f4f0631d839b29870358f5feddbfd458c31
2 files changed
+20
-2
src/isomorphic/classic/types/ReactPropTypes.js
+18
-1
@@ -85,6 +85,23 @@ var ReactPropTypes = {
85
shape: createShapeTypeChecker,
86
};
87
88
+/**
89
+ * inlined Object.is polyfill to avoid requiring consumers ship their own
90
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
91
+ */
92
+/*eslint-disable no-self-compare*/
93
+function is(x, y) {
94
+ // SameValue algorithm
95
+ if (x === y) { // Steps 1-5, 7-10
96
+ // Steps 6.b-6.e: +0 != -0
97
+ return x !== 0 || 1 / x === 1 / y;
98
+ } else {
99
+ // Step 6.a: NaN == NaN
100
+ return x !== x && y !== y;
101
+ }
102
+}
103
+/*eslint-enable no-self-compare*/
104
+
105
function createChainableTypeChecker(validate) {
106
function checkType(
107
isRequired,
@@ -218,7 +235,7 @@ function createEnumTypeChecker(expectedValues) {
235
function validate(props, propName, componentName, location, propFullName) {
236
var propValue = props[propName];
237
for (var i = 0; i < expectedValues.length; i++) {
221
- if (propValue === expectedValues[i]) {
238
+ if (is(propValue, expectedValues[i])) {
239
return null;
240
}
241
}
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+2
-1
@@ -576,7 +576,7 @@ describe('ReactPropTypes', function() {
576
);
577
});
578
579
- it('should warn for invalid strings', function() {
579
+ it('should warn for invalid values', function() {
580
typeCheckFail(
581
PropTypes.oneOf(['red', 'blue']),
582
true,
@@ -606,6 +606,7 @@ describe('ReactPropTypes', function() {
606
it('should not warn for valid values', function() {
607
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
608
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
609
+ typeCheckPass(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
610
});
611
612
it('should be implicitly optional and not warn without values', function() {