Display Text Only When Selected - Material-UI

Is there a way to display only the selected tab's text? I came across this code snippet in the MUI documentation that involves showing labels on selection. How can I achieve this effect, maybe by manipulating the state to change the color of the selected element?

Below is the code snippet I have been working on to implement this functionality. As a newcomer to this framework, any tips or advice would be greatly appreciated!

function TabPanel(props) {
    const { children, value, index, ...other } = props;
  
    return (
      <div
        role="tabpanel"
        hidden={value !== index}
        id={`vertical-tabpanel-${index}`}
        aria-labelledby={`vertical-tab-${index}`}
        {...other}
      >
        {value === index && (
          <Box sx={{ p: 3 }}>
            <Typography>{children}</Typography>
          </Box>
        )}
      </div>
    );
  }
  
  TabPanel.propTypes = {
    children: PropTypes.node,
    index: PropTypes.number.isRequired,
    value: PropTypes.number.isRequired,
  };
  
  function a11yProps(index) {
    return {
      id: `vertical-tab-${index}`,
      'aria-controls': `vertical-tabpanel-${index}`,
    };
  }

//

const JobPostBuild = () => {
    const [value, setValue] = React.useState(0);

    const handleChange = (event, newValue) => {
      setValue(newValue);
    };
  
    return (
      <Box
        sx={{ flexGrow: 1, bgcolor: 'gray', display: 'flex', height: 500 }} 
      >
       <Tabs
        orientation="vertical"
        variant="scrollable"
        centered
        value={value}
        onChange={handleChange}
        sx={{ borderRight: 1, borderColor: 'divider' }}
      >
        <Tab icon={<WorkIcon/>} label={"Role"} {...a11yProps(0)} />
        <Tab icon={<AccountBoxIcon/>} label="People" {...a11yProps(1)} />
        <Tab icon={<ViewCompactIcon/>} label="Build" {...a11yProps(2)} />
        <Tab icon={<PostAddIcon/>} label="Post" {...a11yProps(3)} />

      </Tabs>

      <TabPanel value={value} index={0}>
        Role
      </TabPanel>
      <TabPanel value={value} index={1}>
        People
      </TabPanel>
      <TabPanel value={value} index={2}>
        Build
      </TabPanel>
      <TabPanel value={value} index={3}>
        Post
      </TabPanel>

    </Box>
    );
  }

export default JobPostBuild

I am open to discussing this further on Discord, so feel free to reach out!

Answer №1

To determine when to display the tab label, you can use the selected tab's value as a condition:

<Tab label={value === 0 ? "Role" : ""} {...a11yProps(0)} />

Check out the demo here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to securely store my JWT Token within my application's state?

userAction.js -> Frontend, Action export const login = (userID, password) => async (dispatch) => { try { dispatch({ type: USER_LOGIN_REQUEST }); const url = "http://localhost:8080/authenticate/"; const ...

Is there a way to conceal a div class on the Payment page in Shopify?

I have encountered an issue with the code on the Shopify checkout (Payment) Page. Unfortunately, I am unable to directly edit this page within Shopify, but I have discovered that it is possible to add custom CSS or HTML code within the checkout settings of ...

Transform the componentDidUpdate method that uses prevProps into a custom hook integrated with Redux

Trying to convert a life cycle method into a hook is not working as expected. When the component mounted, if the user ID exists in local storage, the user is connected and their name is displayed in the navbar. If they disconnect and reconnect, their name ...

`Error encountered while looping through images in ReactJS using forEach`

As a newcomer to reactJS, I am currently enhancing a website with more frontend features. I have a section dedicated to displaying icons and I want to utilize a foreach loop in an external file to avoid repeated imports. The foreach loop I attempted is as ...

transitioning from font-awesome v4 to the latest version, v5

For a while now, I have been utilizing a responsive template. However, I am interested in updating its fa-module from v4 to v5. By downloading the free web package, replacing "font-awesome.min.css" with "all.min.css", and adjusting all references to the ...

Disruption of keyboard bindings occurs when Overriding Option is applied to the Base Web Menu

Update August 20: @mark_reeder and I had a follow-up discussion on the Base Web Slack. Mark informed me that it is possible to pass a component to the label prop instead of just a string. Now, rather than creating my own custom Option component, I simply p ...

The typography text exceeds the boundaries of the Material-UI CardContent

In the React Material-UI framework, I am working with a CardContent component that looks like this: <CardContent className={classes.cardContent}> <Typography component="p" className={classes.title} variant="title"> {this.props.post.title ...

How to rotate text direction using JavaScript and CSS

Despite my extensive efforts, I have been unable to find a solution to a seemingly simple problem. In JavaScript, I have generated dynamic text using the following code: context.fillText('name', 10, 10, 20); Now, I need this text to be ori ...

Eliminate the navigation bar option from a website template

Is there a way to permanently eliminate the menu button in this theme? Any guidance would be appreciated. Here's the link to the theme: https://github.com/vvalchev/creative-theme-jekyll-new ...

What is the best way to align the title of a Dialog in Material UI to

Is there a way to center the DialogTitle (Material UI) using my code snippet below? <Dialog open={this.state.open} titleStyle = {styles.Dialog} title='Title centered please!' actions={standardActions} onRequestClose={this.handle ...

Is there a way to allow only the block code to shift while keeping the other span tags stationary?

Is it possible to change the text without affecting other span tags in the code? I want to make sure only this specific text is updated. How can I achieve that? var para_values = [ { content: "BRAND " }, { content: "MISSION" } ]; functi ...

React: Encountered an expression in JSX where an assignment or function call was expected

Trying to build a left-hand menu for my test application using react. Encountering a compilation error in the JSX of one of my classes. Is it because HTML elements cannot be placed within {} scripts in JSX? If so, how do I fix this? ./src/components/Left ...

Importing all Material UI Icons simultaneously through the use of an asterisk

I need help with importing all MUI Icons simultaneously for a dynamic icon component. Whenever I try to import using an asterisk, I encounter an error. The component functions properly when importing a single Icon, but not when using an asterisk to import ...

After updating the state in a Reducer with Redux Toolkit, make sure to utilize a

Issue: Seeking efficient code writing methods. Here is a detailed example of my Redux Toolkit slice below: import { createSlice } from '@reduxjs/toolkit'; import { setCookie } from '../../utils/storageHandler'; const initialState = { ...

Leveraging the power of font awesome icons as background images within React applications

I am facing an issue with my dropdown menu. I have styled the caret using CSS background-image property, but now I would like to use a Font Awesome icon instead. I attempted to implement two different styles for the background image with no success. The d ...

What is the best way to adjust the placement of the menu icon on a webpage when utilizing a material-ui menu component?

On the attached image, the menu icon is currently displayed on the left side of the page, but I need it to appear on the right side. I've attempted to style both the div and the icon element without success. Despite extensive research online, I have ...

New feature that allows color change to be black or white depending on background color

Can anyone suggest a function from material UI that can automatically change text color to either white or black based on the background color? For example: <AppBar color="primary"> <Toolbar> <Typography color="i ...

Issue with the Background not displaying properly in React.js

For some reason, the background image is not showing up on the Contact Us page, even though it appears correctly on other pages. Strangely, the div is displaying the correct style when checked in the console. <div style={{backgroundImage: "url(imag ...

Guide to retrieving a file from the /data/data/ folder within the designated Application Package name directory on an Android device using react native

Despite my efforts to download the file, it seems to be saving in a different path /storage/emulated/0/Android/data/[Application Package name folder]/files instead of /data/data/[Application Package name folder]/files. I attempted to use both the rn-fetch ...

Resetting CSS animations using animation-delay

My aim is to animate a series of images on my landing page using Next.js and SCSS. The issue arises when I try to add dynamic animations that reset after each cycle. The delay in the animation causes the reset to also be delayed, which disrupts the flow. I ...