Skip to content

Commit

Permalink
fix(alert): crash when return null as child on cta property (#2630)
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-mesnil authored Nov 26, 2024
1 parent dc5e8d0 commit 34f76f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/Alert/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ const AlertComponent = forwardRef<'div', AlertProps>(

// Handle clone actions recursively in case of multiple buttons
const cloneActions = (child: React.ReactElement<AlertProps>): CloneActionsReturns => {
if (child.type === AlertButton) return cloneElement(child, { size })
if (child.type === AlertSecondaryButton) return cloneElement(child, { size })
if (child) {
if (child.type === AlertButton) return cloneElement(child, { size })
if (child.type === AlertSecondaryButton) return cloneElement(child, { size })

if (child.props?.children) {
return cloneElement(child, {
...child.props,
children: Children.map(child.props.children, (nestedChild: React.ReactElement) =>
cloneActions(nestedChild)
),
})
if (child.props?.children) {
return cloneElement(child, {
...child.props,
children: Children.map(child.props.children, (nestedChild: React.ReactElement) =>
cloneActions(nestedChild)
),
})
}
}

return child
Expand Down
19 changes: 19 additions & 0 deletions packages/Alert/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ describe('<Alert>', () => {
expect(container).toHaveTextContent(content)
expect(container.getElementsByTagName('svg')[0]).toHaveAttribute('alt', 'Check')
})

it('should render correctly with CTA', () => {
const { getByTestId, queryByTestId } = render(
<Alert
cta={
<>
{false && <Alert.Button dataTestId="button" />}
<Alert.SecondaryButton dataTestId="secondary-button" />
</>
}
>
<Alert.Title dataTestId="alert-title">title</Alert.Title>
{content}
</Alert>
)

expect(queryByTestId('button')).not.toBeInTheDocument()
expect(getByTestId('secondary-button')).toBeInTheDocument()
})
})

0 comments on commit 34f76f1

Please sign in to comment.