Skip to content

Commit 6473eb6

Browse files
committed
fix(ui-side-nav-bar): fix crash on null/falsy children
Now we allow any valid React element. To test: Add null/undefined/falsy children to the SideNavBar Fixes INSTUI-4496
1 parent 75e1540 commit 6473eb6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/ui-side-nav-bar/src/SideNavBar/__new-tests__/SideNavBar.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ describe('<SideNavBar />', () => {
7777
expect(nav).toHaveTextContent('Minimize SideNavBar')
7878
})
7979

80+
it('should not crash with valid React elements', async () => {
81+
render(
82+
<SideNavBar
83+
label="Main navigation"
84+
toggleLabel={{
85+
expandedLabel: 'Minimize SideNavBar',
86+
minimizedLabel: 'Expand SideNavBar'
87+
}}
88+
>
89+
{false}
90+
<div>test123</div>
91+
<SideNavBarItem
92+
icon={<IconDashboardLine />}
93+
label="Dashboard"
94+
href="#"
95+
/>
96+
</SideNavBar>
97+
)
98+
const navElements = screen.getAllByRole('listitem')
99+
expect(navElements[0]).toHaveTextContent('test123')
100+
expect(navElements[1]).toHaveTextContent('Dashboard')
101+
})
102+
80103
it('should render a single semantic nav element', async () => {
81104
render(
82105
<SideNavBar

packages/ui-side-nav-bar/src/SideNavBar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424
/** @jsx jsx */
25-
import React, { Component, Children, ReactElement } from 'react'
25+
import React, { Component, Children, ReactElement, isValidElement } from 'react'
2626

2727
import { testable } from '@instructure/ui-testable'
2828

@@ -47,7 +47,6 @@ const navMinimized = ({ minimized }: { minimized: boolean }) => ({
4747
category: components
4848
---
4949
**/
50-
5150
@withStyle(generateStyle, generateComponentTheme)
5251
@testable()
5352
class SideNavBar extends Component<SideNavBarProps, SideNavBarState> {
@@ -107,6 +106,7 @@ class SideNavBar extends Component<SideNavBarProps, SideNavBarState> {
107106

108107
renderChildren() {
109108
return Children.map(this.props.children, (child) => {
109+
if (!isValidElement(child)) return null
110110
const navItem = safeCloneElement(child as ReactElement, {
111111
minimized: this.state.minimized
112112
})

0 commit comments

Comments
 (0)