import type { FC } from 'react' import { useEffect, useState } from 'react' import cn from '@/utils/classnames' import Badge, { BadgeState } from '@/app/components/base/badge/index' type Option = { value: string text: string } type TabSliderProps = { className?: string value: string onChange: (v: string) => void options: Option[] } const TabSlider: FC = ({ className, value, onChange, options, }) => { const [activeIndex, setActiveIndex] = useState(options.findIndex(option => option.value === value)) const [sliderStyle, setSliderStyle] = useState({}) const updateSliderStyle = (index: number) => { const tabElement = document.getElementById(`tab-${index}`) if (tabElement) { const { offsetLeft, offsetWidth } = tabElement setSliderStyle({ transform: `translateX(${offsetLeft}px)`, width: `${offsetWidth}px`, }) } } useEffect(() => { const newIndex = options.findIndex(option => option.value === value) setActiveIndex(newIndex) updateSliderStyle(newIndex) }, [value, options]) return (
{options.map((option, index) => (
{ if (index !== activeIndex) { onChange(option.value) updateSliderStyle(index) } }} > {option.text} {option.value === 'plugins' && 6 }
))}
) } export default TabSlider