Introduction
In this lab, we will explore how to implement a collapsible accordion menu in React using components and hooks. The purpose of this lab is to provide a hands-on experience in building a functional accordion menu with multiple collapsible content elements. By the end of this lab, you will have a good understanding of how to use React to create dynamic and interactive user interfaces.
Collapsible Accordion
index.htmlandscript.jshave already been provided in the VM. In general, you only need to add code toscript.jsandstyle.css.
To render an accordion menu with multiple collapsible content elements, you can follow these steps:
- Define an
AccordionItemcomponent that renders a<button>and updates the component while notifying its parent via thehandleClickcallback. - Use the
isCollapsedprop inAccordionItemto determine its appearance and set itsclassName. - Define an
Accordioncomponent and use theuseState()hook to initialize the value of thebindIndexstate variable todefaultIndex. - Filter
childrento remove unnecessary nodes, except forAccordionItem, by identifying the function's name. - Use
Array.prototype.map()on the collected nodes to render the individual collapsible elements. - Define
changeItem, which will be executed when clicking anAccordionItem's<button>. changeItemexecutes the passed callback,onItemClick, and updatesbindIndexbased on the clicked element.
Here is the code:
.accordion-item.collapsed {
display: none;
}
.accordion-item.expanded {
display: block;
}
.accordion-button {
display: block;
width: 100%;
}
const AccordionItem = ({ label, isCollapsed, handleClick, children }) => {
return (
<>
<button className="accordion-button"
{label}
</button>
<div
className={`accordion-item ${isCollapsed ? "collapsed" : "expanded"}`}
aria-expanded={isCollapsed}
>
{children}
</div>
</>
);
};
const Accordion = ({ defaultIndex, onItemClick, children }) => {
const [bindIndex, setBindIndex] = React.useState(defaultIndex);
const changeItem = (itemIndex) => {
if (typeof "function") onItemClick(itemIndex);
if (itemIndex !== bindIndex) setBindIndex(itemIndex);
};
const items = children.filter((item) => item.type.name === "AccordionItem");
return (
<>
{items.map(({ props }) => (
<AccordionItem
isCollapsed={bindIndex !== props.index}
label={props.label}
handleClick={() => changeItem(props.index)}
children={props.children}
/>
))}
</>
);
};
ReactDOM.createRoot(document.getElementById("root")).render(
<Accordion defaultIndex="1"
<AccordionItem label="A" index="1">
Lorem ipsum
</AccordionItem>
<AccordionItem label="B" index="2">
Dolor sit amet
</AccordionItem>
</Accordion>
);
Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.
Summary
Congratulations! You have completed the Collapsible Accordion lab. You can practice more labs in LabEx to improve your skills.