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
Let's first create a React application using the following command.
- npx create-react-app platform
Install Material-UI
Now Install Material-UI by using the following command.
- 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.
- import Stepper from '@material-ui/core/Stepper';
- import Step from '@material-ui/core/Step';
- import StepLabel from '@material-ui/core/StepLabel';
- import Button from '@material-ui/core/Button';
- import Typography from '@material-ui/core/Typography';
- import AppBar from '@material-ui/core/AppBar';
- import Toolbar from '@material-ui/core/Toolbar';
Add the following code in Stepper component.
Now open app.js file and add the following code.
- import React from 'react';
- import logo from './logo.svg';
- import './App.css';
- import StepperDemo from './Stepper';
- import TabsDemo from './Tabs';
- function App() {
- return (
- <div className="App">
- <StepperDemo/>
- </div>
- );
- }
-
- 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.
- import React from 'react';
- import PropTypes from 'prop-types';
- import AppBar from '@material-ui/core/AppBar';
- import Tabs from '@material-ui/core/Tabs';
- import Tab from '@material-ui/core/Tab';
- import Typography from '@material-ui/core/Typography';
- import Box from '@material-ui/core/Box';
- function TabPanel(props) {
- const { children, value, index, ...other } = props;
-
- return (
- <Typography
- component="div"
- role="tabpanel"
- hidden={value !== index}
- id={`simple-tabpanel-${index}`}
- aria-labelledby={`simple-tab-${index}`}
- {...other}
- >
- {value === index && <Box p={3}>{children}</Box>}
- </Typography>
- );
- }
-
- TabPanel.propTypes = {
- children: PropTypes.node,
- index: PropTypes.any.isRequired,
- value: PropTypes.any.isRequired,
- };
- function testProps(index) {
- return {
- id: `simple-tab-${index}`,
- 'aria-controls': `simple-tabpanel-${index}`,
- };
- }
- export default function TabsDemo() {
- const [value, setValue] = React.useState(0);
-
- const handleChange = (event, newValue) => {
- setValue(newValue);
- };
-
- return (
- <div>
- <AppBar position="static">
- <Tabs value={value} onChange={handleChange}>
- <Tab label="Home" {...testProps(0)} />
- <Tab label="About" {...testProps(1)} />
- <Tab label="Contact" {...testProps(2)} />
- </Tabs>
- </AppBar>
- <TabPanel value={value} index={0}>
- Home
- </TabPanel>
- <TabPanel value={value} index={1}>
- About
- </TabPanel>
- <TabPanel value={value} index={2}>
- Contact
- </TabPanel>
- </div>
- );
- }
Now open app.js file and add the following code.
- import React from 'react';
- import logo from './logo.svg';
- import './App.css';
- import StepperDemo from './Stepper';
- import TabsDemo from './Tabs';
- function App() {
- return (
- <div className="App">
- {}
- <TabsDemo/>
- </div>
- );
- }
-
- export default App;
Run the project by using 'npm start' and check the result.
In this article, we learned how we add Stepper and Tabs in React applications using React material UI.