TabNavigation.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Tab 导航组件
  3. *
  4. * 统一的 Tab 切换样式,用于各个页面的模式/类型切换
  5. */
  6. import React from 'react';
  7. import { LucideIcon } from 'lucide-react';
  8. export interface TabItem<T = string> {
  9. id: T;
  10. label: string;
  11. icon?: LucideIcon;
  12. }
  13. interface TabNavigationProps<T = string> {
  14. /** Tab 列表 */
  15. tabs: TabItem<T>[];
  16. /** 当前激活的 Tab ID */
  17. activeTab: T;
  18. /** Tab 切换回调 */
  19. onTabChange: (tabId: T) => void;
  20. /** 自定义容器类名 */
  21. className?: string;
  22. }
  23. function TabNavigation<T extends string = string>({
  24. tabs,
  25. activeTab,
  26. onTabChange,
  27. className = ''
  28. }: TabNavigationProps<T>) {
  29. return (
  30. <div className={`flex space-x-1 p-1 bg-gray-100/50 w-fit rounded-xl border border-gray-100 ${className}`}>
  31. {tabs.map((tab) => {
  32. const Icon = tab.icon;
  33. return (
  34. <button
  35. key={tab.id}
  36. onClick={() => onTabChange(tab.id)}
  37. className={`flex items-center space-x-2 px-6 py-2 rounded-lg text-sm font-bold transition-all whitespace-nowrap ${
  38. activeTab === tab.id
  39. ? 'bg-white text-blue-600 shadow-sm'
  40. : 'text-gray-500 hover:text-gray-700'
  41. }`}
  42. >
  43. {Icon && <Icon className="w-4 h-4" />}
  44. <span>{tab.label}</span>
  45. </button>
  46. );
  47. })}
  48. </div>
  49. );
  50. }
  51. export default TabNavigation;