| 1 |
import { useEffect } from 'react'; |
| 2 |
import { isWizardParentStep, WizardNavItemType } from './types'; |
| 3 |
import { WizardBodyProps } from './WizardBody'; |
| 4 |
import { useWizardContext } from './WizardContext'; |
| 5 |
import { WizardFooterProps } from './WizardFooter'; |
| 6 |
|
| 7 |
/** |
| 8 |
* The primary child component for Wizard. Step props are used for the list of steps managed in context. |
| 9 |
*/ |
| 10 |
|
| 11 |
export interface WizardStepProps { |
| 12 |
/** Name of the step's navigation item */ |
| 13 |
name: React.ReactNode; |
| 14 |
/** Unique identifier */ |
| 15 |
id: string | number; |
| 16 |
/** Optional for when the step is used as a parent to sub-steps */ |
| 17 |
children?: React.ReactNode | undefined; |
| 18 |
/** Props for WizardBody that wraps content by default. */ |
| 19 |
body?: Omit<Omit<WizardBodyProps, 'children'>, 'children'>; |
| 20 |
/** Optional list of sub-steps */ |
| 21 |
steps?: React.ReactElement<WizardStepProps>[]; |
| 22 |
/** Flag to disable the step's navigation item */ |
| 23 |
isDisabled?: boolean; |
| 24 |
/** Flag to determine whether the step is hidden */ |
| 25 |
isHidden?: boolean; |
| 26 |
/** Replaces the step's navigation item or its properties. */ |
| 27 |
navItem?: WizardNavItemType; |
| 28 |
/** Replaces the step's footer. The step's footer takes precedence over the wizard's footer. */ |
| 29 |
footer?: React.ReactElement<any> | Partial<WizardFooterProps>; |
| 30 |
/** Used to determine icon next to the step's navigation item */ |
| 31 |
status?: 'default' | 'error' | 'success' | 'warning'; |
| 32 |
/** Flag to determine whether parent steps can expand or not. Defaults to false. */ |
| 33 |
isExpandable?: boolean; |
| 34 |
} |
| 35 |
|
| 36 |
export const WizardStep = ({ children, steps: _subSteps, ...props }: WizardStepProps) => { |
| 37 |
const { activeStep, setStep } = useWizardContext(); |
| 38 |
const { id, name, body, isDisabled, isHidden, navItem, footer, status } = props; |
| 39 |
const isParentStep = isWizardParentStep(activeStep); |
| 40 |
|
| 41 |
// Update step in context when props change or when the step is active has yet to be marked as visited. |
| 42 |
useEffect(() => { |
| 43 |
setStep({ |
| 44 |
id, |
| 45 |
name, |
| 46 |
...(body && { body }), |
| 47 |
...(isDisabled && { isDisabled }), |
| 48 |
...(isHidden && { isHidden }), |
| 49 |
...(navItem && { navItem }), |
| 50 |
...(footer && { footer }), |
| 51 |
...(status && { status }), |
| 52 |
...(!isParentStep && id === activeStep?.id && !activeStep?.isVisited && { isVisited: true }) |
| 53 |
}); |
| 54 |
}, [ |
| 55 |
body, |
| 56 |
footer, |
| 57 |
id, |
| 58 |
isDisabled, |
| 59 |
isHidden, |
| 60 |
name, |
| 61 |
navItem, |
| 62 |
status, |
| 63 |
isParentStep, |
| 64 |
setStep, |
| 65 |
activeStep?.id, |
| 66 |
activeStep?.isVisited |
| 67 |
]); |
| 68 |
|
| 69 |
return <>{children}</>; |
| 70 |
}; |
| 71 |
|
| 72 |
WizardStep.displayName = 'WizardStep'; |