Skip to content

Commit

Permalink
Add support for React Fragments to CardList and FormLayout (#44)
Browse files Browse the repository at this point in the history
Fragments are not traversed with `React.children.map()` [1][2] so we are using the `react-keyed-flatten-children` bundle to flatten the children to a one-dimensional array. This is necessary to be able to pass props to children of these layouts.

[1] https://reactjs.org/docs/react-api.html#reactchildrenmap
[2] reactjs/rfcs#61 (comment)
  • Loading branch information
adamkudrna committed Jun 5, 2020
1 parent 9d1c196 commit ea8ca8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
23 changes: 14 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"normalize.css": "^8.0.1",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1"
"react-dom": "^16.13.1",
"react-keyed-flatten-children": "^1.2.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/layout/CardList/CardList.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flattenChildren from 'react-keyed-flatten-children';
import PropTypes from 'prop-types';
import React from 'react';
import styles from './CardList.scss';
Expand All @@ -19,7 +20,7 @@ const CardList = (props) => {
className={styles.root}
{...other}
>
{React.Children.map(children, (child) => {
{flattenChildren(children).map((child) => {
if (!React.isValidElement(child)) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/layout/FormLayout/FormLayout.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flattenChildren from 'react-keyed-flatten-children';
import PropTypes from 'prop-types';
import React from 'react';
import styles from './FormLayout.scss';
Expand Down Expand Up @@ -66,7 +67,7 @@ const FormLayout = (props) => {
].join(' ')}
style={inlineStyle(customLabelWidth, labelAutoWidthFallback)}
>
{React.Children.map(children, (child) => {
{flattenChildren(children).map((child) => {
if (!React.isValidElement(child)) {
return null;
}
Expand Down

0 comments on commit ea8ca8b

Please sign in to comment.