index.tsx 1.4 KB

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