【发布时间】:2022-12-16 14:02:42
【问题描述】:
请告知如何有条件地隐藏或显示 MUI 选项卡标题?
我试图在悬停在特定区域或单击另一个按钮时有条件地隐藏我的 MUI 选项卡标题,是否可行?请高手指教一个可行的办法?
【问题讨论】:
标签: reactjs material-ui react-mui
请告知如何有条件地隐藏或显示 MUI 选项卡标题?
我试图在悬停在特定区域或单击另一个按钮时有条件地隐藏我的 MUI 选项卡标题,是否可行?请高手指教一个可行的办法?
【问题讨论】:
标签: reactjs material-ui react-mui
添加了一个状态来切换选项卡的显示。修改了来自https://mui.com/material-ui/react-tabs/的Basic Tabs演示代码
import * as React from 'react';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import { Button } from '@mui/material';
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
function a11yProps(index: number) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
export default function BasicTabs() {
const [value, setValue] = React.useState(0);
// State to toggle tab 3
const [showItemThree, setShowItemThree] = React.useState(true);
const handleChange = (event: React.SyntheticEvent, newValue: number) => {
setValue(newValue);
};
return (
<Box sx={{ width: '100%' }}>
{/* Button to toggle tab 3 */}
<Button variant="contained" onClick={() => setShowItemThree(!showItemThree)}>
Toggle ITEM THREE display
</Button>
{/* Tabs */}
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs value={value} onChange={handleChange} aria-label="basic tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
{/* Conditionally show/hide tab 3 */}
{showItemThree &&
<Tab label="Item Three" {...a11yProps(2)} />
}
</Tabs>
</Box>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</Box>
);
}
【讨论】: