|
| 1 | +import { StepHeader } from './StepHeader' |
| 2 | +import { getStepStatus, isStepClickable } from './utils' |
| 3 | +import type { Step } from './types' |
| 4 | + |
| 5 | +export interface StepperProps { |
| 6 | + steps: Step[] |
| 7 | + currentStep: number |
| 8 | + completedSteps: number[] |
| 9 | + onStepChange: (step: number) => void |
| 10 | + onStepComplete: (step: number) => void |
| 11 | + showContinueButton?: boolean |
| 12 | + continueButtonText?: string |
| 13 | +} |
| 14 | + |
| 15 | +export function Stepper({ |
| 16 | + steps, |
| 17 | + currentStep, |
| 18 | + completedSteps, |
| 19 | + onStepChange, |
| 20 | + onStepComplete, |
| 21 | + showContinueButton = true, |
| 22 | + continueButtonText = 'Continue', |
| 23 | +}: StepperProps) { |
| 24 | + const handleGoToStep = (step: number) => { |
| 25 | + if (isStepClickable(step, currentStep, completedSteps)) { |
| 26 | + onStepChange(step) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const continueToNextStep = () => { |
| 31 | + const currentStepConfig = steps.find(s => s.id === currentStep) |
| 32 | + const isValid = currentStepConfig?.validate ? currentStepConfig.validate() : true |
| 33 | + |
| 34 | + if (isValid) { |
| 35 | + onStepComplete(currentStep) |
| 36 | + if (currentStep < steps.length) { |
| 37 | + onStepChange(currentStep + 1) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="space-y-0"> |
| 44 | + {steps.map((step) => { |
| 45 | + const status = getStepStatus(step.id, currentStep, completedSteps) |
| 46 | + const isActive = status === 'active' |
| 47 | + const clickable = isStepClickable(step.id, currentStep, completedSteps) |
| 48 | + |
| 49 | + return ( |
| 50 | + <div key={step.id} className="border-b border-slate-200 last:border-b-0"> |
| 51 | + <StepHeader |
| 52 | + number={step.id} |
| 53 | + title={step.title} |
| 54 | + status={status} |
| 55 | + summary={step.summary} |
| 56 | + onClick={() => handleGoToStep(step.id)} |
| 57 | + isClickable={clickable} |
| 58 | + /> |
| 59 | + |
| 60 | + {/* Step content */} |
| 61 | + {isActive && ( |
| 62 | + <div className="px-4 pb-6 pt-2"> |
| 63 | + <div className="ml-12"> |
| 64 | + {step.content} |
| 65 | + |
| 66 | + {/* Continue button */} |
| 67 | + {showContinueButton && currentStep < steps.length && ( |
| 68 | + <div className="mt-6"> |
| 69 | + <button |
| 70 | + type="button" |
| 71 | + onClick={continueToNextStep} |
| 72 | + className="px-5 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium" |
| 73 | + > |
| 74 | + {continueButtonText} |
| 75 | + </button> |
| 76 | + </div> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + )} |
| 81 | + </div> |
| 82 | + ) |
| 83 | + })} |
| 84 | + </div> |
| 85 | + ) |
| 86 | +} |
0 commit comments