Allow iterables to pass node prop type check
Antonio Ruberto committed
Jun 24, 2015 at 09:43 UTC
8e72956430f4ff59de090b0b82a7ab6d13176046
2 files changed
+36
-4
src/isomorphic/classic/types/ReactPropTypes.js
+20
-4
@@ -16,6 +16,7 @@ var ReactFragment = require('ReactFragment');
16
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
17
18
var emptyFunction = require('emptyFunction');
19
+var getIteratorFn = require('getIteratorFn');
20
21
/**
22
* Collection of methods that allow declaration and validation of props that are
@@ -345,12 +346,27 @@ function isNode(propValue) {
346
if (propValue === null || ReactElement.isValidElement(propValue)) {
347
return true;
348
}
348
- propValue = ReactFragment.extractIfFragment(propValue);
349
- for (var k in propValue) {
350
- if (!isNode(propValue[k])) {
351
- return false;
349
+
350
+ var iteratorFn = getIteratorFn(propValue);
351
+ if (iteratorFn) {
352
+ if (iteratorFn !== propValue.entries) {
353
+ var iterator = iteratorFn.call(propValue);
354
+ var step;
355
+ while (!(step = iterator.next()).done) {
356
+ if (!isNode(step.value)) {
357
+ return false;
358
+ }
359
+ }
360
+ }
361
+ } else {
362
+ propValue = ReactFragment.extractIfFragment(propValue);
363
+ for (var k in propValue) {
364
+ if (!isNode(propValue[k])) {
365
+ return false;
366
+ }
367
}
368
}
369
+
370
return true;
371
default:
372
return false;
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+16
@@ -405,6 +405,22 @@ describe('ReactPropTypes', function() {
405
});
406
});
407
408
+ it('should not warn for iterables', function() {
409
+ var iterable = {
410
+ '@@iterator': function() {
411
+ var i = 0;
412
+ return {
413
+ next: function() {
414
+ var done = ++i > 2;
415
+ return {value: done ? undefined : <MyComponent />, done: done};
416
+ },
417
+ };
418
+ },
419
+ };
420
+
421
+ typeCheckPass(PropTypes.node, iterable);
422
+ });
423
+
424
it('should not warn for null/undefined if not required', function() {
425
typeCheckPass(PropTypes.node, null);
426
typeCheckPass(PropTypes.node, undefined);