| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import cn from 'classnames' | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Option = { | 
					
						
							|  |  |  |   value: string | 
					
						
							|  |  |  |   text: string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | type TabSliderProps = { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   className?: string | 
					
						
							|  |  |  |   itemWidth?: number | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |   value: string | 
					
						
							|  |  |  |   onChange: (v: string) => void | 
					
						
							|  |  |  |   options: Option[] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | const TabSlider: FC<TabSliderProps> = ({ | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   className, | 
					
						
							|  |  |  |   itemWidth = 118, | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |   value, | 
					
						
							|  |  |  |   onChange, | 
					
						
							|  |  |  |   options, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const currentIndex = options.findIndex(option => option.value === value) | 
					
						
							|  |  |  |   const current = options[currentIndex] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |     <div className={cn(className, 'relative flex p-0.5 rounded-lg bg-gray-200')}> | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |       { | 
					
						
							|  |  |  |         options.map((option, index) => ( | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             key={option.value} | 
					
						
							|  |  |  |             className={`
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |               flex justify-center items-center h-7 text-[13px]  | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |               font-semibold text-gray-600 rounded-[7px] cursor-pointer | 
					
						
							|  |  |  |               hover:bg-gray-50 | 
					
						
							|  |  |  |               ${index !== options.length - 1 && 'mr-[1px]'} | 
					
						
							|  |  |  |             `}
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             style={{ | 
					
						
							|  |  |  |               width: itemWidth, | 
					
						
							|  |  |  |             }} | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |             onClick={() => onChange(option.value)} | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             {option.text} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         current && ( | 
					
						
							|  |  |  |           <div | 
					
						
							|  |  |  |             className={`
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |               absolute flex justify-center items-center h-7 bg-white text-[13px] font-semibold text-primary-600  | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |               border-[0.5px] border-gray-200 rounded-[7px] shadow-xs transition-transform | 
					
						
							|  |  |  |             `}
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             style={{ | 
					
						
							|  |  |  |               width: itemWidth, | 
					
						
							|  |  |  |               transform: `translateX(${currentIndex * itemWidth + 1}px)`, | 
					
						
							|  |  |  |             }} | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:49 +08:00
										 |  |  |           > | 
					
						
							|  |  |  |             {current.text} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default TabSlider |