@@ -11,7 +11,26 @@ import React from 'react';
11
11
import { CARBON_SIDENAV_ITEMS } from './_utils' ;
12
12
import { usePrefix } from '../../internal/usePrefix' ;
13
13
14
- const SideNavItems = ( {
14
+ interface SideNavItemsProps {
15
+ /**
16
+ * Provide a single icon as the child to `SideNavIcon` to render in the
17
+ * container
18
+ */
19
+ children : React . ReactNode ;
20
+
21
+ /**
22
+ * Provide an optional class to be applied to the containing node
23
+ */
24
+ className ?: string ;
25
+
26
+ /**
27
+ * Property to indicate if the side nav container is open (or not). Use to
28
+ * keep local state and styling in step with the SideNav expansion state.
29
+ */
30
+ isSideNavExpanded ?: boolean ;
31
+ }
32
+
33
+ const SideNavItems : React . FC < SideNavItemsProps > = ( {
15
34
className : customClassName ,
16
35
children,
17
36
isSideNavExpanded,
@@ -21,13 +40,16 @@ const SideNavItems = ({
21
40
const childrenWithExpandedState = React . Children . map ( children , ( child ) => {
22
41
if ( React . isValidElement ( child ) ) {
23
42
// avoid spreading `isSideNavExpanded` to non-Carbon UI Shell children
24
- return React . cloneElement ( child , {
25
- ...( CARBON_SIDENAV_ITEMS . includes ( child . type ?. displayName )
26
- ? {
27
- isSideNavExpanded,
28
- }
29
- : { } ) ,
30
- } ) ;
43
+ const childType = child . type as React . ComponentType ;
44
+ if ( childType && childType . displayName ) {
45
+ return React . cloneElement ( child , {
46
+ ...( CARBON_SIDENAV_ITEMS . includes ( childType . displayName )
47
+ ? {
48
+ isSideNavExpanded,
49
+ }
50
+ : { } ) ,
51
+ } ) ;
52
+ }
31
53
}
32
54
} ) ;
33
55
return < ul className = { className } > { childrenWithExpandedState } </ ul > ;
0 commit comments