import React, { DetailedHTMLProps, HTMLAttributes } from "react"; import { twMerge } from "tailwind-merge"; import Border from "./Border"; import Stack from "../layout/Stack"; import Row from "../layout/Row"; import Span from "../layout/Span"; export type TWUITabsObject = { title: string; value: string; content: React.ReactNode; defaultActive?: boolean; }; export type TWUI_TOGGLE_PROPS = React.ComponentProps & { tabsContentArray: TWUITabsObject[]; tabsBorderProps?: React.ComponentProps; tabsButtonsWrapperProps?: React.DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; centered?: boolean; debounce?: number; }; /** * # Tabs Component * @className twui-tabs-wrapper * @className twui-tab-buttons * @className twui-tab-button-active * @className twui-tab-buttons-wrapper */ export default function Tabs({ tabsContentArray, tabsBorderProps, tabsButtonsWrapperProps, centered, debounce = 100, ...props }: TWUI_TOGGLE_PROPS) { const values = tabsContentArray.map((obj) => obj.value); const [activeValue, setActiveValue] = React.useState( tabsContentArray.find((ctn) => ctn.defaultActive)?.value || values[0] || undefined ); const targetContent = tabsContentArray.find( (ctn) => ctn.value == activeValue ); return (
{values.map((value, index) => { const targetObject = tabsContentArray.find( (ctn) => ctn.value == value ); const isActive = value == activeValue; return ( { setActiveValue(undefined); setTimeout(() => { setActiveValue(value); }, debounce); }} key={index} > {targetObject?.title} ); })}
{targetContent?.content}
); }