1- import React , { CSSProperties , forwardRef , ReactNode , useContext , useRef , useState } from 'react'
1+ import React , {
2+ CSSProperties ,
3+ forwardRef ,
4+ ReactNode ,
5+ useContext ,
6+ useEffect ,
7+ useRef ,
8+ useState ,
9+ } from 'react'
210import PropTypes from 'prop-types'
311import classNames from 'classnames'
412import { Transition } from 'react-transition-group'
@@ -10,14 +18,10 @@ export interface CNavGroupProps {
1018 * A string of all className you want applied to the component. [docs]
1119 */
1220 className ?: string
13- /**
14- * Set component's icon. [docs]
15- */
16- icon ?: string | ReactNode
1721 /**
1822 * Set group toggler label. [docs]
1923 */
20- toggler ?: string
24+ toggler ?: string | ReactNode
2125 /**
2226 * Show nav group items. [docs]
2327 */
@@ -29,10 +33,30 @@ export interface CNavGroupProps {
2933}
3034
3135export const CNavGroup = forwardRef < HTMLLIElement , CNavGroupProps > (
32- ( { children, toggler , className, icon , idx , visible, ...rest } , ref ) => {
36+ ( { children, className, idx , toggler , visible, ...rest } , ref ) => {
3337 const [ height , setHeight ] = useState < number | string > ( )
3438 const navItemsRef = useRef < HTMLUListElement > ( null )
3539
40+ const { visibleGroup, setVisibleGroup } = useContext ( CNavContext )
41+
42+ const [ _visible , setVisible ] = useState (
43+ Boolean (
44+ visible || ( idx && visibleGroup && visibleGroup . toString ( ) . startsWith ( idx . toString ( ) ) ) ,
45+ ) ,
46+ )
47+
48+ useEffect ( ( ) => {
49+ setVisible ( Boolean ( idx && visibleGroup && visibleGroup . toString ( ) . startsWith ( idx . toString ( ) ) ) )
50+ } , [ visibleGroup ] )
51+
52+ const handleTogglerOnCLick = ( event : React . MouseEvent < HTMLElement > ) => {
53+ event . preventDefault ( )
54+ setVisibleGroup (
55+ _visible ? ( idx ?. toString ( ) . includes ( '.' ) ? idx . slice ( 0 , idx . lastIndexOf ( '.' ) ) : '' ) : idx ,
56+ )
57+ setVisible ( ! _visible )
58+ }
59+
3660 const style : CSSProperties = {
3761 height : 0 ,
3862 }
@@ -67,31 +91,12 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
6791 exited : { height : height } ,
6892 }
6993
70- const { visibleGroup, setVisibleGroup } = useContext ( CNavContext )
71-
72- const _visible = Boolean (
73- visible || ( idx && visibleGroup && visibleGroup . toString ( ) . startsWith ( idx . toString ( ) ) ) ,
74- )
75-
7694 const _className = classNames ( 'nav-group' , { show : _visible } , className )
7795
7896 return (
7997 < li className = { _className } { ...rest } ref = { ref } >
8098 { toggler && (
81- < a
82- className = "nav-link nav-group-toggle"
83- onClick = { ( event ) => {
84- event . preventDefault ( )
85- setVisibleGroup (
86- _visible
87- ? idx ?. toString ( ) . includes ( '.' )
88- ? idx . slice ( 0 , idx . lastIndexOf ( '.' ) )
89- : ''
90- : idx ,
91- )
92- } }
93- >
94- { icon && typeof icon === 'string' ? < i className = { `nav-icon ${ icon } ` } > </ i > : icon }
99+ < a className = "nav-link nav-group-toggle" onClick = { ( event ) => handleTogglerOnCLick ( event ) } >
95100 { toggler }
96101 </ a >
97102 ) }
@@ -127,9 +132,8 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
127132CNavGroup . propTypes = {
128133 children : PropTypes . node ,
129134 className : PropTypes . string ,
130- icon : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . node ] ) ,
131135 idx : PropTypes . string ,
132- toggler : PropTypes . string ,
136+ toggler : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . node ] ) ,
133137 visible : PropTypes . bool ,
134138}
135139
0 commit comments