@@ -12,52 +12,6 @@ import Slugger from 'github-slugger';
12
12
import { normalizeString } from './helpers' ;
13
13
import './less/accordion-menu.less' ;
14
14
15
- const RenderAccordionItems = ( { items } ) => {
16
- const [ activeItems , setActiveItems ] = useState ( { } ) ;
17
-
18
- const handleClick = ( index , hasSubItems ) => {
19
- if ( hasSubItems ) {
20
- setActiveItems ( ( prevActiveItems ) => ( {
21
- ...prevActiveItems ,
22
- [ index ] : ! prevActiveItems [ index ] ,
23
- } ) ) ;
24
- }
25
- } ;
26
-
27
- return (
28
- < Accordion fluid styled >
29
- { items . map ( ( item , index ) => {
30
- const { title, override_toc, plaintext, items : subItems } = item ;
31
- const slug = override_toc
32
- ? Slugger . slug ( normalizeString ( plaintext ) )
33
- : Slugger . slug ( normalizeString ( title ) ) ;
34
-
35
- const isActive = ! ! activeItems [ index ] ;
36
- const hasSubItems = subItems && subItems . length > 0 ;
37
-
38
- return (
39
- < React . Fragment key = { index } >
40
- < Accordion . Title
41
- active = { isActive }
42
- onClick = { ( ) => handleClick ( index , hasSubItems ) }
43
- >
44
- { subItems && subItems . length > 0 && (
45
- < Icon name = { isActive ? 'angle up' : 'angle right' } />
46
- ) }
47
- < AnchorLink href = { `#${ slug } ` } > { title } </ AnchorLink >
48
- </ Accordion . Title >
49
- < Accordion . Content active = { isActive } >
50
- { subItems && subItems . length > 0 && (
51
- < RenderAccordionItems items = { subItems } />
52
- ) }
53
- </ Accordion . Content >
54
- </ React . Fragment >
55
- ) ;
56
- } ) }
57
- </ Accordion >
58
- ) ;
59
- } ;
60
-
61
15
/**
62
16
* View toc block class.
63
17
* @class View
@@ -66,6 +20,7 @@ const RenderAccordionItems = ({ items }) => {
66
20
const View = ( { data, tocEntries } ) => {
67
21
const tocRef = useRef ( ) ; // Ref for the ToC component
68
22
const spacerRef = useRef ( ) ; // Ref for the spacer div
23
+ const [ activeItems , setActiveItems ] = useState ( { } ) ;
69
24
//get relative offsetTop of an element
70
25
function getOffsetTop ( elem ) {
71
26
let offsetTop = 0 ;
@@ -130,6 +85,53 @@ const View = ({ data, tocEntries }) => {
130
85
}
131
86
} , [ data . sticky ] ) ;
132
87
88
+ const RenderAccordionItems = ( { item, level } ) => {
89
+ const handleClick = ( id , hasSubItems ) => {
90
+ if ( hasSubItems ) {
91
+ setActiveItems ( ( prevActiveItems ) => ( {
92
+ ...prevActiveItems ,
93
+ [ id ] : ! prevActiveItems [ id ] ,
94
+ } ) ) ;
95
+ }
96
+ } ;
97
+
98
+ const { title, override_toc, plaintext, items : subItems , id } = item ;
99
+ const slug = override_toc
100
+ ? Slugger . slug ( normalizeString ( plaintext ) )
101
+ : Slugger . slug ( normalizeString ( title ) ) ;
102
+
103
+ const isActive = ! ! activeItems [ id ] ;
104
+ const hasSubItems = subItems && subItems . length > 0 ;
105
+
106
+ return (
107
+ < li key = { id } >
108
+ { hasSubItems ? (
109
+ < Accordion fluid styled >
110
+ < Accordion . Title
111
+ active = { isActive }
112
+ onClick = { ( ) => handleClick ( id , hasSubItems ) }
113
+ >
114
+ { subItems && subItems . length > 0 && (
115
+ < Icon name = { isActive ? 'angle up' : 'angle right' } />
116
+ ) }
117
+
118
+ < AnchorLink href = { `#${ slug } ` } > { title } </ AnchorLink >
119
+ </ Accordion . Title >
120
+ < Accordion . Content active = { isActive } >
121
+ < ul className = "accordion-list" >
122
+ { subItems . map ( ( child ) =>
123
+ RenderAccordionItems ( { item : child , level : level + 1 } ) ,
124
+ ) }
125
+ </ ul >
126
+ </ Accordion . Content >
127
+ </ Accordion >
128
+ ) : (
129
+ < AnchorLink href = { `#${ slug } ` } > { title } </ AnchorLink >
130
+ ) }
131
+ </ li >
132
+ ) ;
133
+ } ;
134
+
133
135
return (
134
136
< >
135
137
{ data . title && ! data . hide_title && (
@@ -144,7 +146,9 @@ const View = ({ data, tocEntries }) => {
144
146
) }
145
147
< div ref = { spacerRef } /> { /* Spacer div */ }
146
148
< div ref = { tocRef } className = "accordionMenu" >
147
- < RenderAccordionItems items = { tocEntries } data = { data } />
149
+ < ul style = { { listStyle : 'none' } } >
150
+ { tocEntries . map ( ( item ) => RenderAccordionItems ( { item } ) ) }
151
+ </ ul >
148
152
</ div >
149
153
</ >
150
154
) ;
0 commit comments