import * as React from 'react'; import Box from '@mui/material/Box'; import Stepper from '@mui/material/Stepper'; import Step from '@mui/material/Step'; import StepButton from '@mui/material/StepButton'; import Button from '@mui/material/Button'; import Typography from '@mui/material/Typography'; const steps = ['Select campaign settings', 'Create an ad group', 'Create an ad']; export default function HorizontalNonLinearStepper() { const [activeStep, setActiveStep] = React.useState(0); const [completed, setCompleted] = React.useState<{ [k: number]: boolean; }>({}); const totalSteps = () => { return steps.length; }; const completedSteps = () => { return Object.keys(completed).length; }; const isLastStep = () => { return activeStep === totalSteps() - 1; }; const allStepsCompleted = () => { return completedSteps() === totalSteps(); }; const handleNext = () => { const newActiveStep = isLastStep() && !allStepsCompleted() ? // It's the last step, but not all steps have been completed, // find the first step that has been completed steps.findIndex((step, i) => !(i in completed)) : activeStep + 1; setActiveStep(newActiveStep); }; const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1); }; const handleStep = (step: number) => () => { setActiveStep(step); }; const handleComplete = () => { setCompleted({ ...completed, [activeStep]: true, }); handleNext(); }; const handleReset = () => { setActiveStep(0); setCompleted({}); }; return ( {steps.map((label, index) => ( {label} ))}
{allStepsCompleted() ? ( All steps completed - you're finished ) : ( Step {activeStep + 1} {activeStep !== steps.length && (completed[activeStep] ? ( Step {activeStep + 1} already completed ) : ( ))} )}
); }