From 3441e405a64d5b1051e5c911a1b762cde05e44ed Mon Sep 17 00:00:00 2001 From: julius Date: Mon, 17 Mar 2025 11:09:29 +0100 Subject: [PATCH] feat: keep states between tab switches --- src/Rankings.tsx | 41 +++++++++++++-------------------------- src/TabController.tsx | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 src/TabController.tsx diff --git a/src/Rankings.tsx b/src/Rankings.tsx index fb5f166..51a2bc9 100644 --- a/src/Rankings.tsx +++ b/src/Rankings.tsx @@ -1,8 +1,16 @@ -import { ButtonHTMLAttributes, useEffect, useRef, useState } from "react"; +import { + ButtonHTMLAttributes, + Fragment, + ReactNode, + useEffect, + useRef, + useState, +} from "react"; import { ReactSortable, ReactSortableProps } from "react-sortablejs"; import { apiAuth, User } from "./api"; import { useSession } from "./Session"; import { Chemistry, MVPRanking } from "./types"; +import TabController from "./TabController"; type PlayerListProps = Partial> & { orderedList?: boolean; @@ -271,7 +279,6 @@ function HeaderControl({ onLoad, onClear }: HeaderControlProps) { export default function Rankings() { const { user } = useSession(); const [players, setPlayers] = useState(null); - const [openTab, setOpenTab] = useState("Chemistry"); async function loadPlayers() { try { @@ -294,32 +301,10 @@ export default function Rankings() { return ( <> {user && players ? ( -
-
- {tabs.map((tab) => ( - - ))} -
- {tabs.map((tab) => { - if (openTab !== tab.id) return null; - switch (tab.id) { - case "Chemistry": - return ; - case "MVP": - return ; - default: - return null; - } - })} -
+ + + + ) : ( )} diff --git a/src/TabController.tsx b/src/TabController.tsx new file mode 100644 index 0000000..3be700b --- /dev/null +++ b/src/TabController.tsx @@ -0,0 +1,45 @@ +import { Fragment, ReactNode, useState } from "react"; + +interface TabProps { + id: string; + label: string; +} + +interface TabControllerProps { + tabs: TabProps[]; + children: ReactNode[]; +} + +export default function TabController({ tabs, children }: TabControllerProps) { + const [currentIndex, setCurrentIndex] = useState(0); + const handleTabClick = (index: number) => { + setCurrentIndex(index); + }; + + return ( +
+
+
+ {tabs.map((tab, index) => ( + + ))} +
+
+ {children.map((child, index) => ( + +
+ {child} +
+
+ ))} +
+ ); +}