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 May 30, 2020
1 parent 8c8103c commit 66b5cbb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 48 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
76 changes: 40 additions & 36 deletions src/demo/pages/DemoContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,49 +422,53 @@ class DemoContainer extends React.Component {
name="Horizontal Form Layout"
component={(
<FormLayout fieldLayout="horizontal">
<TextField
id="formLayoutHorizontalFirstName"
changeHandler={logger}
label="First Name"
/>
<TextField
id="formLayoutHorizontalLastName"
changeHandler={logger}
label="Last Name"
/>
<>
<TextField
id="formLayoutHorizontalFirstName"
changeHandler={logger}
label="First Name"
/>
<TextField
id="formLayoutHorizontalLastName"
changeHandler={logger}
label="Last Name"
/>
</>
<TextField
id="formLayoutHorizontalEmail"
changeHandler={logger}
label="Email address"
type="email"
helperText="Optional"
/>
<TextField
id="formLayoutHorizontalAddress1"
changeHandler={logger}
label="Address"
placeholder="Address line 1"
/>
<TextField
id="formLayoutHorizontalAddress2"
changeHandler={logger}
isLabelVisible={false}
label="Address 2"
placeholder="Address line 2"
/>
<TextField
id="formLayoutHorizontalZip"
changeHandler={logger}
helperText="ZIP should be 5 to 6 digits long code."
label="ZIP"
inputSize={6}
validationState="invalid"
/>
<TextField
id="formLayoutHorizontalCountry"
changeHandler={logger}
label="Country"
/>
<>
<TextField
id="formLayoutHorizontalAddress1"
changeHandler={logger}
label="Address"
placeholder="Address line 1"
/>
<TextField
id="formLayoutHorizontalAddress2"
changeHandler={logger}
isLabelVisible={false}
label="Address 2"
placeholder="Address line 2"
/>
<TextField
id="formLayoutHorizontalZip"
changeHandler={logger}
helperText="ZIP should be 5 to 6 digits long code."
label="ZIP"
inputSize={6}
validationState="invalid"
/>
<TextField
id="formLayoutHorizontalCountry"
changeHandler={logger}
label="Country"
/>
</>
<SelectField
id="formLayoutHorizontalFruit"
changeHandler={logger}
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 @@ -29,7 +30,7 @@ const FormLayout = (props) => {
fieldLayoutClass(fieldLayout),
].join(' ')}
>
{React.Children.map(children, (child) => {
{flattenChildren(children).map((child) => {
if (!React.isValidElement(child)) {
return null;
}
Expand Down

0 comments on commit 66b5cbb

Please sign in to comment.