forked from namastedev/namaste-frontend-system-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d43a018
commit 08905fe
Showing
3 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useState } from "react"; | ||
import AccordionItem from "./AccordionItem"; | ||
|
||
const data = [ | ||
{ | ||
title: "Accordion Item #1", | ||
body: "This is the first item's accordion body. It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.", | ||
}, | ||
{ | ||
title: "Accordion Item #2", | ||
body: "This is the first item's accordion body. It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.", | ||
}, | ||
{ | ||
title: "Accordion Item #3", | ||
body: "This is the first item's accordion body. It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.", | ||
}, | ||
]; | ||
|
||
const Accordion = () => { | ||
const [openIndex, setOpenIndex] = useState(0); | ||
return ( | ||
<div className="w-[50%] m-auto mt-5"> | ||
{data.map((item, index) => ( | ||
<AccordionItem | ||
key={index} | ||
title={item.title} | ||
body={item.body} | ||
isOpen={index === openIndex ? true : false} | ||
setIsOpen={() => { | ||
index === openIndex ? setOpenIndex(null) : setOpenIndex(index); | ||
}} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}; | ||
export default Accordion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const AccordionItem = ({ title, body, isOpen, setIsOpen }) => { | ||
return ( | ||
<div className="border border-black "> | ||
<div | ||
className="font-bold p-2 bg-slate-200 flex justify-between cursor-pointer" | ||
onClick={() => { | ||
setIsOpen((isOpen) => !isOpen); | ||
}} | ||
> | ||
<span>{title}</span> | ||
<span>⬇️</span> | ||
</div> | ||
{isOpen && <div className="p-2">{body}</div>} | ||
</div> | ||
); | ||
}; | ||
export default AccordionItem; |