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'; const steps = ['Select campaign settings', 'Create an ad group', 'Create an ad']; export default function HorizontalLinearStepper() { const [activeStep, setActiveStep] = React.useState(0); const [skipped, setSkipped] = React.useState(new Set()); const isStepOptional = React.useCallback((step) => { return step === 1; }, []); const isStepSkipped = (step) => { return skipped.has(step); }; const handleNext = () => { let newSkipped = skipped; if (isStepSkipped(activeStep)) { newSkipped = new Set(newSkipped.values()); newSkipped.delete(activeStep); } setActiveStep((prevActiveStep) => prevActiveStep + 1); setSkipped(newSkipped); }; const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1); }; const handleSkip = () => { if (!isStepOptional(activeStep)) { // You probably want to guard against something like this, // it should never occur unless someone's actively trying to break something. throw new Error("You can't skip a step that isn't optional."); } setActiveStep((prevActiveStep) => prevActiveStep + 1); setSkipped((prevSkipped) => { const newSkipped = new Set(prevSkipped.values()); newSkipped.add(activeStep); return newSkipped; }); }; const handleReset = () => { setActiveStep(0); }; const previousActiveStepRef = React.useRef(activeStep); const resetButtonRef = React.useRef(null); const nextButtonRef = React.useRef(null); // Manage focus when the active step changes. React.useEffect(() => { const previousActiveStep = previousActiveStepRef.current; previousActiveStepRef.current = activeStep; if (activeStep === steps.length) { // If the user has completed all steps and hits "Finish", focus the "Reset" button. resetButtonRef.current.focus(); return; } if (activeStep === 0 && previousActiveStep === steps.length) { // If the user has completed all steps and hits "Reset", focus the "Next" button. nextButtonRef.current.focus(); return; } if (isStepOptional(previousActiveStep) && !isStepOptional(activeStep)) { // If the user hits "Skip" and the next step is not optional, focus the "Next" button. nextButtonRef.current.focus(); } }, [activeStep, isStepOptional]); return ( {steps.map((label, index) => { const stepProps = {}; const labelProps = {}; if (isStepOptional(index)) { labelProps.optional = ( Optional ); } if (isStepSkipped(index)) { stepProps.completed = false; } return ( {label} ); })} {activeStep === steps.length ? ( All steps completed - you're finished ) : ( Step {activeStep + 1} {isStepOptional(activeStep) && ( )} )} ); }