Skip to content

Commit e8d6029

Browse files
committed
Simplifies child processing.
1 parent 72cc0ab commit e8d6029

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed

commonjs/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function reactTreeWalker(element, visitor, context) {
8181
if (child == null) {
8282
// If no children then we can't traverse. We've reached the leaf.
8383
resolve();
84-
} else if (isChildren) {
84+
} else if (isChildren || _react.Children.count(child)) {
8585
// If its a react Children collection we need to breadth-first
8686
// traverse each of them.
8787
var mapper = function mapper(aChild) {
@@ -120,10 +120,10 @@ function reactTreeWalker(element, visitor, context) {
120120
// Is this element a Component?
121121
if (typeof element.type === 'function') {
122122
var Component = element.type;
123-
var props = Object.assign({}, Component.defaultProps, element.props
123+
var props = Object.assign({}, Component.defaultProps, element.props);
124124

125125
// Is this a class component? (http://bit.ly/2j9Ifk3)
126-
);var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
126+
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
127127

128128
if (isReactClassComponent) {
129129
// React class component

src/__tests__/index.test.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* eslint-disable class-methods-use-this */
66

77
import React, { Component } from 'react'
8+
import PropTypes from 'prop-types'
89
import reactTreeWalker from '../index'
910

1011
const Bob = ({ children }) => <div>{children}</div>
@@ -30,15 +31,13 @@ describe('reactTreeWalker', () => {
3031

3132
const resolveLater = result =>
3233
new Promise(resolve =>
33-
setTimeout(
34-
() => {
35-
resolve(result)
36-
},
37-
10,
38-
))
39-
40-
const createTree = async => (
41-
<div>
34+
setTimeout(() => {
35+
resolve(result)
36+
}, 10),
37+
)
38+
39+
const createTree = async =>
40+
(<div>
4241
<h1>Hello World!</h1>
4342
<Foo something={async ? () => resolveLater(1) : 1} />
4443
<Foo something={async ? () => resolveLater(2) : 2}>
@@ -53,8 +52,7 @@ describe('reactTreeWalker', () => {
5352
</div>
5453
</Foo>
5554
<Foo something={async ? () => resolveLater(3) : 3} />
56-
</div>
57-
)
55+
</div>)
5856

5957
it('simple sync visitor', () => {
6058
const tree = createTree(false)
@@ -95,7 +93,7 @@ describe('reactTreeWalker', () => {
9593
let actual = {}
9694

9795
class Baz extends Component {
98-
state: { foo: string };
96+
state: { foo: string }
9997

10098
constructor(props) {
10199
super(props)
@@ -139,7 +137,7 @@ describe('reactTreeWalker', () => {
139137

140138
it('getChildContext', () => {
141139
class Baz extends Component {
142-
props: { children?: any };
140+
props: { children?: any }
143141
getChildContext() {
144142
return { foo: 'bar' }
145143
}
@@ -153,7 +151,7 @@ describe('reactTreeWalker', () => {
153151
actual = context
154152
return <div>qux</div>
155153
}
156-
Qux.contextTypes = { foo: React.PropTypes.string.isRequired }
154+
Qux.contextTypes = { foo: PropTypes.string.isRequired }
157155

158156
const tree = <Baz><Qux /></Baz>
159157
return reactTreeWalker(tree, () => true).then(() => {

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ const pMapSeries = (iterable, iterator) => {
4545
return pReduce(iterable, (a, b, i) =>
4646
Promise.resolve(iterator(b, i)).then((val) => {
4747
ret.push(val)
48-
})).then(() => ret)
48+
}),
49+
).then(() => ret)
4950
}
5051

5152
export const isPromise = x => x != null && typeof x.then === 'function'
@@ -55,7 +56,7 @@ export const isPromise = x => x != null && typeof x.then === 'function'
5556
// or recurse into its child elements
5657
export default function reactTreeWalker(element, visitor, context, options = defaultOptions) {
5758
return new Promise((resolve) => {
58-
const doVisit = (getChildren, visitorResult, childContext, isChildren) => {
59+
const doVisit = (getChildren, visitorResult, childContext) => {
5960
const doTraverse = (shouldContinue) => {
6061
if (!shouldContinue) {
6162
// We recieved a false, which indicates a desire to stop traversal.
@@ -68,7 +69,7 @@ export default function reactTreeWalker(element, visitor, context, options = def
6869
if (child == null) {
6970
// If no children then we can't traverse. We've reached the leaf.
7071
resolve()
71-
} else if (isChildren || Children.count(child)) {
72+
} else if (Children.count(child)) {
7273
// If its a react Children collection we need to breadth-first
7374
// traverse each of them.
7475
const mapper = aChild =>
@@ -109,7 +110,8 @@ export default function reactTreeWalker(element, visitor, context, options = def
109110
const props = Object.assign({}, Component.defaultProps, element.props)
110111

111112
// Is this a class component? (http://bit.ly/2j9Ifk3)
112-
const isReactClassComponent = Component.prototype &&
113+
const isReactClassComponent =
114+
Component.prototype &&
113115
(Component.prototype.isReactComponent || Component.prototype.isPureReactComponent)
114116

115117
if (isReactClassComponent) {
@@ -164,10 +166,9 @@ export default function reactTreeWalker(element, visitor, context, options = def
164166
} else {
165167
// This must be a basic element, such as a string or dom node.
166168
doVisit(
167-
() => element.props && element.props.children ? element.props.children : undefined,
169+
() => (element.props && element.props.children ? element.props.children : undefined),
168170
visitor(element, null, context),
169171
context,
170-
true,
171172
)
172173
}
173174
}).catch((err) => {

umd/react-tree-walker.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
exports["ReactTreeWalker"] = factory(require("react"));
88
else
99
root["ReactTreeWalker"] = factory(root["React"]);
10-
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
10+
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
1111
return /******/ (function(modules) { // webpackBootstrap
1212
/******/ // The module cache
1313
/******/ var installedModules = {};
@@ -43,9 +43,6 @@ return /******/ (function(modules) { // webpackBootstrap
4343
/******/ // expose the module cache
4444
/******/ __webpack_require__.c = installedModules;
4545
/******/
46-
/******/ // identity function for calling harmony imports with the correct context
47-
/******/ __webpack_require__.i = function(value) { return value; };
48-
/******/
4946
/******/ // define getter function for harmony exports
5047
/******/ __webpack_require__.d = function(exports, name, getter) {
5148
/******/ if(!__webpack_require__.o(exports, name)) {
@@ -73,17 +70,11 @@ return /******/ (function(modules) { // webpackBootstrap
7370
/******/ __webpack_require__.p = "";
7471
/******/
7572
/******/ // Load entry module and return exports
76-
/******/ return __webpack_require__(__webpack_require__.s = 1);
73+
/******/ return __webpack_require__(__webpack_require__.s = 0);
7774
/******/ })
7875
/************************************************************************/
7976
/******/ ([
8077
/* 0 */
81-
/***/ (function(module, exports) {
82-
83-
module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
84-
85-
/***/ }),
86-
/* 1 */
8778
/***/ (function(module, exports, __webpack_require__) {
8879

8980
"use strict";
@@ -95,7 +86,7 @@ Object.defineProperty(exports, "__esModule", {
9586
exports.isPromise = undefined;
9687
exports.default = reactTreeWalker;
9788

98-
var _react = __webpack_require__(0);
89+
var _react = __webpack_require__(1);
9990

10091
var defaultOptions = {
10192
componentWillUnmount: false
@@ -170,7 +161,7 @@ function reactTreeWalker(element, visitor, context) {
170161
if (child == null) {
171162
// If no children then we can't traverse. We've reached the leaf.
172163
resolve();
173-
} else if (isChildren) {
164+
} else if (isChildren || _react.Children.count(child)) {
174165
// If its a react Children collection we need to breadth-first
175166
// traverse each of them.
176167
var mapper = function mapper(aChild) {
@@ -209,10 +200,10 @@ function reactTreeWalker(element, visitor, context) {
209200
// Is this element a Component?
210201
if (typeof element.type === 'function') {
211202
var Component = element.type;
212-
var props = Object.assign({}, Component.defaultProps, element.props
203+
var props = Object.assign({}, Component.defaultProps, element.props);
213204

214205
// Is this a class component? (http://bit.ly/2j9Ifk3)
215-
);var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
206+
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
216207

217208
if (isReactClassComponent) {
218209
// React class component
@@ -275,6 +266,12 @@ function reactTreeWalker(element, visitor, context) {
275266
});
276267
}
277268

269+
/***/ }),
270+
/* 1 */
271+
/***/ (function(module, exports) {
272+
273+
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
274+
278275
/***/ })
279276
/******/ ]);
280277
});

umd/react-tree-walker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)