Skip to content

Commit c4bd4c1

Browse files
committed
Simplified the logic for determining when to link children.
1 parent a088497 commit c4bd4c1

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

dist/linkClass.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ linkClass = function (element, styles, userConfiguration) {
3030
if (styles === undefined) styles = {};
3131

3232
var appendClassName = undefined,
33-
childrenCount = undefined,
3433
clonedElement = undefined,
3534
configuration = undefined,
3635
newChildren = undefined,
@@ -67,26 +66,29 @@ linkClass = function (element, styles, userConfiguration) {
6766
appendClassName = appendClassName.join(' ');
6867
}
6968

70-
if (_utils2['default'].isArray(element.props.children) || _react2['default'].isValidElement(element.props.children)) {
71-
childrenCount = _react2['default'].Children.count(element.props.children);
69+
// element.props.children can be one of the following:
70+
// 'text'
71+
// ['text']
72+
// [ReactElement, 'text']
73+
// ReactElement
7274

73-
// console.log('childrenCount', childrenCount, 'element.props.children', element.props.children);
75+
// console.log(`element.props.children`, element.props.children, `React.Children.count(element.props.children)`, React.Children.count(element.props.children));
7476

75-
if (childrenCount > 1 || _utils2['default'].isArray(element.props.children)) {
76-
newChildren = _react2['default'].Children.map(element.props.children, function (node) {
77-
if (_react2['default'].isValidElement(node)) {
78-
return linkClass(node, styles, configuration);
79-
} else {
80-
return node;
81-
}
82-
});
83-
// https://github.com/facebook/react/issues/4723#issuecomment-135555277
84-
// Forcing children into an array produces the following error:
85-
// Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
86-
// newChildren = _.values(newChildren);
87-
} else if (childrenCount === 1) {
88-
newChildren = linkClass(_react2['default'].Children.only(element.props.children), styles, configuration);
77+
if (_react2['default'].isValidElement(element.props.children)) {
78+
newChildren = linkClass(_react2['default'].Children.only(element.props.children), styles, configuration);
79+
} else if (_utils2['default'].isArray(element.props.children)) {
80+
newChildren = _react2['default'].Children.map(element.props.children, function (node) {
81+
if (_react2['default'].isValidElement(node)) {
82+
return linkClass(node, styles, configuration);
83+
} else {
84+
return node;
8985
}
86+
});
87+
88+
// https://github.com/facebook/react/issues/4723#issuecomment-135555277
89+
// Forcing children into an array produces the following error:
90+
// Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
91+
// newChildren = _.values(newChildren);
9092
}
9193

9294
if (appendClassName) {

src/linkClass.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ let linkClass;
1212
*/
1313
linkClass = (element, styles = {}, userConfiguration) => {
1414
let appendClassName,
15-
childrenCount,
1615
clonedElement,
1716
configuration,
1817
newChildren,
@@ -49,28 +48,30 @@ linkClass = (element, styles = {}, userConfiguration) => {
4948
appendClassName = appendClassName.join(' ');
5049
}
5150

52-
if (_.isArray(element.props.children) || React.isValidElement(element.props.children)) {
53-
childrenCount = React.Children.count(element.props.children);
51+
// element.props.children can be one of the following:
52+
// 'text'
53+
// ['text']
54+
// [ReactElement, 'text']
55+
// ReactElement
5456

55-
// console.log('childrenCount', childrenCount, 'element.props.children', element.props.children);
57+
// console.log(`element.props.children`, element.props.children, `React.Children.count(element.props.children)`, React.Children.count(element.props.children));
5658

57-
if (childrenCount > 1 || _.isArray(element.props.children)) {
58-
newChildren = React.Children.map(element.props.children, (node) => {
59-
if (React.isValidElement(node)) {
60-
return linkClass(node, styles, configuration);
61-
} else {
62-
return node;
63-
}
64-
});
65-
// https://github.com/facebook/react/issues/4723#issuecomment-135555277
66-
// Forcing children into an array produces the following error:
67-
// Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
68-
// newChildren = _.values(newChildren);
69-
} else if (childrenCount === 1) {
70-
newChildren = linkClass(React.Children.only(element.props.children), styles, configuration);
71-
}
72-
}
59+
if (React.isValidElement(element.props.children)) {
60+
newChildren = linkClass(React.Children.only(element.props.children), styles, configuration);
61+
} else if (_.isArray(element.props.children)) {
62+
newChildren = React.Children.map(element.props.children, (node) => {
63+
if (React.isValidElement(node)) {
64+
return linkClass(node, styles, configuration);
65+
} else {
66+
return node;
67+
}
68+
});
7369

70+
// https://github.com/facebook/react/issues/4723#issuecomment-135555277
71+
// Forcing children into an array produces the following error:
72+
// Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
73+
// newChildren = _.values(newChildren);
74+
}
7475

7576
if (appendClassName) {
7677
if (element.props.className) {

0 commit comments

Comments
 (0)