FPSMS-frontend
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

103 řádky
2.7 KiB

  1. "use client";
  2. import { useState, ReactNode, useEffect } from "react";
  3. import { Box, Tabs, Tab } from "@mui/material";
  4. import { useTranslation } from "react-i18next";
  5. import { useSearchParams, useRouter } from "next/navigation";
  6. interface TabPanelProps {
  7. children?: ReactNode;
  8. index: number;
  9. value: number;
  10. }
  11. function TabPanel(props: TabPanelProps) {
  12. const { children, value, index, ...other } = props;
  13. return (
  14. <div
  15. role="tabpanel"
  16. hidden={value !== index}
  17. id={`qr-code-handle-tabpanel-${index}`}
  18. aria-labelledby={`qr-code-handle-tab-${index}`}
  19. {...other}
  20. >
  21. {value === index && <Box sx={{ py: 3 }}>{children}</Box>}
  22. </div>
  23. );
  24. }
  25. interface QrCodeHandleTabsProps {
  26. userTabContent: ReactNode;
  27. equipmentTabContent: ReactNode;
  28. warehouseTabContent: ReactNode;
  29. }
  30. const QrCodeHandleTabs: React.FC<QrCodeHandleTabsProps> = ({
  31. userTabContent,
  32. equipmentTabContent,
  33. warehouseTabContent,
  34. }) => {
  35. const { t: tQrCodeHandle } = useTranslation("qrCodeHandle");
  36. const { t: tUser } = useTranslation("user");
  37. const { t: tWarehouse } = useTranslation("warehouse");
  38. const searchParams = useSearchParams();
  39. const router = useRouter();
  40. const getInitialTab = () => {
  41. const tab = searchParams.get("tab");
  42. if (tab === "equipment") return 1;
  43. if (tab === "warehouse") return 2;
  44. if (tab === "user") return 0;
  45. return 0;
  46. };
  47. const [currentTab, setCurrentTab] = useState(getInitialTab);
  48. useEffect(() => {
  49. const tab = searchParams.get("tab");
  50. if (tab === "equipment") {
  51. setCurrentTab(1);
  52. } else if (tab === "warehouse") {
  53. setCurrentTab(2);
  54. } else if (tab === "user") {
  55. setCurrentTab(0);
  56. }
  57. }, [searchParams]);
  58. const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
  59. setCurrentTab(newValue);
  60. let tabName = "user";
  61. if (newValue === 1) tabName = "equipment";
  62. else if (newValue === 2) tabName = "warehouse";
  63. const params = new URLSearchParams(searchParams.toString());
  64. params.set("tab", tabName);
  65. router.push(`?${params.toString()}`, { scroll: false });
  66. };
  67. return (
  68. <Box sx={{ width: "100%" }}>
  69. <Box sx={{ borderBottom: 1, borderColor: "divider" }}>
  70. <Tabs value={currentTab} onChange={handleTabChange}>
  71. <Tab label={tUser("User")} />
  72. <Tab label={tQrCodeHandle("Equipment")} />
  73. <Tab label={tWarehouse("Warehouse")} />
  74. </Tabs>
  75. </Box>
  76. <TabPanel value={currentTab} index={0}>
  77. {userTabContent}
  78. </TabPanel>
  79. <TabPanel value={currentTab} index={1}>
  80. {equipmentTabContent}
  81. </TabPanel>
  82. <TabPanel value={currentTab} index={2}>
  83. {warehouseTabContent}
  84. </TabPanel>
  85. </Box>
  86. );
  87. };
  88. export default QrCodeHandleTabs;