From fe20fab96138236d5e9040bd95742b886d7b4009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20V=C3=B6lcsey?= <41576384+zvolcsey@users.noreply.github.com> Date: Thu, 16 Feb 2023 12:35:07 +0100 Subject: [PATCH] feat: Made the categories collapsible (#207) * feat: Made the categories collapsible * Fixed the name of the SideNavbarCategory component * Fixed the gap between the categories --- components/SideNavbar/SideNavbar.tsx | 21 +------- components/SideNavbar/SideNavbarCategory.tsx | 51 +++++++++++++++++++ .../SideNavbar/SideNavbarCategoryList.tsx | 17 +++++++ components/SideNavbar/SideNavbarElement.tsx | 2 +- 4 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 components/SideNavbar/SideNavbarCategory.tsx create mode 100644 components/SideNavbar/SideNavbarCategoryList.tsx diff --git a/components/SideNavbar/SideNavbar.tsx b/components/SideNavbar/SideNavbar.tsx index 6b72e484f..207140654 100644 --- a/components/SideNavbar/SideNavbar.tsx +++ b/components/SideNavbar/SideNavbar.tsx @@ -5,9 +5,9 @@ import { createRef, useContext, useEffect } from "react"; import { AiOutlineClose, AiOutlineMenu } from "react-icons/ai"; import { GlobalContext } from "context/GlobalContext"; import Logo from "../logo"; -import { SideNavbarElement } from "./SideNavbarElement"; import classNames from "classnames"; import { useTheme } from "next-themes"; +import { SideNavbarCategoryList } from "./SideNavbarCategoryList"; export const SideNavbar = () => { const { toggleNav, sidebar, openNav, closeNav } = useContext(GlobalContext); @@ -80,24 +80,7 @@ export const SideNavbar = () => {
-
- {searchResults.map((item, index) => { - return ( -
-

- {item.category} -

- {item.subcategory - .sort((a, b) => - a.name.toUpperCase() < b.name.toUpperCase() ? -1 : 1 - ) - .map((list, i) => { - return ; - })} -
- ); - })} -
+ ); diff --git a/components/SideNavbar/SideNavbarCategory.tsx b/components/SideNavbar/SideNavbarCategory.tsx new file mode 100644 index 000000000..3c3f82a5b --- /dev/null +++ b/components/SideNavbar/SideNavbarCategory.tsx @@ -0,0 +1,51 @@ +import { FC, useEffect, useState } from "react"; +import { FaAngleDown } from "react-icons/fa"; +import { SideNavbarElement } from "./SideNavbarElement"; +import type { ISidebar } from "../../types"; + +export const SideNavbarCategory: FC<{item: ISidebar, openByDefault: string}> = (props) => { + const { item, openByDefault } = props; + + const [isOpen, setIsOpen] = useState(false); + + useEffect(() => { + if (openByDefault === item.category) { + setIsOpen(true); + } + }, [item.category, openByDefault]); + + const handleToggle = () => { + setIsOpen((prevState) => !prevState); + } + + let subcategoryList = null; + + if (isOpen) { + subcategoryList = ( + + ); + } + + return ( +
  • + + {subcategoryList} +
  • + ); +} diff --git a/components/SideNavbar/SideNavbarCategoryList.tsx b/components/SideNavbar/SideNavbarCategoryList.tsx new file mode 100644 index 000000000..cdfef8b39 --- /dev/null +++ b/components/SideNavbar/SideNavbarCategoryList.tsx @@ -0,0 +1,17 @@ +import { FC } from "react"; +import type { ISidebar } from "../../types"; +import { SideNavbarCategory } from "./SideNavbarCategory"; + +export const SideNavbarCategoryList: FC<{items: ISidebar[], openByDefault: string}> = (props) => { + const { items, openByDefault } = props; + + return ( + + ); +}; \ No newline at end of file diff --git a/components/SideNavbar/SideNavbarElement.tsx b/components/SideNavbar/SideNavbarElement.tsx index 7464acb87..f9607bd71 100644 --- a/components/SideNavbar/SideNavbarElement.tsx +++ b/components/SideNavbar/SideNavbarElement.tsx @@ -18,7 +18,7 @@ export const SideNavbarElement = ({ name, url }: SubCategories) => { : "" } collapse w-full hover:bg-gradient-to-l hover:text-gray-200 from-violet-900 to-violet-500 text-start rounded `} > -
    {name}
    +
    {name}
    ); };