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

aligning an icon in the middle of its container

I'm having trouble centering an icon vertically inside its box only. I've tried using vertical-align: middle; but it doesn't seem to be working. I also attempted the line-height method, but the icon has an outer border that stays attached to ...

Tips on how to align a wrapper-div in the center without affecting the positioning of the

I am looking to keep my page perfectly centered in the browser without affecting the content (similar to how align-text: center works). Specifically, I want to center my wrapper-div. How can I achieve this? Here is a simplified version of my current page ...

I am encountering a TypeScript error when using the createRef() method in React JS

Encountering some TypeScript warnings with the following dependencies: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df9f4fde8feeeffe4fdf9cdbea3b8a3bf">[email protected]</a>, <a href="/cdn-cgi/l/email-protect ...

stop cascading style sheets from being overwritten

In my ASP.NET web forms project, I am utilizing Telerik controls. The Problem In one instance, I need to retrieve HTML data from the database, which includes a lot of CSS, images, and HTML content. Here is an excerpt of my code: <telerik:RadLabel ...

Encountering a 404 error with Symfony and React Router integration

Whenever I try to refresh the site on routes other than "/" I encounter a 404 error. My setup involves Symfony and React Router. I have tried adding priority to the route, but unfortunately, it doesn't seem to resolve the issue. The code snippet be ...

How can I alter the icon's color?

Is it possible for the icon's color to change to red when the condition is greater than 0, and to gray when the condition is equal to zero? <TouchableOpacity onPress={() => { if (Object.values(selectedIt ...

Utilizing Reactjs to efficiently transmit name and value to Material UI autocomplete

I am trying to customize the Material UI Autocomplete component. I need to pass name, value and onchange similarly to how it is done for TextField. Can someone please help me achieve this? My current code is not functioning as expected. < ...

Having trouble loading background color or image in three.js

I've been struggling to load a background image or color on my webpage. Despite trying various methods, including commenting out javascript files and checking the stylesheet link, nothing seems to work. Even when I load the three.js scene with javascr ...

Error: The property 'language' is undefined and cannot be read

Struggling to execute the App-test.js file provided by React Native in the __test__ directory: import 'react-native'; import React from 'react'; import App from '../src/app'; // Note: test renderer must be required after rea ...

Switching the active className in React and Next.js based on selection status

My goal is to dynamically change the className of each Card component when clicked on. When one Card is selected, the others should revert back to their default className. This is my UserBookingData component: const UserBookingData = ({ bookings }: any) = ...

Modifying a single element within a class with Jquery

Is it possible to create a stack of pages on a website using JQuery? I found this image that shows what I'm trying to achieve: image. Instead of applying ID css for each page, I'd like to use JQuery. While researching similar questions, I came ac ...

What is the process for sending a PUT request using axios?

Currently, I am working on updating a field in my MongoDB database using the Axios PUT method within a React JS application. Additionally, I am utilizing the react form hook. const onSubmit = async itemData => { console.log(itemData); const url ...

Css shaky letters transformation

[SOLUTION] To resolve this issue, you can utilize the will-change CSS property that enforces GPU rendering: will-change: transform; [ORIGINAL QUESTION] After extensive searching on the internet, I have yet to find a solution to a seemingly simple prob ...

Using the sx attribute with a personalized component

I have a unique layout component that generally serves its purpose well, but I have encountered a few cases where I need to customize the styles. Instead of using a custom prop for toggling these styles on or off, I would prefer to simply pass the specific ...

Issues with images failing to load in React CardMedia from URLs

There seems to be an issue with the React MaterialUI component - CardMedia. The problem arises when trying to display an image using a URL provided via the image property. <CardMedia component="img" sx={{ height: 140 }} image={imag ...

Prevent updating components when modifying state variables

Introduction I've developed a React component that consists of two nested components. One is a chart (created with react-charts) and the other is a basic input field. Initially, I have set the input field to be hidden, but it becomes visible when the ...

Increasing the height of nested Bootstrap columns by stretching them out

I am trying to ensure that the first two columns in both the green and violet rows always have the same height. I initially thought of using d-flex align-items-stretch, but it seems like it is not working because those elements are not within the same row ...

Align several labels and text areas in the center

I am facing an issue with aligning multiple labels and textareas together. Currently, they line up like this: __________________ | | | | Label:|__________________| However, I want them to appear as fol ...

Creating a dynamic height sticky sidebar and footer in Bootstrap 5 using CSS/HTML

Looking to implement a sticky sidebar feature with a header and footer that remain visible while the middle content scrolls. The overall height of the page will vary, so I plan to use Bootstrap 5 grid for structuring. <!DOCTYPE html> <html la ...

Which is the best choice for a large-scale production app: caret, tilde, or a fixed package.json

I am currently managing a sizeable react application in production and I find myself undecided on whether it is more beneficial to use fixed versions for my packages. Some suggest that using the caret (^) is a safer route, but I worry that this could poten ...