Open In App

React MUI StepIcon API

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 StepIcon API. The Steppers are used to convey numbers in some steps. StepIcon is used to assign the icon to the Stepper. The API provides a lot of functionality and we are going to learn to implement them.

Import StepIcon API

import StepIcon from '@mui/material/StepIcon';
// or
import { StepIcon } from '@mui/material';

Props List: Here is the list of 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): This overrides the existing styles or adds new styles to the component.
  • sx (Array<func/object/bool> func/object): The system prop allows defining system overrides as well as additional CSS styles. 
  • active (bool): If true, StepIcon is active. The default value is false.
  • completed (bool): If true, the step is marked as completed. The default value is false.
  • error (Array<func/object/bool> func/object): If true, the step is marked as failed.
  • icon (node): It is the label displayed in the step icon.

CSS Rules:

  • root (.MuiStepIcon-root): It is the style applied to the root element.
  • text (.MuiStepIcon-text): It is the style applied to the SVG text element.
  • active (.Mui-active): It is the state class applied to the root element if active is set to true.
  • completed (.Mui-completed): It is the state class applied to the root element if completed is set to true.
  • error (.Mui-error): It is the state class applied to the root element if the error is set to true.

Syntax: Create a StepIcon in Stepper as follows:

<Step>
      <StepIcon icon={<HtmlIcon />}></StepIcon>
</Step>

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 @mui/icons-material

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have Stepper with StepIcon.

App.js




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Stepper from "@mui/material/Stepper";
import Step from "@mui/material/Step";
import StepLabel from "@mui/material/StepLabel";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import { StepIcon } from "@mui/material";
import { Javascript } from "@mui/icons-material";
import HtmlIcon from "@mui/icons-material/Html";
import CssIcon from "@mui/icons-material/Css";
  
const steps = [
    "Select campaign settings",
    "Create an ad group",
    "Create an ad",
];
  
function App() {
    const [activeStep, setActiveStep] = React.useState(0);
    const handleNext = () => {
        setActiveStep((prevActiveStep) => prevActiveStep + 1);
    };
  
    const handleBack = () => {
        setActiveStep((prevActiveStep) => prevActiveStep - 1);
    };
  
    const handleReset = () => {
        setActiveStep(0);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI StepIcon API</strong>
            </div>
            <br />
            <Box sx={{ width: "80%", margin: "auto" }}>
                <Stepper activeStep={activeStep}>
                    <Step>
                        <StepLabel>HTML</StepLabel>
                        <StepIcon icon={<HtmlIcon />}></StepIcon>
                    </Step>
                    <Step>
                        <StepLabel>CSS</StepLabel>
                        <StepIcon icon={<CssIcon />}></StepIcon>
                    </Step>
                    <Step>
                        <StepLabel>Javascript</StepLabel>
                        <StepIcon icon={<Javascript />}></StepIcon>
                    </Step>
                </Stepper>
                {activeStep === steps.length ? (
                    <React.Fragment>
                        <Typography sx={{ mt: 2, mb: 1 }}>
                            All steps completed
                        </Typography>
                        <Box sx={{ 
                            display: "flex"
                            flexDirection: "row"
                            pt: 2 
                        }}>
                            <Box sx={{ flex: "1 1 auto" }} />
                            <Button onClick={handleReset}>
                                Reset
                            </Button>
                        </Box>
                    </React.Fragment>
                ) : (
                    <React.Fragment>
                        <Typography sx={{ mt: 2, mb: 1 }}>
                            Step {activeStep + 1}
                        </Typography>
                        <Box sx={{ 
                            display: "flex"
                            flexDirection: "row"
                            pt: 2 
                        }}>
                            <Button
                                color="inherit"
                                disabled={activeStep === 0}
                                onClick={handleBack}
                                sx={{ mr: 1 }}
                            >
                                Back
                            </Button>
                            <Box sx={{ flex: "1 1 auto" }} />
  
                            <Button onClick={handleNext}>
                                {activeStep === steps.length - 1 ? 
                                    "Finish" : "Next"}
                            </Button>
                        </Box>
                    </React.Fragment>
                )}
            </Box>
        </div>
    );
}
  
export default App;


Output:

 

Example 2: In the following example, one step is marked as an error.

App.js




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Stepper from "@mui/material/Stepper";
import Step from "@mui/material/Step";
import StepLabel from "@mui/material/StepLabel";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import { StepIcon } from "@mui/material";
import { Javascript } from "@mui/icons-material";
import HtmlIcon from "@mui/icons-material/Html";
import CssIcon from "@mui/icons-material/Css";
  
const steps = [
    "Select campaign settings",
    "Create an ad group",
    "Create an ad",
];
  
function App() {
    const [activeStep, setActiveStep] = React.useState(0);
    const handleNext = () => {
        setActiveStep((prevActiveStep) => prevActiveStep + 1);
    };
  
    const handleBack = () => {
        setActiveStep((prevActiveStep) => prevActiveStep - 1);
    };
  
    const handleReset = () => {
        setActiveStep(0);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI StepIcon API</strong>
            </div>
            <br />
            <Box sx={{ width: "80%", margin: "auto" }}>
                <Stepper activeStep={activeStep}>
                    <Step>
                        <StepLabel error>
                            <StepIcon 
                                icon={<HtmlIcon />}>
                            </StepIcon>
                        </StepLabel>
                    </Step>
                    <Step>
                        <StepLabel>
                            <StepIcon 
                                icon={<CssIcon />}>
                            </StepIcon>
                        </StepLabel>
                    </Step>
                    <Step>
                        <StepLabel>
                            <StepIcon 
                                icon={<Javascript />}>
                            </StepIcon>
                        </StepLabel>
                    </Step>
                </Stepper>
                {activeStep === steps.length ? (
                    <React.Fragment>
                        <Typography sx={{ mt: 2, mb: 1 }}>
                            All steps completed
                        </Typography>
                        <Box sx={{ 
                            display: "flex"
                            flexDirection: "row"
                            pt: 2 
                        }}>
                            <Box sx={{ flex: "1 1 auto" }} />
                            <Button onClick={handleReset}>
                                Reset
                            </Button>
                        </Box>
                    </React.Fragment>
                ) : (
                    <React.Fragment>
                        <Typography sx={{ mt: 2, mb: 1 }}>
                            Step {activeStep + 1}
                        </Typography>
                        <Box sx={{ 
                            display: "flex"
                            flexDirection: "row"
                            pt: 2 
                        }}>
                            <Button
                                color="inherit"
                                disabled={activeStep === 0}
                                onClick={handleBack}
                                sx={{ mr: 1 }}
                            >
                                Back
                            </Button>
                            <Box sx={{ flex: "1 1 auto" }} />
  
                            <Button onClick={handleNext}>
                                {activeStep === steps.length - 1 
                                    ? "Finish" : "Next"}
                            </Button>
                        </Box>
                    </React.Fragment>
                )}
            </Box>
        </div>
    );
}
  
export default App;


Output:

 

Reference: https://mui.com/material-ui/api/step-icon/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads