feat: keep states between tab switches
This commit is contained in:
parent
8f355c0cf3
commit
3441e405a6
@ -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 { ReactSortable, ReactSortableProps } from "react-sortablejs";
|
||||||
import { apiAuth, User } from "./api";
|
import { apiAuth, User } from "./api";
|
||||||
import { useSession } from "./Session";
|
import { useSession } from "./Session";
|
||||||
import { Chemistry, MVPRanking } from "./types";
|
import { Chemistry, MVPRanking } from "./types";
|
||||||
|
import TabController from "./TabController";
|
||||||
|
|
||||||
type PlayerListProps = Partial<ReactSortableProps<any>> & {
|
type PlayerListProps = Partial<ReactSortableProps<any>> & {
|
||||||
orderedList?: boolean;
|
orderedList?: boolean;
|
||||||
@ -271,7 +279,6 @@ function HeaderControl({ onLoad, onClear }: HeaderControlProps) {
|
|||||||
export default function Rankings() {
|
export default function Rankings() {
|
||||||
const { user } = useSession();
|
const { user } = useSession();
|
||||||
const [players, setPlayers] = useState<User[] | null>(null);
|
const [players, setPlayers] = useState<User[] | null>(null);
|
||||||
const [openTab, setOpenTab] = useState("Chemistry");
|
|
||||||
|
|
||||||
async function loadPlayers() {
|
async function loadPlayers() {
|
||||||
try {
|
try {
|
||||||
@ -294,32 +301,10 @@ export default function Rankings() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{user && players ? (
|
{user && players ? (
|
||||||
<div>
|
<TabController tabs={tabs}>
|
||||||
<div className="container navbar">
|
<ChemistryDnD {...{ user, players }} />
|
||||||
{tabs.map((tab) => (
|
<MVPDnD {...{ user, players }} />
|
||||||
<button
|
</TabController>
|
||||||
key={tab.id}
|
|
||||||
className={
|
|
||||||
openTab === tab.id ? "tab-button active" : "tab-button"
|
|
||||||
}
|
|
||||||
onClick={() => setOpenTab(tab.id)}
|
|
||||||
>
|
|
||||||
{tab.label}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
{tabs.map((tab) => {
|
|
||||||
if (openTab !== tab.id) return null;
|
|
||||||
switch (tab.id) {
|
|
||||||
case "Chemistry":
|
|
||||||
return <ChemistryDnD key={tab.id} {...{ user, players }} />;
|
|
||||||
case "MVP":
|
|
||||||
return <MVPDnD key={tab.id} {...{ user, players }} />;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
) : (
|
) : (
|
||||||
<span className="loader" />
|
<span className="loader" />
|
||||||
)}
|
)}
|
||||||
|
45
src/TabController.tsx
Normal file
45
src/TabController.tsx
Normal file
@ -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 (
|
||||||
|
<div>
|
||||||
|
<div>
|
||||||
|
<div className="container navbar">
|
||||||
|
{tabs.map((tab, index) => (
|
||||||
|
<button
|
||||||
|
key={tab.id}
|
||||||
|
className={
|
||||||
|
currentIndex === index ? "tab-button active" : "tab-button"
|
||||||
|
}
|
||||||
|
onClick={() => handleTabClick(index)}
|
||||||
|
>
|
||||||
|
{tab.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{children.map((child, index) => (
|
||||||
|
<Fragment key={index}>
|
||||||
|
<div style={{ display: currentIndex === index ? "block" : "none" }}>
|
||||||
|
{child}
|
||||||
|
</div>
|
||||||
|
</Fragment>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user