Tuesday, November 26, 2024
Google search engine
HomeLanguagesReact MUI TabPanel API

React MUI TabPanel API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI TabPanel API. The Tabs are used to make navigation easier and more useful. We can easily switch between the views. The TabPanel is used to place the content of the respective tab. The API provides a lot of functionality and we will learn to implement them.

Import TabPanel API:

import TabPanel from '@mui/lab/TabPanel';
// or
import { TabPanel } from '@mui/lab';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.

  • children (node): It is a component similar to the table row.
  • classes (Object): Override the existing styles or add new styles to the component.
  • component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
  • sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.

Syntax: Create a TabPanel as follows:

<TabPanel value={value} index={0}>
      Panel 1
</TabPanel>

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a Tab with TabPanel with its contents.

App.js




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Tab from "@mui/material/Tab";
import TabContext from "@mui/lab/TabContext";
import TabList from "@mui/lab/TabList";
import TabPanel from "@mui/lab/TabPanel";
function App() {
  const [value, setValue] = React.useState("1");
  
  const handleChange = (event, newValue) => {
    setValue(newValue);
  };
  
  return (
    <div className="App">
      <div
        className="head"
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <h1
          style={{
            color: "green",
          }}
        >
          neveropen
        </h1>
      </div>
      <div
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <strong>React MUI TabPanel API</strong>
      </div>
      <br />
      <div
        style={{
          margin: "auto",
          display: "flex",
          justifyContent: "space-evenly",
        }}
      >
        <Box sx={{ width: "100%", typography: "body1" }}>
          <TabContext value={value}>
            <Box sx={{ borderBottom: 1, 
                borderColor: "divider" }}>
              <TabList
                onChange={handleChange}
                aria-label="lab API tabs example"
              >
                <Tab label="Tutorial 1" value="1" />
                <Tab label="Tutorial 2" value="2" />
                <Tab label="Tutorial 3" value="3" />
              </TabList>
            </Box>
            <TabPanel value="1">Data Structures</TabPanel>
            <TabPanel value="2">Algorithms</TabPanel>
            <TabPanel value="3">Web Development</TabPanel>
          </TabContext>
        </Box>
      </div>
    </div>
  );
}
  
export default App;


Output:

 

Example 2: In the following example, we have customized on TabPanel using the sx field.

App.js




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Tab from "@mui/material/Tab";
import TabContext from "@mui/lab/TabContext";
import TabList from "@mui/lab/TabList";
import TabPanel from "@mui/lab/TabPanel";
function App() {
  const [value, setValue] = React.useState("1");
  
  const handleChange = (event, newValue) => {
    setValue(newValue);
  };
  
  return (
    <div className="App">
      <div
        className="head"
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <h1
          style={{
            color: "green",
          }}
        >
          neveropen
        </h1>
      </div>
      <div
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <strong>React MUI TabPanel API</strong>
      </div>
      <br />
      <div
        style={{
          margin: "auto",
          display: "flex",
          justifyContent: "space-evenly",
        }}
      >
        <Box sx={{ width: "100%", typography: "body1" }}>
          <TabContext value={value}>
            <Box sx={{ borderBottom: 1, 
                borderColor: "divider" }}>
              <TabList
                onChange={handleChange}
                aria-label="lab API tabs example"
              >
                <Tab label="Tutorial 1" value="1" />
                <Tab label="Tutorial 2" value="2" />
                <Tab label="Tutorial 3" value="3" />
              </TabList>
            </Box>
            <TabPanel value="1">Data Structures</TabPanel>
            <TabPanel
              value="2"
              sx={{
                backgroundColor: "lightgreen",
              }}
            >
              Algorithms
            </TabPanel>
            <TabPanel value="3">Web Development</TabPanel>
          </TabContext>
        </Box>
      </div>
    </div>
  );
}
  
export default App;


Output:

 

Reference: https://mui.com/material-ui/api/tab-panel/

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments