import classnames from 'classnames'; import type { ReactNode } from 'react'; import { memo, useEffect, useRef } from 'react'; import { Button } from '../Button'; import { Icon } from '../Icon'; import type { RadioDropdownProps } from '../RadioDropdown'; import { RadioDropdown } from '../RadioDropdown'; import { HStack } from '../Stacks'; import './Tabs.css'; export type TabItem = { value: string; label: string; options?: Omit; }; interface Props { label: string; value?: string; onChangeValue: (value: string) => void; tabs: TabItem[]; tabListClassName?: string; className?: string; children: ReactNode; } export const Tabs = memo(function Tabs({ value, onChangeValue, label, children, tabs, className, tabListClassName, }: Props) { const ref = useRef(null); const handleTabChange = (value: string) => { const tabs = ref.current?.querySelectorAll(`[data-tab]`); for (const tab of tabs ?? []) { const v = tab.getAttribute('data-tab'); if (v === value) { tab.setAttribute('tabindex', '0'); tab.setAttribute('data-state', 'active'); } else { tab.setAttribute('data-state', 'inactive'); } } onChangeValue(value); }; useEffect(() => { if (value === undefined) return; handleTabChange(value); }, [value]); return (
{tabs.map((t) => { const isActive = t.value === value; if (t.options && isActive) { return ( ); } else if (t.options && !isActive) { return ( ); } else { return ( ); } })}
{children}
); }); interface TabContentProps { value: string; children: ReactNode; className?: string; } export const TabContent = memo(function TabContent({ value, children, className, }: TabContentProps) { return (
{children}
); });