reactjs 是否可以有条件地隐藏和显示MUI选项卡标题?

3lxsmp7m  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(143)

请建议如何有条件地隐藏或显示MUI选项卡标题?
我正在尝试隐藏我的多用户界面标签标题有条件的悬停在一个特定的区域或点击另一个按钮,这是可行的吗?Maven,请建议一个可行的方法,如果可能的话?

2eafrhcq

2eafrhcq1#

添加了切换选项卡显示的状态。修改了https://mui.com/material-ui/react-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>
  );
}

相关问题