Skip to content

Commit

Permalink
machine-coding/accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymarch7 committed Feb 23, 2024
1 parent d43a018 commit 08905fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
7 changes: 5 additions & 2 deletions LLD/app/src/App.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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");
return (
<div>
<header className="text-2xl font-bold py-5 bg-black text-white text-center flex">
Hello World
<nav className="p-2 m-2 w-96 justify-between text-lg">
<nav className="px-20 m-2 w-[600px] flex justify-between text-lg">
<a href="/">Home </a>
<a href="/about">About </a>
<a href="/accordion">Accordion </a>
<a href="/team">Team </a>
<a href="/login">Login </a>
</nav>
Expand All @@ -34,6 +36,7 @@ function App() {
</Route>
<Route path="/about" element={<About lang={lang} />}></Route>
<Route path="/login" element={<Login />}></Route>
<Route path="/accordion" element={<Accordion />}></Route>
</Routes>
</BrowserRouter>
</div>
Expand Down
37 changes: 37 additions & 0 deletions LLD/app/src/components/Accordion.js
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;
17 changes: 17 additions & 0 deletions LLD/app/src/components/AccordionItem.js
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;

0 comments on commit 08905fe

Please sign in to comment.