From e07e5cacde669f17eab20c35a283baa9a51aaa90 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 17 Jul 2024 16:07:38 +0200 Subject: [PATCH] :bug: Fixing selected tab --- .../Elements/Tabs/HorizontalTab/index.tsx | 5 +++-- .../Elements/Tabs/VerticalTabs/index.tsx | 5 +++-- src/front/Components/Elements/Tabs/index.tsx | 14 +++++++++----- .../Components/Layouts/DesignSystem/index.tsx | 14 +++++++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/front/Components/Elements/Tabs/HorizontalTab/index.tsx b/src/front/Components/Elements/Tabs/HorizontalTab/index.tsx index 3c0f46fa..198cd857 100644 --- a/src/front/Components/Elements/Tabs/HorizontalTab/index.tsx +++ b/src/front/Components/Elements/Tabs/HorizontalTab/index.tsx @@ -2,13 +2,14 @@ import { useCallback } from "react"; import classes from "./classes.module.scss"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; import useHoverable from "@Front/Hooks/useHoverable"; +import { ITabValue } from ".."; export type ITab = { label: React.ReactNode; }; export type IProps = { - onSelect: (value: T) => void; - value: T; + onSelect: (value: ITabValue) => void; + value: ITabValue; isSelected: boolean; } & ITab; diff --git a/src/front/Components/Elements/Tabs/VerticalTabs/index.tsx b/src/front/Components/Elements/Tabs/VerticalTabs/index.tsx index dc901e91..e54247bb 100644 --- a/src/front/Components/Elements/Tabs/VerticalTabs/index.tsx +++ b/src/front/Components/Elements/Tabs/VerticalTabs/index.tsx @@ -1,13 +1,14 @@ import { useCallback } from "react"; import classes from "./classes.module.scss"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; +import { ITabValue } from ".."; export type ITab = { label: React.ReactNode; }; export type IProps = { - onSelect: (value: T) => void; - value: T; + onSelect: (value: ITabValue) => void; + value: ITabValue; isSelected: boolean; } & ITab; diff --git a/src/front/Components/Elements/Tabs/index.tsx b/src/front/Components/Elements/Tabs/index.tsx index 08cc6438..bd5d04bd 100644 --- a/src/front/Components/Elements/Tabs/index.tsx +++ b/src/front/Components/Elements/Tabs/index.tsx @@ -6,9 +6,13 @@ import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Ty import { useDebounce, useWindowSize } from "@uidotdev/usehooks"; import useOpenable from "@Front/Hooks/useOpenable"; +export type ITabValue = T & { + id: unknown; +}; + type ITabInternal = ITab & { key?: string; - value: T; + value: ITabValue; }; type IProps = { @@ -22,7 +26,7 @@ export default function Tabs(props: IProps) { const [visibleElements, setVisibleElements] = useState[]>([]); const [overflowedElements, setOverflowedElements] = useState[]>([]); - const [selectedTab, setSelectedTab] = useState(props.tabs[0]!.value); + const [selectedTab, setSelectedTab] = useState>(props.tabs[0]!.value); const { close, isOpen, toggle } = useOpenable(); @@ -55,7 +59,7 @@ export default function Tabs(props: IProps) { }, [calculateVisibleElements, windowSizeDebounced]); const handleSelect = useCallback( - (value: T) => { + (value: ITabValue) => { setSelectedTab(value); onSelect(value); close(); @@ -72,7 +76,7 @@ export default function Tabs(props: IProps) { key={element.key ?? index} value={element.value} onSelect={handleSelect} - isSelected={selectedTab === element.value} + isSelected={element.value.id === selectedTab.id} /> ))} @@ -84,7 +88,7 @@ export default function Tabs(props: IProps) { key={element.key ?? index} value={element.value} onSelect={handleSelect} - isSelected={selectedTab === element.value} + isSelected={element.value.id === selectedTab.id} /> ))} diff --git a/src/front/Components/Layouts/DesignSystem/index.tsx b/src/front/Components/Layouts/DesignSystem/index.tsx index 93c99890..37d26764 100644 --- a/src/front/Components/Layouts/DesignSystem/index.tsx +++ b/src/front/Components/Layouts/DesignSystem/index.tsx @@ -8,7 +8,7 @@ import DefaultTemplate from "@Front/Components/LayoutTemplates/DefaultTemplate"; import classes from "./classes.module.scss"; import Tabs from "@Front/Components/Elements/Tabs"; -import { useCallback } from "react"; +import { useCallback, useState } from "react"; export default function DesignSystem() { const userDb = [ @@ -34,13 +34,17 @@ export default function DesignSystem() { }, ]; + const [selectedTab, setSelectedTab] = useState<(typeof userDb)[number]>(userDb[0]!); + const onSelect = useCallback((value: (typeof userDb)[number]) => { - console.log(value); + setSelectedTab(value); }, []); return ( + DesignSystem + Tabs tabs={userDb.map((user) => ({ label: user.username, @@ -49,8 +53,12 @@ export default function DesignSystem() { }))} onSelect={onSelect} /> +
+ + {selectedTab.id} - {selectedTab.username} + +
- DesignSystem
Circle Progress