|
1 |
| -import React from 'react'; |
| 1 | +import React, { Children, cloneElement, isValidElement, useState } from 'react'; |
| 2 | + |
| 3 | +import { stepContentStyles, wizardContainerStyles } from './Wizard.styles'; |
2 | 4 | import { WizardProps } from './Wizard.types';
|
3 | 5 |
|
4 |
| -export function Wizard({}: WizardProps) { |
5 |
| - return <div>your content here</div>; |
| 6 | +export function Wizard({ |
| 7 | + activeStep: controlledActiveStep, |
| 8 | + onStepChange, |
| 9 | + children, |
| 10 | +}: WizardProps) { |
| 11 | + // Internal state for uncontrolled mode |
| 12 | + const [internalActiveStep, setInternalActiveStep] = useState<number>(0); |
| 13 | + |
| 14 | + // Use controlled prop if provided, otherwise use internal state |
| 15 | + const isControlled = controlledActiveStep !== undefined; |
| 16 | + const activeStep = isControlled ? controlledActiveStep : internalActiveStep; |
| 17 | + |
| 18 | + // Handle step changes |
| 19 | + const handleStepChange = (newStep: number) => { |
| 20 | + if (!isControlled) { |
| 21 | + setInternalActiveStep(newStep); |
| 22 | + } |
| 23 | + onStepChange?.(newStep); |
| 24 | + }; |
| 25 | + |
| 26 | + // Filter children to separate steps from footer |
| 27 | + const childrenArray = Children.toArray(children); |
| 28 | + |
| 29 | + // For now, we'll look for components with displayName ending in 'Step' or 'Footer' |
| 30 | + // This will be more precise once Wizard.Step and Wizard.Footer are implemented |
| 31 | + const stepChildren = childrenArray.filter(child => { |
| 32 | + if (isValidElement(child)) { |
| 33 | + const displayName = (child.type as any)?.displayName; |
| 34 | + return displayName && displayName.includes('Step'); |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | + }); |
| 39 | + |
| 40 | + const footerChild = childrenArray.find(child => { |
| 41 | + if (isValidElement(child)) { |
| 42 | + const displayName = (child.type as any)?.displayName; |
| 43 | + return displayName && displayName.includes('Footer'); |
| 44 | + } |
| 45 | + |
| 46 | + return false; |
| 47 | + }); |
| 48 | + |
| 49 | + // Get the current step to render |
| 50 | + const currentStep = stepChildren[activeStep] || null; |
| 51 | + |
| 52 | + // Clone footer with step navigation handlers if it exists |
| 53 | + const clonedFooter = |
| 54 | + footerChild && isValidElement(footerChild) |
| 55 | + ? cloneElement(footerChild as React.ReactElement<any>, { |
| 56 | + activeStep, |
| 57 | + totalSteps: stepChildren.length, |
| 58 | + onStepChange: handleStepChange, |
| 59 | + isControlled, |
| 60 | + }) |
| 61 | + : null; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className={wizardContainerStyles}> |
| 65 | + <code>activeStep: {activeStep}</code> |
| 66 | + {/* Render current step */} |
| 67 | + <div className={stepContentStyles}>{currentStep}</div> |
| 68 | + |
| 69 | + {/* Render footer */} |
| 70 | + {clonedFooter} |
| 71 | + </div> |
| 72 | + ); |
6 | 73 | }
|
7 | 74 |
|
8 | 75 | Wizard.displayName = 'Wizard';
|
0 commit comments