From 86cb2a139a68c983fecc15f8ec333623a4060cf1 Mon Sep 17 00:00:00 2001 From: Daniel B Date: Wed, 1 Oct 2025 13:26:39 +0200 Subject: [PATCH] create stepper component --- components/stepper/stepper.tsx | 95 +++++++++++++++++++++++++++++----- components/stepper/steps.ts | 12 +++-- hooks/useStepper.ts | 41 +++++++++++---- pages/index.tsx | 33 ++++++------ 4 files changed, 141 insertions(+), 40 deletions(-) diff --git a/components/stepper/stepper.tsx b/components/stepper/stepper.tsx index fe3602e..0e1f11a 100644 --- a/components/stepper/stepper.tsx +++ b/components/stepper/stepper.tsx @@ -1,14 +1,83 @@ -interface StepperProps {} - -export default function Stepper() { - /*TODO: Replace this with the actual Stepper implementation*/ - return ( -
- {""} -
- ); +import { steps } from "./steps"; + +interface StepperProps { + currentStep: number; + onStepClick: (stepId: number) => void; } + +const Stepper: React.FC = ({ currentStep, onStepClick }) => { + const getStepStatus = ( + stepId: number + ): "completed" | "current" | "upcoming" => { + if (stepId < currentStep) return "completed"; + if (stepId === currentStep) return "current"; + return "upcoming"; + }; + + const isClickable = (stepId: number): boolean => { + return stepId <= currentStep; + }; + + return ( +
+
+ {steps.map((step, index) => { + const status = getStepStatus(step.id); + const clickable = isClickable(step.id); + const isLast = index === steps.length - 1; + + return ( +
+
+ + + + {step.label} + +
+ + {!isLast && ( +
+ )} +
+ ); + })} +
+
+ ); +}; + +export default Stepper; diff --git a/components/stepper/steps.ts b/components/stepper/steps.ts index d840d60..5e80cdf 100644 --- a/components/stepper/steps.ts +++ b/components/stepper/steps.ts @@ -1,5 +1,11 @@ -interface Step { - title: string; +export interface Step { + id: number; + label: string; } -export const steps: Step[] = []; +export const steps: Step[] = [ + { id: 1, label: "Service" }, + { id: 2, label: "Termin" }, + { id: 3, label: "Fahrzeug" }, + { id: 4, label: "Kontakt" }, +]; diff --git a/hooks/useStepper.ts b/hooks/useStepper.ts index 588deeb..6621d05 100644 --- a/hooks/useStepper.ts +++ b/hooks/useStepper.ts @@ -1,11 +1,34 @@ import { useState } from "react"; +import { steps } from "../components/stepper/steps"; -export default function useStepper() { - const [currentStep, setCurrentStep] = useState(0); - function handleNextStep() { - setCurrentStep((prev) => { - return prev + 1; - }); - } - return { currentStep, handleNextStep }; -} +const useStepper = () => { + const [currentStep, setCurrentStep] = useState(1); + + const handleNextStep = () => { + setCurrentStep((prev) => Math.min(prev + 1, steps.length)); + }; + + const handlePrevStep = () => { + setCurrentStep((prev) => Math.max(prev - 1, 1)); + }; + + const handleStepClick = (stepId: number) => { + if (stepId <= currentStep) { + setCurrentStep(stepId); + } + }; + + const isFirstStep = currentStep === 1; + const isLastStep = currentStep === steps.length; + + return { + currentStep, + handleNextStep, + handlePrevStep, + handleStepClick, + isFirstStep, + isLastStep, + }; +}; + +export default useStepper; diff --git a/pages/index.tsx b/pages/index.tsx index f1ad62d..d0ea9db 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -7,19 +7,22 @@ import Stepper from "../components/stepper/stepper"; import useStepper from "../hooks/useStepper"; export default function Home() { - const { currentStep, handleNextStep } = useStepper(); - return ( - <> -
- - - {/*TODO: Make sure the Stepper handles clicks on the button*/} - - - - - - - - ); + const { currentStep, handleNextStep, handleStepClick, isLastStep } = + useStepper(); + + return ( + <> +
+ + + + + {!isLastStep && ( + + + + )} + + + ); }