|
1 |
| -import React from "react"; |
| 1 | +import React, { useState, useEffect } from "react"; |
2 | 2 | import { Box, Tab, TabProps } from "@mui/material";
|
3 | 3 | import { TabContext, TabList, TabPanel } from "@mui/lab";
|
4 | 4 | import withStyles from "@mui/styles/withStyles";
|
5 | 5 | import { Theme, useTheme } from "@mui/material/styles";
|
6 | 6 | import createStyles from "@mui/styles/createStyles";
|
7 | 7 | import useMediaQuery from "@mui/material/useMediaQuery";
|
| 8 | +import { useLocation } from "react-router-dom"; |
8 | 9 |
|
9 | 10 | export type TabItemProps = {
|
10 | 11 | tabConfig: TabProps | any;
|
@@ -91,25 +92,39 @@ const VerticalTabs = ({
|
91 | 92 | routes,
|
92 | 93 | isRouteTabs,
|
93 | 94 | }: VerticalTabsProps) => {
|
94 |
| - const [value, setValue] = React.useState(selectedTab); |
95 |
| - |
96 | 95 | const theme = useTheme();
|
| 96 | + const { pathname = "" } = useLocation(); |
| 97 | + |
97 | 98 | const isSmallScreen = useMediaQuery(theme.breakpoints.down("md"));
|
98 | 99 |
|
99 |
| - const handleChange = (event: React.SyntheticEvent, newValue: string) => { |
100 |
| - setValue(newValue); |
101 |
| - }; |
| 100 | + const [value, setValue] = useState(selectedTab); |
102 | 101 |
|
103 | 102 | const headerList: TabProps[] = [];
|
104 | 103 | const contentList: React.ReactNode[] = [];
|
105 | 104 |
|
| 105 | + useEffect(() => { |
| 106 | + if (isRouteTabs) { |
| 107 | + const tabConfigElement = children.find( |
| 108 | + (item) => item.tabConfig.to === pathname |
| 109 | + ); |
| 110 | + |
| 111 | + if (tabConfigElement) { |
| 112 | + setValue(tabConfigElement.tabConfig.value); |
| 113 | + } |
| 114 | + } |
| 115 | + }, [isRouteTabs, children, pathname]); |
| 116 | + |
106 | 117 | if (!children) return null;
|
107 | 118 |
|
108 | 119 | children.forEach((child) => {
|
109 | 120 | headerList.push(child.tabConfig);
|
110 | 121 | contentList.push(child.content);
|
111 | 122 | });
|
112 | 123 |
|
| 124 | + const handleChange = (event: React.SyntheticEvent, newValue: string) => { |
| 125 | + setValue(newValue); |
| 126 | + }; |
| 127 | + |
113 | 128 | return (
|
114 | 129 | <TabContext value={`${value}`}>
|
115 | 130 | <Box className={classes.tabsContainer}>
|
|
0 commit comments