|
| 1 | +import React, { useState, useCallback, ReactNode, useEffect } from "react"; |
| 2 | +import { Button } from "./ui/Button"; |
| 3 | +import { ChevronRight, CircleCheck } from "lucide-react"; |
| 4 | +import { cn } from "@/lib/utils"; |
| 5 | +import { Icons } from "./Icons"; |
| 6 | + |
| 7 | +interface StepProps { |
| 8 | + children: ReactNode; |
| 9 | +} |
| 10 | + |
| 11 | +export interface StepperRenderProps { |
| 12 | + updateValidity: (isValid: boolean) => void; |
| 13 | + isValid: boolean; |
| 14 | + stepIndex: number; |
| 15 | +} |
| 16 | + |
| 17 | +export interface StepData { |
| 18 | + id: string; |
| 19 | + label: string; |
| 20 | + description?: string; |
| 21 | + isValid?: boolean; |
| 22 | + isCompleted?: boolean; |
| 23 | + nextLabel?: string; |
| 24 | + disabled?: boolean; |
| 25 | + render: (props: StepperRenderProps) => ReactNode; |
| 26 | +} |
| 27 | + |
| 28 | +interface StepperProps { |
| 29 | + steps: StepData[]; |
| 30 | + initialStep?: number; |
| 31 | + className?: string; |
| 32 | + stepClassName?: string; |
| 33 | + completeLabel?: string; |
| 34 | + showArrow?: boolean; |
| 35 | + onComplete?: () => void; |
| 36 | +} |
| 37 | + |
| 38 | +export const Step = ({ children }: StepProps) => { |
| 39 | + return <>{children}</>; |
| 40 | +}; |
| 41 | + |
| 42 | +const StepRenderer = React.memo( |
| 43 | + ({ |
| 44 | + step, |
| 45 | + stepIndex, |
| 46 | + updateValidity, |
| 47 | + }: { |
| 48 | + step: StepData; |
| 49 | + stepIndex: number; |
| 50 | + updateValidity: (isValid: boolean) => void; |
| 51 | + }) => { |
| 52 | + useEffect(() => { |
| 53 | + if (step.isValid) { |
| 54 | + updateValidity(true); |
| 55 | + } |
| 56 | + }, [step.id, step.isValid]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + {step?.render({ |
| 61 | + updateValidity, |
| 62 | + isValid: !!step.isValid, |
| 63 | + stepIndex, |
| 64 | + })} |
| 65 | + </> |
| 66 | + ); |
| 67 | + }, |
| 68 | +); |
| 69 | + |
| 70 | +StepRenderer.displayName = "StepRenderer"; |
| 71 | + |
| 72 | +const safeArrayLength = (arr: any[] | null | undefined = []): number => { |
| 73 | + return Array.isArray(arr) ? arr.length : 0; |
| 74 | +}; |
| 75 | + |
| 76 | +const StepperBase = ({ |
| 77 | + steps: initialSteps, |
| 78 | + initialStep = 0, |
| 79 | + className = "", |
| 80 | + stepClassName = "", |
| 81 | + completeLabel = "Complete", |
| 82 | + showArrow = true, |
| 83 | + onComplete, |
| 84 | +}: StepperProps) => { |
| 85 | + const [currentStep, setCurrentStep] = useState(initialStep); |
| 86 | + const [steps, setSteps] = useState<StepData[]>(() => { |
| 87 | + const validSteps = Array.isArray(initialSteps) ? initialSteps : []; |
| 88 | + return validSteps.map((step) => ({ |
| 89 | + ...step, |
| 90 | + isValid: step.isValid || false, |
| 91 | + isCompleted: step.isCompleted || false, |
| 92 | + })); |
| 93 | + }); |
| 94 | + |
| 95 | + useEffect(() => { |
| 96 | + const validSteps = Array.isArray(initialSteps) ? initialSteps : []; |
| 97 | + setSteps(validSteps.map((newStep, index) => { |
| 98 | + const currentStep = steps[index]; |
| 99 | + return { |
| 100 | + ...newStep, |
| 101 | + isValid: newStep.isValid !== undefined ? newStep.isValid : (currentStep?.isValid || false), |
| 102 | + isCompleted: newStep.isCompleted !== undefined ? newStep.isCompleted : (currentStep?.isCompleted || false), |
| 103 | + }; |
| 104 | + })); |
| 105 | + }, [initialSteps]); |
| 106 | + |
| 107 | + const getCurrentStep = useCallback((): StepData | null => { |
| 108 | + const stepsLength = safeArrayLength(steps); |
| 109 | + if (stepsLength === 0) return null; |
| 110 | + return currentStep >= 0 && currentStep < stepsLength |
| 111 | + ? steps[currentStep] |
| 112 | + : null; |
| 113 | + }, [steps, currentStep]); |
| 114 | + |
| 115 | + const updateStepValidity = useCallback( |
| 116 | + (stepIndex: number, isValid: boolean): void => { |
| 117 | + const stepsLength = safeArrayLength(steps); |
| 118 | + if (!Array.isArray(steps) || stepIndex < 0 || stepIndex >= stepsLength) |
| 119 | + return; |
| 120 | + |
| 121 | + setSteps((prev) => { |
| 122 | + const safeArray = Array.isArray(prev) ? prev : []; |
| 123 | + return safeArray.map((step, idx) => |
| 124 | + idx === stepIndex ? { ...step, isValid } : step, |
| 125 | + ); |
| 126 | + }); |
| 127 | + }, |
| 128 | + [], |
| 129 | + ); |
| 130 | + |
| 131 | + const completeStep = useCallback( |
| 132 | + (stepIndex: number): void => { |
| 133 | + const stepsLength = safeArrayLength(steps); |
| 134 | + if (!Array.isArray(steps) || stepIndex < 0 || stepIndex >= stepsLength) |
| 135 | + return; |
| 136 | + |
| 137 | + setSteps((prev) => { |
| 138 | + const safeArray = Array.isArray(prev) ? prev : []; |
| 139 | + return safeArray.map((step, idx) => |
| 140 | + idx === stepIndex ? { ...step, isCompleted: true } : step, |
| 141 | + ); |
| 142 | + }); |
| 143 | + }, |
| 144 | + [], |
| 145 | + ); |
| 146 | + |
| 147 | + const nextStep = useCallback((): boolean => { |
| 148 | + const stepsLength = safeArrayLength(steps); |
| 149 | + if (stepsLength === 0 || currentStep >= stepsLength - 1) return false; |
| 150 | + |
| 151 | + const currentStepData = steps?.[currentStep]; |
| 152 | + if (!currentStepData?.isValid) return false; |
| 153 | + |
| 154 | + completeStep(currentStep); |
| 155 | + setCurrentStep((prev) => prev + 1); |
| 156 | + return true; |
| 157 | + }, [currentStep, steps, completeStep]); |
| 158 | + |
| 159 | + const handleStepClick = useCallback( |
| 160 | + (stepIndex: number): boolean => { |
| 161 | + const stepsLength = safeArrayLength(steps); |
| 162 | + if (stepsLength === 0 || stepIndex < 0 || stepIndex >= stepsLength) |
| 163 | + return false; |
| 164 | + |
| 165 | + const safeSteps = Array.isArray(steps) ? steps : []; |
| 166 | + const isAccessible = |
| 167 | + stepIndex <= currentStep + 1 && |
| 168 | + (stepIndex === 0 || |
| 169 | + safeSteps.slice(0, stepIndex).every((step) => step.isCompleted)); |
| 170 | + |
| 171 | + if (isAccessible) { |
| 172 | + setCurrentStep(stepIndex); |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + return false; |
| 177 | + }, |
| 178 | + [currentStep, steps], |
| 179 | + ); |
| 180 | + |
| 181 | + const handleNext = () => { |
| 182 | + const stepsLength = safeArrayLength(steps); |
| 183 | + const isLastStep = stepsLength > 0 && currentStep === stepsLength - 1; |
| 184 | + |
| 185 | + if (isLastStep) { |
| 186 | + if (onComplete && typeof onComplete === "function") { |
| 187 | + onComplete(); |
| 188 | + } |
| 189 | + } else { |
| 190 | + nextStep(); |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + const currentStepData = getCurrentStep(); |
| 195 | + const stepsLength = safeArrayLength(steps); |
| 196 | + const isLastStep = stepsLength > 0 && currentStep === stepsLength - 1; |
| 197 | + |
| 198 | + if (stepsLength === 0) { |
| 199 | + return null; |
| 200 | + } |
| 201 | + |
| 202 | + const safeSteps = Array.isArray(steps) ? steps : []; |
| 203 | + |
| 204 | + return ( |
| 205 | + <div className={cn("w-full flex flex-col gap-10", className)}> |
| 206 | + <div className="flex items-center gap-2 pb-3 border-b border-b-sidebar-border"> |
| 207 | + {safeSteps.map((step, index) => { |
| 208 | + const isActive = index === currentStep; |
| 209 | + const isCompleted = !!step.isCompleted; |
| 210 | + const isClickable = index <= currentStep || isCompleted; |
| 211 | + const isLastStep = index === safeSteps.length - 1; |
| 212 | + |
| 213 | + return ( |
| 214 | + <React.Fragment key={step.id || index}> |
| 215 | + <div |
| 216 | + className={`flex flex-col items-center ${isClickable ? "cursor-pointer" : "cursor-not-allowed opacity-60"} ${stepClassName}`} |
| 217 | + onClick={() => isClickable && handleStepClick(index)} |
| 218 | + > |
| 219 | + <div className="flex items-center gap-1.5"> |
| 220 | + {isCompleted ? ( |
| 221 | + <Icons.Check /> |
| 222 | + ) : ( |
| 223 | + <CircleCheck |
| 224 | + className={cn( |
| 225 | + "h-5 w-5 text-black", |
| 226 | + !isActive && "opacity-50", |
| 227 | + )} |
| 228 | + /> |
| 229 | + )} |
| 230 | + |
| 231 | + <span |
| 232 | + className={`text-sm ${isActive || isCompleted ? "font-medium" : "font-normal"}`} |
| 233 | + > |
| 234 | + {step.label} |
| 235 | + </span> |
| 236 | + </div> |
| 237 | + </div> |
| 238 | + {!isLastStep && ( |
| 239 | + <ChevronRight className="text-base-muted-foreground h-4 w-4" /> |
| 240 | + )} |
| 241 | + </React.Fragment> |
| 242 | + ); |
| 243 | + })} |
| 244 | + </div> |
| 245 | + |
| 246 | + <div className="flex flex-col gap-3.5"> |
| 247 | + {currentStepData?.description && ( |
| 248 | + <h3 className="text-sm text-base-foreground font-semibold font-inter"> |
| 249 | + {currentStepData?.description} |
| 250 | + </h3> |
| 251 | + )} |
| 252 | + {currentStepData && ( |
| 253 | + <StepRenderer |
| 254 | + step={currentStepData} |
| 255 | + stepIndex={currentStep} |
| 256 | + updateValidity={(isValid: boolean) => |
| 257 | + updateStepValidity(currentStep, isValid) |
| 258 | + } |
| 259 | + /> |
| 260 | + )} |
| 261 | + </div> |
| 262 | + |
| 263 | + <div className="flex justify-end mt-6"> |
| 264 | + {!isLastStep ? ( |
| 265 | + <Button |
| 266 | + onClick={handleNext} |
| 267 | + className="min-w-[200px]" |
| 268 | + disabled={!currentStepData?.isValid} |
| 269 | + > |
| 270 | + <div className="flex items-center"> |
| 271 | + {currentStepData?.nextLabel || "Next"} |
| 272 | + {showArrow && <ChevronRight />} |
| 273 | + </div> |
| 274 | + </Button> |
| 275 | + ) : ( |
| 276 | + <button |
| 277 | + onClick={handleNext} |
| 278 | + disabled={!currentStepData?.isValid} |
| 279 | + className="bg-gray-400 hover:bg-gray-500 text-white font-medium px-6 py-2 rounded-md disabled:opacity-50 enabled:bg-gray-600" |
| 280 | + > |
| 281 | + {completeLabel} |
| 282 | + </button> |
| 283 | + )} |
| 284 | + </div> |
| 285 | + </div> |
| 286 | + ); |
| 287 | +}; |
| 288 | + |
| 289 | +const Stepper = { |
| 290 | + displayName: "Stepper", |
| 291 | + Base: StepperBase, |
| 292 | + Step, |
| 293 | +}; |
| 294 | + |
| 295 | +export default Stepper; |
0 commit comments