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'
2
10
import PropTypes from 'prop-types'
3
11
import classNames from 'classnames'
4
12
import { Transition } from 'react-transition-group'
@@ -10,14 +18,10 @@ export interface CNavGroupProps {
10
18
* A string of all className you want applied to the component. [docs]
11
19
*/
12
20
className ?: string
13
- /**
14
- * Set component's icon. [docs]
15
- */
16
- icon ?: string | ReactNode
17
21
/**
18
22
* Set group toggler label. [docs]
19
23
*/
20
- toggler ?: string
24
+ toggler ?: string | ReactNode
21
25
/**
22
26
* Show nav group items. [docs]
23
27
*/
@@ -29,10 +33,30 @@ export interface CNavGroupProps {
29
33
}
30
34
31
35
export const CNavGroup = forwardRef < HTMLLIElement , CNavGroupProps > (
32
- ( { children, toggler , className, icon , idx , visible, ...rest } , ref ) => {
36
+ ( { children, className, idx , toggler , visible, ...rest } , ref ) => {
33
37
const [ height , setHeight ] = useState < number | string > ( )
34
38
const navItemsRef = useRef < HTMLUListElement > ( null )
35
39
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
+
36
60
const style : CSSProperties = {
37
61
height : 0 ,
38
62
}
@@ -67,31 +91,12 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
67
91
exited : { height : height } ,
68
92
}
69
93
70
- const { visibleGroup, setVisibleGroup } = useContext ( CNavContext )
71
-
72
- const _visible = Boolean (
73
- visible || ( idx && visibleGroup && visibleGroup . toString ( ) . startsWith ( idx . toString ( ) ) ) ,
74
- )
75
-
76
94
const _className = classNames ( 'nav-group' , { show : _visible } , className )
77
95
78
96
return (
79
97
< li className = { _className } { ...rest } ref = { ref } >
80
98
{ 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 ) } >
95
100
{ toggler }
96
101
</ a >
97
102
) }
@@ -127,9 +132,8 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
127
132
CNavGroup . propTypes = {
128
133
children : PropTypes . node ,
129
134
className : PropTypes . string ,
130
- icon : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . node ] ) ,
131
135
idx : PropTypes . string ,
132
- toggler : PropTypes . string ,
136
+ toggler : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . node ] ) ,
133
137
visible : PropTypes . bool ,
134
138
}
135
139
0 commit comments