From 08905fe097ba3b7310ae73aefb313fe5d5f958d1 Mon Sep 17 00:00:00 2001 From: Akshay Saini Date: Fri, 23 Feb 2024 15:39:04 +0530 Subject: [PATCH] machine-coding/accordion --- LLD/app/src/App.js | 7 +++-- LLD/app/src/components/Accordion.js | 37 +++++++++++++++++++++++++ LLD/app/src/components/AccordionItem.js | 17 ++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 LLD/app/src/components/Accordion.js create mode 100644 LLD/app/src/components/AccordionItem.js diff --git a/LLD/app/src/App.js b/LLD/app/src/App.js index 2b7254f..bbfbd59 100644 --- a/LLD/app/src/App.js +++ b/LLD/app/src/App.js @@ -1,10 +1,11 @@ import About from "./components/About"; import Body from "./components/Body"; -import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom"; +import { BrowserRouter, Route, Routes } from "react-router-dom"; import Team from "./components/Team"; import Login from "./components/Login"; import ProtectedRoute from "./components/ProtectedRoute"; import { useState } from "react"; +import Accordion from "./components/Accordion"; function App() { const [lang, setLang] = useState("en"); @@ -12,9 +13,10 @@ function App() {
Hello World -
diff --git a/LLD/app/src/components/Accordion.js b/LLD/app/src/components/Accordion.js new file mode 100644 index 0000000..5a6f17e --- /dev/null +++ b/LLD/app/src/components/Accordion.js @@ -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 ( +
+ {data.map((item, index) => ( + { + index === openIndex ? setOpenIndex(null) : setOpenIndex(index); + }} + /> + ))} +
+ ); +}; +export default Accordion; diff --git a/LLD/app/src/components/AccordionItem.js b/LLD/app/src/components/AccordionItem.js new file mode 100644 index 0000000..07e584d --- /dev/null +++ b/LLD/app/src/components/AccordionItem.js @@ -0,0 +1,17 @@ +const AccordionItem = ({ title, body, isOpen, setIsOpen }) => { + return ( +
+
{ + setIsOpen((isOpen) => !isOpen); + }} + > + {title} + ⬇️ +
+ {isOpen &&
{body}
} +
+ ); +}; +export default AccordionItem;