File size: 1,676 Bytes
a8b3f00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import type { FC } from 'react'
import cn from '@/utils/classnames'
type Option = {
value: string
text: string
}
type TabSliderProps = {
className?: string
itemWidth?: number
value: string
onChange: (v: string) => void
options: Option[]
}
const TabSlider: FC<TabSliderProps> = ({
className,
itemWidth = 118,
value,
onChange,
options,
}) => {
const currentIndex = options.findIndex(option => option.value === value)
const current = options[currentIndex]
return (
<div className={cn(className, 'relative flex p-0.5 rounded-lg bg-gray-200')}>
{
options.map((option, index) => (
<div
key={option.value}
className={`
flex justify-center items-center h-7 text-[13px]
font-semibold text-gray-600 rounded-[7px] cursor-pointer
hover:bg-gray-50
${index !== options.length - 1 && 'mr-[1px]'}
`}
style={{
width: itemWidth,
}}
onClick={() => onChange(option.value)}
>
{option.text}
</div>
))
}
{
current && (
<div
className={`
absolute flex justify-center items-center h-7 bg-white text-[13px] font-semibold text-primary-600
border-[0.5px] border-gray-200 rounded-[7px] shadow-xs transition-transform
`}
style={{
width: itemWidth,
transform: `translateX(${currentIndex * itemWidth + 1}px)`,
}}
>
{current.text}
</div>
)
}
</div>
)
}
export default TabSlider
|