Show only three steps when in mobile view

This commit is contained in:
Hristo Nedelkov 2024-01-09 13:10:54 +02:00
parent deabb78767
commit f7f275c202
1 changed files with 31 additions and 13 deletions

View File

@ -19,6 +19,20 @@ type FormStepperProps = {
}
const FormStepper = ({ activeStep }: FormStepperProps) => {
const isStepVisible = (index: number) => {
if (activeStep === 0) {
// Show the first three steps if the active step is the first one
return index <= 2;
} else if (activeStep === steps.length - 1) {
// Show the last three steps if the active step is the last one
return index >= steps.length - 3;
} else {
// Otherwise, show the previous, current, and next steps
return index === activeStep || index === activeStep - 1 || index === activeStep + 1;
}
}
const dispatch = useDispatch()
const changeStepOnClickHandler = (index: number) => {
@ -27,6 +41,7 @@ const FormStepper = ({ activeStep }: FormStepperProps) => {
}
}
const visibleSteps = steps.filter((_, index) => isStepVisible(index));
return (
<Stepper
activeStep={activeStep}
@ -42,17 +57,20 @@ const FormStepper = ({ activeStep }: FormStepperProps) => {
overflow: 'hidden',
}}
>
{steps.map((step, index) => (
<Step
key={index}
label={step.label}
className="custom-step"
onClick={() => changeStepOnClickHandler(index)}
completed={activeStep > index - 1}
data-subtitle={step.subtitle}
data-step={step.label}
/>
))}
{visibleSteps.map((step) => {
const originalIndex = steps.indexOf(step);
return (
<Step
key={originalIndex}
label={step.label}
className="custom-step"
onClick={() => changeStepOnClickHandler(originalIndex)}
completed={activeStep > originalIndex - 1}
data-subtitle={step.subtitle}
data-step={step.label}
/>
);
})}
</Stepper>
)
}