Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 44461dc

Browse files
author
Franco Correa
authored
Ensure to-be-cloned element is not null (#1554)
1 parent 0544a3b commit 44461dc

File tree

7 files changed

+34
-9
lines changed

7 files changed

+34
-9
lines changed

core/components/_helpers/action-group-creator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const getActionGroup = (actions = [], actionOverrides = {}) => {
1010
<ButtonGroup compressed>
1111
{actions.map((action, index) => {
1212
/* add key to each element of array */
13+
if (!action) return null
1314
return React.cloneElement(action, { key: index, ...actionOverrides })
1415
})}
1516
</ButtonGroup>

core/components/_helpers/layout-margin-reset.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { css } from '@auth0/cosmos/styled'
44
export const LAYOUT_CHILDREN_CLASS_NAME = 'cosmos-layout-child'
55

66
export const applyLayoutChildClass = children =>
7-
React.Children.map(children, child =>
8-
React.cloneElement(child, { className: LAYOUT_CHILDREN_CLASS_NAME })
9-
)
7+
React.Children.map(children, child => {
8+
if (!child) return null
9+
return React.cloneElement(child, { className: LAYOUT_CHILDREN_CLASS_NAME })
10+
})
1011

1112
/**
1213
* Applies the margin reset depending on the prop value that comes

core/components/atoms/checkbox/checkbox.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Checkbox.Element = styled.div`
125125
Checkbox.Group = props => (
126126
<Checkbox.Element {...props} {...Automation('checkbox.group')}>
127127
{React.Children.map(props.children, child => {
128+
if (!child) return null
128129
return React.cloneElement(child, {
129130
name: props.name,
130131
defaultChecked: props.selected.indexOf(child.props.value) > -1,

core/components/atoms/radio/radio.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const justifyContent = {
1515

1616
const Radio = props => (
1717
<Radio.Element {...props} {...Automation('radio')}>
18-
{React.Children.map(props.children, child =>
19-
React.cloneElement(child, {
18+
{React.Children.map(props.children, child => {
19+
if (!child) return null
20+
return React.cloneElement(child, {
2021
name: props.name,
2122
checked: props.selected === child.props.value,
2223
readOnly: props.readOnly || child.props.readOnly
2324
})
24-
)}
25+
})}
2526
</Radio.Element>
2627
)
2728

core/components/atoms/tooltip/action.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ActionTooltip extends React.Component {
7373
processAction() {
7474
const { children: button, resetDelay } = this.props
7575
const content = this.preprocessContent()
76+
if (!button) return null
7677

7778
const newHandler = event => {
7879
this.resetState()

core/components/molecules/form/field/field.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ const ariaDescribedBy = (helperTextId, errorTextId) => {
5656
}
5757

5858
const applyAriaToFieldChild = (children, inputId, helperTextId, errorTextId) =>
59-
React.Children.map(children, child =>
60-
React.cloneElement(child, {
59+
React.Children.map(children, child => {
60+
if (!child) return null
61+
return React.cloneElement(child, {
6162
id: inputId,
6263
...ariaDescribedBy(helperTextId, errorTextId)
6364
})
64-
)
65+
})
6566

6667
const getIdFromChild = child => child.props.id
6768

core/components/molecules/form/field/stories/field.story.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,22 @@ storiesOf('Form', module).add('Form full width', () => (
299299
</RowLayout>
300300
</Example>
301301
))
302+
303+
storiesOf('Form', module).add('Form Fields with null children', () => (
304+
<Example title="Form Fields with null children">
305+
<RowLayout gutter="spacious">
306+
<Form>
307+
<Form.Field label="Something">
308+
<TextInput
309+
type="text"
310+
placeholder="Enter something"
311+
actions={[{ icon: 'copy', handler: () => {}, label: 'Copy to clipboard' }]}
312+
/>
313+
{null}
314+
</Form.Field>
315+
316+
<Form.Actions primaryAction={{ label: 'Save Changes', handler: () => {} }} />
317+
</Form>
318+
</RowLayout>
319+
</Example>
320+
))

0 commit comments

Comments
 (0)