From 726afcc7f81bd931e1c4b91157aceceb9df72300 Mon Sep 17 00:00:00 2001 From: LASER-Yi Date: Wed, 9 Nov 2022 22:09:10 +0800 Subject: [PATCH] Add support of string value for badge in navbar --- frontend/src/App/Navbar.tsx | 27 +++++++++++++++++++-------- frontend/src/Router/type.d.ts | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/frontend/src/App/Navbar.tsx b/frontend/src/App/Navbar.tsx index a6e8791d4..abde1e82b 100644 --- a/frontend/src/App/Navbar.tsx +++ b/frontend/src/App/Navbar.tsx @@ -53,6 +53,10 @@ function useSelection() { function useBadgeValue(route: Route.Item) { const { badge, children } = route; return useMemo(() => { + if (typeof badge === "string") { + return badge; + } + let value = badge ?? 0; if (children === undefined) { @@ -61,8 +65,9 @@ function useBadgeValue(route: Route.Item) { value += children.reduce((acc, child: Route.Item) => { - if (child.badge && child.hidden !== true) { - return acc + (child.badge ?? 0); + const childBadgeValue = child.badge; + if (typeof childBadgeValue === "number" && child.hidden !== true) { + return acc + childBadgeValue; } return acc; }, 0) ?? 0; @@ -290,7 +295,7 @@ interface NavbarItemProps { name: string; link: string; icon?: IconDefinition; - badge?: number; + badge?: number | string; primary?: boolean; onClick?: (event: React.MouseEvent) => void; } @@ -309,6 +314,16 @@ const NavbarItem: FunctionComponent = ({ const { ref, hovered } = useHover(); + const shouldHideBadge = useMemo(() => { + if (typeof badge === "number") { + return badge === 0; + } else if (typeof badge === "string") { + return badge.length === 0; + } + + return true; + }, [badge]); + return ( = ({ > )} {name} -