How to create mui dialog with tabs in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To create mui dialog with tabs in react js, you can use <Tabs> in dialog. it will create mui dialog with tabs in React JS.

Today, I am going to show you, How to create mui dialog with tabs in react js

Installation

Install the following packages to use mui dialog in react js.

npm

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

yarn

yarn add @mui/material @emotion/react @emotion/styled

Table of contents

  • Install MUI and create a new React app.
  • Import Material-UI dialog.
  • Use the dialog Component

Step 1: Install MUI and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import Material-UI dialog.

After installing MUI, you have to import your React component. To do this, add the following line to the top of your component file.

import React, { useState } from "react";
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  Tab,
  Tabs,
  Typography,
  Box
} from "@mui/material";
import PropTypes from "prop-types";

Step 3: Use the dialog Component.

You can use the dialog component in your react js. For example, A Dialog is a type of modal window that appears in front of app content to provide critical information or ask for a decision. Dialogs disable all app functionality when they appear, and remain on screen until confirmed, dismissed, or a required action has been taken. You can use <Dialog> component make mui dialog button in react js.

<Dialog open={open} onClose={handleClose} fullWidth maxWidth="md">
  <DialogTitle>
    Dialog with Tabs
      <Tabs
        value={value}
        onChange={handleChange}
        indicatorColor="primary"
        textColor="primary"
          centered
          >
            <Tab label="Tab 1" />
            <Tab label="Tab 2" />
            <Tab label="Tab 3" />
          </Tabs>
        </DialogTitle>
        <DialogContent>
          <TabPanel value={value} index={0}>
            <Typography>Content for Tab 1</Typography>
          </TabPanel>
          <TabPanel value={value} index={1}>
            <Typography>Content for Tab 2</Typography>
          </TabPanel>
          <TabPanel value={value} index={2}>
            <Typography>Content for Tab 3</Typography>
          </TabPanel>
    </DialogContent>
    <DialogActions>
    <Button onClick={handleClose} color="primary">
      Close
    </Button>
  </DialogActions>
</Dialog>

MUI material create mui dialog with tabs example.

The below code is an example, you need to import dialog Component. Then, you can create a dialog and you can use <Tabs>. Then it will create mui dialog with tabs in react js.

App.js

import React, { useState } from "react";
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  Tab,
  Tabs,
  Typography,
  Box
} from "@mui/material";
import PropTypes from "prop-types";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`tabpanel-${index}`}
      aria-labelledby={`tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box sx={{ p: 3 }}>
          <Typography component="div">{children}</Typography>
        </Box>
      )}
    </div>
  );
}

TabPanel.propTypes = {
  children: PropTypes.node,
  index: PropTypes.number.isRequired,
  value: PropTypes.number.isRequired
};

function MyDialog() {
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(0); // To manage the active tab index

  const handleOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div>
      <Button variant="outlined" onClick={handleOpen}>
        Open Dialog
      </Button>
      <Dialog open={open} onClose={handleClose} fullWidth maxWidth="md">
        <DialogTitle>
          Dialog with Tabs
          <Tabs
            value={value}
            onChange={handleChange}
            indicatorColor="primary"
            textColor="primary"
            centered
          >
            <Tab label="Tab 1" />
            <Tab label="Tab 2" />
            <Tab label="Tab 3" />
          </Tabs>
        </DialogTitle>
        <DialogContent>
          <TabPanel value={value} index={0}>
            <Typography>Content for Tab 1</Typography>
          </TabPanel>
          <TabPanel value={value} index={1}>
            <Typography>Content for Tab 2</Typography>
          </TabPanel>
          <TabPanel value={value} index={2}>
            <Typography>Content for Tab 3</Typography>
          </TabPanel>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            Close
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

export default MyDialog;

In the above code example, I have used the @mui/material component and created mui dialog with tabs in react js.

Check the output of the above code example.

React, dialog-with-tabs

Here, we are provided code sandbox links for the above program create mui dialog with tabs in react js. Then you can use whenever you want and do the changes as per your requirements.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts