Introduction

In this article, we will discuss React Material UI Stepper and Tabs. Stepper is a component that is used to create a wizard-like workflow by separating content into different steps.
You can check my previous article in which we discussed ReactJS and its basic components from the below-mentioned links.
Prerequisites

Create ReactJS project

Let's first create a React application using the following command.
  1. npx create-react-app platform

Install Material-UI

Now Install Material-UI by using the following command.
  1. npm install @material-ui/core --save
Now, right-click on the "src" folder and add a new component named 'Stepper.js'
First let’s add the following reference in this component.
  1. import Stepper from '@material-ui/core/Stepper';
  2. import Step from '@material-ui/core/Step';
  3. import StepLabel from '@material-ui/core/StepLabel';
  4. import Button from '@material-ui/core/Button';
  5. import Typography from '@material-ui/core/Typography';
  6. import AppBar from '@material-ui/core/AppBar';
  7. import Toolbar from '@material-ui/core/Toolbar';
Add the following code in Stepper component.
  1. import React from 'react';
  2. import { makeStyles } from '@material-ui/core/styles';
  3. import Stepper from '@material-ui/core/Stepper';
  4. import Step from '@material-ui/core/Step';
  5. import StepLabel from '@material-ui/core/StepLabel';
  6. import Button from '@material-ui/core/Button';
  7. import Typography from '@material-ui/core/Typography';
  8. import AppBar from '@material-ui/core/AppBar';
  9. import Toolbar from '@material-ui/core/Toolbar';
  10. function getSteps() {
  11. return ['Personal Details', 'Address', 'Review Details'];
  12. }
  13. function getStepContent(step) {
  14. switch (step) {
  15. case 0:
  16. return 'Personal Details';
  17. case 1:
  18. return 'Address';
  19. case 2:
  20. return 'Review Details';
  21. default:
  22. return 'Unknown step';
  23. }
  24. }
  25. export default function StepperDemo() {
  26. const [activeStep, setActiveStep] = React.useState(0);
  27. const [skipped, setSkipped] = React.useState(new Set());
  28. const steps = getSteps();
  29. const isStepOptional = step => {
  30. return step === 1;
  31. };
  32. const isStepSkipped = step => {
  33. return skipped.has(step);
  34. };
  35. const handleNext = () => {
  36. let newSkipped = skipped;
  37. if (isStepSkipped(activeStep)) {
  38. newSkipped = new Set(newSkipped.values());
  39. newSkipped.delete(activeStep);
  40. }
  41. setActiveStep(prevActiveStep => prevActiveStep + 1);
  42. setSkipped(newSkipped);
  43. };
  44. const handleBack = () => {
  45. setActiveStep(prevActiveStep => prevActiveStep - 1);
  46. };
  47. const handleSkip = () => {
  48. if (!isStepOptional(activeStep)) {
  49. throw new Error("You can't skip a step that isn't optional.");
  50. }
  51. setActiveStep(prevActiveStep => prevActiveStep + 1);
  52. setSkipped(prevSkipped => {
  53. const newSkipped = new Set(prevSkipped.values());
  54. newSkipped.add(activeStep);
  55. return newSkipped;
  56. });
  57. };
  58. const handleReset = () => {
  59. setActiveStep(0);
  60. };
  61. return (
  62. <>
  63. <AppBar position="static">
  64. <Toolbar style={{ 'paddingLeft': "600px" }}>
  65. Stepper in React Application
  66. </Toolbar>
  67. </AppBar>
  68. <div>
  69. <Stepper activeStep={activeStep}>
  70. {steps.map((label, index) => {
  71. const stepProps = {};
  72. const labelProps = {};
  73. if (isStepOptional(index)) {
  74. labelProps.optional = <Typography variant="caption">Optional</Typography>;
  75. }
  76. if (isStepSkipped(index)) {
  77. stepProps.completed = false;
  78. }
  79. return (
  80. <Step key={label} {...stepProps}>
  81. <StepLabel {...labelProps}>{label}</StepLabel>
  82. </Step>
  83. );
  84. })}
  85. </Stepper>
  86. <div>
  87. {activeStep === steps.length ? (
  88. <div>
  89. <Typography >
  90. All steps completed - you're finished
  91. </Typography>
  92. <Button onClick={handleReset} >
  93. Reset
  94. </Button>
  95. </div>
  96. ) : (
  97. <div>
  98. <Typography >{getStepContent(activeStep)}</Typography>
  99. <div>
  100. <Button disabled={activeStep === 0} onClick={handleBack} >
  101. Back
  102. </Button>
  103. {isStepOptional(activeStep) && (
  104. <Button
  105. variant="contained"
  106. color="primary"
  107. onClick={handleSkip}
  108. >
  109. Skip
  110. </Button>
  111. )}
  112. <Button
  113. variant="contained"
  114. color="primary"
  115. onClick={handleNext}
  116. >
  117. {activeStep === steps.length - 1 ? 'Finish' : 'Next'}
  118. </Button>
  119. </div>
  120. </div>
  121. )}
  122. </div>
  123. </div>
  124. </>
  125. );
  126. }
Now open app.js file and add the following code.
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import StepperDemo from './Stepper';
  5. import TabsDemo from './Tabs';
  6. function App() {
  7. return (
  8. <div className="App">
  9. <StepperDemo/>
  10. </div>
  11. );
  12. }
  13. export default App;
Now run the project by using 'npm start' and check the result.
Now let's add another component 'Tabs.js' file,in this component we add Tabs. Tabs is used to switch between different pages. Open Tabs.js and add the following code.
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import AppBar from '@material-ui/core/AppBar';
  4. import Tabs from '@material-ui/core/Tabs';
  5. import Tab from '@material-ui/core/Tab';
  6. import Typography from '@material-ui/core/Typography';
  7. import Box from '@material-ui/core/Box';
  8. function TabPanel(props) {
  9. const { children, value, index, ...other } = props;
  10. return (
  11. <Typography
  12. component="div"
  13. role="tabpanel"
  14. hidden={value !== index}
  15. id={`simple-tabpanel-${index}`}
  16. aria-labelledby={`simple-tab-${index}`}
  17. {...other}
  18. >
  19. {value === index && <Box p={3}>{children}</Box>}
  20. </Typography>
  21. );
  22. }
  23. TabPanel.propTypes = {
  24. children: PropTypes.node,
  25. index: PropTypes.any.isRequired,
  26. value: PropTypes.any.isRequired,
  27. };
  28. function testProps(index) {
  29. return {
  30. id: `simple-tab-${index}`,
  31. 'aria-controls': `simple-tabpanel-${index}`,
  32. };
  33. }
  34. export default function TabsDemo() {
  35. const [value, setValue] = React.useState(0);
  36. const handleChange = (event, newValue) => {
  37. setValue(newValue);
  38. };
  39. return (
  40. <div>
  41. <AppBar position="static">
  42. <Tabs value={value} onChange={handleChange}>
  43. <Tab label="Home" {...testProps(0)} />
  44. <Tab label="About" {...testProps(1)} />
  45. <Tab label="Contact" {...testProps(2)} />
  46. </Tabs>
  47. </AppBar>
  48. <TabPanel value={value} index={0}>
  49. Home
  50. </TabPanel>
  51. <TabPanel value={value} index={1}>
  52. About
  53. </TabPanel>
  54. <TabPanel value={value} index={2}>
  55. Contact
  56. </TabPanel>
  57. </div>
  58. );
  59. }
Now open app.js file and add the following code.
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import StepperDemo from './Stepper';
  5. import TabsDemo from './Tabs';
  6. function App() {
  7. return (
  8. <div className="App">
  9. {/* <StepperDemo/> */}
  10. <TabsDemo/>
  11. </div>
  12. );
  13. }
  14. export default App;
Run the project by using 'npm start' and check the result.

Summary

In this article, we learned how we add Stepper and Tabs in React applications using React material UI.