network-resurrector-frontend/src/components/layout/stepper/ApplicationStepper.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-12-19 00:46:02 +02:00
import React, { useState, useEffect } from "react";
2020-12-19 01:28:53 +02:00
import steps from "../../../constants/steps";
2020-12-16 03:09:24 +02:00
import {
Card,
Stepper,
Step,
StepButton,
StepLabel,
makeStyles
} from "@material-ui/core";
import { useLocation, useHistory } from "react-router-dom";
2020-12-19 01:30:03 +02:00
import StepperConnector from "./StepperConnector";
2020-12-19 01:28:53 +02:00
import ColorlibStepIcon from "./ColorlibStepIcon";
2020-12-16 03:09:24 +02:00
const styles = () => ({
stepperCard: {
2020-12-19 01:28:53 +02:00
padding: "0px",
2020-12-16 03:25:25 +02:00
margin: "15px"
2020-12-16 03:09:24 +02:00
}
});
const useStyles = makeStyles(styles);
const ApplicationStepper = () => {
2020-12-19 00:46:02 +02:00
const firstStep = steps.find((z) => z.id === 0);
const [activeStep, setActiveStep] = useState(firstStep);
2020-12-16 03:09:24 +02:00
const classes = useStyles();
const location = useLocation();
const history = useHistory();
2020-12-19 00:46:02 +02:00
useEffect(() => {
const step = steps.find((z) => z.route === location.pathname);
if (step) {
setActiveStep(step);
}
}, [location.pathname]);
2020-12-16 03:09:24 +02:00
const handleStepClick = (step) => {
if (!step) return;
2020-12-19 00:46:02 +02:00
setActiveStep(step);
2020-12-16 03:09:24 +02:00
if (location.pathname === step.route) return;
history.push(step.route);
};
return (
<Card className={classes.stepperCard}>
2020-12-19 01:28:53 +02:00
<Stepper
activeStep={activeStep.id}
orientation="horizontal"
2020-12-19 01:30:03 +02:00
connector={<StepperConnector />}
2020-12-19 01:28:53 +02:00
>
{steps.map((step) => (
<Step key={step.id}>
<StepButton
id={"appStepper_" + step.title}
disabled={step.disabled}
onClick={() => handleStepClick(step)}
>
<StepLabel StepIconComponent={ColorlibStepIcon}>
{step.title}
</StepLabel>
</StepButton>
</Step>
))}
</Stepper>
2020-12-16 03:09:24 +02:00
</Card>
);
};
export default ApplicationStepper;