🐛 Fixing selected tab
This commit is contained in:
parent
88acd08bcf
commit
e07e5cacde
@ -2,13 +2,14 @@ import { useCallback } from "react";
|
|||||||
import classes from "./classes.module.scss";
|
import classes from "./classes.module.scss";
|
||||||
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
|
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
|
||||||
import useHoverable from "@Front/Hooks/useHoverable";
|
import useHoverable from "@Front/Hooks/useHoverable";
|
||||||
|
import { ITabValue } from "..";
|
||||||
export type ITab = {
|
export type ITab = {
|
||||||
label: React.ReactNode;
|
label: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type IProps<T> = {
|
export type IProps<T> = {
|
||||||
onSelect: (value: T) => void;
|
onSelect: (value: ITabValue<T>) => void;
|
||||||
value: T;
|
value: ITabValue<T>;
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
} & ITab;
|
} & ITab;
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import { useCallback } from "react";
|
import { useCallback } from "react";
|
||||||
import classes from "./classes.module.scss";
|
import classes from "./classes.module.scss";
|
||||||
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
|
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
|
||||||
|
import { ITabValue } from "..";
|
||||||
export type ITab = {
|
export type ITab = {
|
||||||
label: React.ReactNode;
|
label: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type IProps<T> = {
|
export type IProps<T> = {
|
||||||
onSelect: (value: T) => void;
|
onSelect: (value: ITabValue<T>) => void;
|
||||||
value: T;
|
value: ITabValue<T>;
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
} & ITab;
|
} & ITab;
|
||||||
|
|
||||||
|
@ -6,9 +6,13 @@ import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Ty
|
|||||||
import { useDebounce, useWindowSize } from "@uidotdev/usehooks";
|
import { useDebounce, useWindowSize } from "@uidotdev/usehooks";
|
||||||
import useOpenable from "@Front/Hooks/useOpenable";
|
import useOpenable from "@Front/Hooks/useOpenable";
|
||||||
|
|
||||||
|
export type ITabValue<T> = T & {
|
||||||
|
id: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
type ITabInternal<T> = ITab & {
|
type ITabInternal<T> = ITab & {
|
||||||
key?: string;
|
key?: string;
|
||||||
value: T;
|
value: ITabValue<T>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type IProps<T> = {
|
type IProps<T> = {
|
||||||
@ -22,7 +26,7 @@ export default function Tabs<T>(props: IProps<T>) {
|
|||||||
const [visibleElements, setVisibleElements] = useState<ITabInternal<T>[]>([]);
|
const [visibleElements, setVisibleElements] = useState<ITabInternal<T>[]>([]);
|
||||||
const [overflowedElements, setOverflowedElements] = useState<ITabInternal<T>[]>([]);
|
const [overflowedElements, setOverflowedElements] = useState<ITabInternal<T>[]>([]);
|
||||||
|
|
||||||
const [selectedTab, setSelectedTab] = useState<T>(props.tabs[0]!.value);
|
const [selectedTab, setSelectedTab] = useState<ITabValue<T>>(props.tabs[0]!.value);
|
||||||
|
|
||||||
const { close, isOpen, toggle } = useOpenable();
|
const { close, isOpen, toggle } = useOpenable();
|
||||||
|
|
||||||
@ -55,7 +59,7 @@ export default function Tabs<T>(props: IProps<T>) {
|
|||||||
}, [calculateVisibleElements, windowSizeDebounced]);
|
}, [calculateVisibleElements, windowSizeDebounced]);
|
||||||
|
|
||||||
const handleSelect = useCallback(
|
const handleSelect = useCallback(
|
||||||
(value: T) => {
|
(value: ITabValue<T>) => {
|
||||||
setSelectedTab(value);
|
setSelectedTab(value);
|
||||||
onSelect(value);
|
onSelect(value);
|
||||||
close();
|
close();
|
||||||
@ -72,7 +76,7 @@ export default function Tabs<T>(props: IProps<T>) {
|
|||||||
key={element.key ?? index}
|
key={element.key ?? index}
|
||||||
value={element.value}
|
value={element.value}
|
||||||
onSelect={handleSelect}
|
onSelect={handleSelect}
|
||||||
isSelected={selectedTab === element.value}
|
isSelected={element.value.id === selectedTab.id}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@ -84,7 +88,7 @@ export default function Tabs<T>(props: IProps<T>) {
|
|||||||
key={element.key ?? index}
|
key={element.key ?? index}
|
||||||
value={element.value}
|
value={element.value}
|
||||||
onSelect={handleSelect}
|
onSelect={handleSelect}
|
||||||
isSelected={selectedTab === element.value}
|
isSelected={element.value.id === selectedTab.id}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
@ -8,7 +8,7 @@ import DefaultTemplate from "@Front/Components/LayoutTemplates/DefaultTemplate";
|
|||||||
|
|
||||||
import classes from "./classes.module.scss";
|
import classes from "./classes.module.scss";
|
||||||
import Tabs from "@Front/Components/Elements/Tabs";
|
import Tabs from "@Front/Components/Elements/Tabs";
|
||||||
import { useCallback } from "react";
|
import { useCallback, useState } from "react";
|
||||||
|
|
||||||
export default function DesignSystem() {
|
export default function DesignSystem() {
|
||||||
const userDb = [
|
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]) => {
|
const onSelect = useCallback((value: (typeof userDb)[number]) => {
|
||||||
console.log(value);
|
setSelectedTab(value);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultTemplate title={"DesignSystem"}>
|
<DefaultTemplate title={"DesignSystem"}>
|
||||||
|
<Typography typo={ETypo.DISPLAY_LARGE}>DesignSystem</Typography>
|
||||||
<Newsletter isOpen />
|
<Newsletter isOpen />
|
||||||
|
<Typography typo={ETypo.TEXT_LG_BOLD}>Tabs</Typography>
|
||||||
<Tabs<(typeof userDb)[number]>
|
<Tabs<(typeof userDb)[number]>
|
||||||
tabs={userDb.map((user) => ({
|
tabs={userDb.map((user) => ({
|
||||||
label: user.username,
|
label: user.username,
|
||||||
@ -49,8 +53,12 @@ export default function DesignSystem() {
|
|||||||
}))}
|
}))}
|
||||||
onSelect={onSelect}
|
onSelect={onSelect}
|
||||||
/>
|
/>
|
||||||
|
<div className={classes["tab-content"]}>
|
||||||
|
<Typography typo={ETypo.TEXT_MD_REGULAR}>
|
||||||
|
{selectedTab.id} - {selectedTab.username}
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
<div className={classes["root"]}>
|
<div className={classes["root"]}>
|
||||||
<Typography typo={ETypo.DISPLAY_LARGE}>DesignSystem</Typography>
|
|
||||||
<div className={classes["components"]}>
|
<div className={classes["components"]}>
|
||||||
<Typography typo={ETypo.TEXT_LG_BOLD}>Circle Progress</Typography>
|
<Typography typo={ETypo.TEXT_LG_BOLD}>Circle Progress</Typography>
|
||||||
<div className={classes["rows"]}>
|
<div className={classes["rows"]}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user