Skip to content

Commit 3d26225

Browse files
committed
WIP: refactor accordion variation to inlcude lists
1 parent e6af54b commit 3d26225

File tree

3 files changed

+66
-49
lines changed

3 files changed

+66
-49
lines changed

src/Block/variations/AccordionMenu.jsx

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,6 @@ import Slugger from 'github-slugger';
1212
import { normalizeString } from './helpers';
1313
import './less/accordion-menu.less';
1414

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-
6115
/**
6216
* View toc block class.
6317
* @class View
@@ -66,6 +20,7 @@ const RenderAccordionItems = ({ items }) => {
6620
const View = ({ data, tocEntries }) => {
6721
const tocRef = useRef(); // Ref for the ToC component
6822
const spacerRef = useRef(); // Ref for the spacer div
23+
const [activeItems, setActiveItems] = useState({});
6924
//get relative offsetTop of an element
7025
function getOffsetTop(elem) {
7126
let offsetTop = 0;
@@ -130,6 +85,53 @@ const View = ({ data, tocEntries }) => {
13085
}
13186
}, [data.sticky]);
13287

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+
133135
return (
134136
<>
135137
{data.title && !data.hide_title && (
@@ -144,7 +146,9 @@ const View = ({ data, tocEntries }) => {
144146
)}
145147
<div ref={spacerRef} /> {/* Spacer div */}
146148
<div ref={tocRef} className="accordionMenu">
147-
<RenderAccordionItems items={tocEntries} data={data} />
149+
<ul style={{ listStyle: 'none' }}>
150+
{tocEntries.map((item) => RenderAccordionItems({ item }))}
151+
</ul>
148152
</div>
149153
</>
150154
);

src/Block/variations/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const ToCVariations = [
1919
id: 'accordionMenu',
2020
title: 'Accordion Menu',
2121
view: AccordionMenu,
22+
schemaEnhancer: ({ schema }) => {
23+
schema.fieldsets[0].fields.push('bulleted_list');
24+
schema.properties.bulleted_list = {
25+
title: 'Use bullet list',
26+
type: 'boolean',
27+
};
28+
return schema;
29+
},
2230
},
2331
{
2432
id: 'eea-side-menu',

src/Block/variations/less/accordion-menu.less

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
border: none !important;
1313
background-color: #f9f9f9 !important;
1414

15-
a {
16-
padding-left: 15% !important;
15+
li[role='listitem'] {
16+
padding: 10px 1.5em !important;
17+
font-size: 0.95em;
18+
}
19+
20+
& > a {
21+
padding: 10px 30px !important;
1722
}
1823
}
1924
}

0 commit comments

Comments
 (0)