| 1 |
import { Children, Fragment, isValidElement } from 'react'; |
| 2 |
import { isCustomWizardNavItem, isWizardBasicStep, isWizardParentStep } from './types'; |
| 3 |
import { WizardProps } from './Wizard'; |
| 4 |
import { useWizardContext } from './WizardContext'; |
| 5 |
import { WizardNav, WizardNavProps } from './WizardNav'; |
| 6 |
import { WizardNavItem } from './WizardNavItem'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Hosts default wizard navigation logic by utilizing the wizard's context and WizardNav/WizardNavItem. |
| 10 |
* This component is not exposed to consumers. |
| 11 |
*/ |
| 12 |
|
| 13 |
interface WizardNavInternalProps extends Pick<WizardProps, 'isVisitRequired' | 'isProgressive'> { |
| 14 |
nav: Partial<WizardNavProps>; |
| 15 |
navAriaLabel: string; |
| 16 |
isNavExpanded: boolean; |
| 17 |
} |
| 18 |
|
| 19 |
export const WizardNavInternal = ({ |
| 20 |
nav, |
| 21 |
navAriaLabel, |
| 22 |
isVisitRequired, |
| 23 |
isProgressive, |
| 24 |
isNavExpanded |
| 25 |
}: WizardNavInternalProps) => { |
| 26 |
const { activeStep, steps, goToStepByIndex } = useWizardContext(); |
| 27 |
|
| 28 |
const wizardNavProps: WizardNavProps = { |
| 29 |
isExpanded: isNavExpanded, |
| 30 |
'aria-label': navAriaLabel || nav?.['aria-label'] || 'Wizard steps', |
| 31 |
...(nav?.['aria-labelledby'] && { |
| 32 |
'aria-labelledby': nav['aria-labelledby'] |
| 33 |
}) |
| 34 |
}; |
| 35 |
|
| 36 |
return ( |
| 37 |
<WizardNav {...wizardNavProps}> |
| 38 |
{steps.map((step, stepIndex) => { |
| 39 |
const hasVisitedNextStep = steps.some((step) => step.index > stepIndex + 1 && step.isVisited); |
| 40 |
const isStepDisabled = step.isDisabled || (isVisitRequired && !step.isVisited && !hasVisitedNextStep); |
| 41 |
const customStepNavItem = isCustomWizardNavItem(step.navItem) && ( |
| 42 |
<Fragment key={step.id}> |
| 43 |
{typeof step.navItem === 'function' ? step.navItem(step, activeStep, steps, goToStepByIndex) : step.navItem} |
| 44 |
</Fragment> |
| 45 |
); |
| 46 |
|
| 47 |
if (isWizardParentStep(step) && !step.isHidden) { |
| 48 |
let firstSubStepIndex: number; |
| 49 |
let hasActiveChild = false; |
| 50 |
|
| 51 |
const subNavItems = step.subStepIds?.map((subStepId, _subStepIndex) => { |
| 52 |
const subStep = steps.find((step) => step.id === subStepId); |
| 53 |
const hasVisitedNextStep = steps.some((step) => step.index > subStep.index && step.isVisited); |
| 54 |
const isSubStepDisabled = |
| 55 |
subStep.isDisabled || (isVisitRequired && !subStep.isVisited && !hasVisitedNextStep); |
| 56 |
const customSubStepNavItem = isCustomWizardNavItem(subStep.navItem) && ( |
| 57 |
<Fragment key={subStep.id}> |
| 58 |
{typeof subStep.navItem === 'function' |
| 59 |
? subStep.navItem(subStep, activeStep, steps, goToStepByIndex) |
| 60 |
: subStep.navItem} |
| 61 |
</Fragment> |
| 62 |
); |
| 63 |
|
| 64 |
// Don't render the sub-step navigation item if the hidden property is enabled |
| 65 |
if (subStep.isHidden) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Store the first visible sub-step index so that when its parent is clicked, the first sub-step is focused |
| 70 |
if (firstSubStepIndex === undefined) { |
| 71 |
firstSubStepIndex = subStep.index; |
| 72 |
} |
| 73 |
|
| 74 |
// When a sub-step is active, use this flag to set the parent step as active (isCurrent) |
| 75 |
if (activeStep?.id === subStep.id) { |
| 76 |
hasActiveChild = true; |
| 77 |
} |
| 78 |
|
| 79 |
if (!isProgressive || (isProgressive && subStep.index <= activeStep.index)) { |
| 80 |
return ( |
| 81 |
customSubStepNavItem || ( |
| 82 |
<WizardNavItem |
| 83 |
key={subStep.id} |
| 84 |
id={subStep.id} |
| 85 |
content={subStep.name} |
| 86 |
isCurrent={activeStep?.id === subStep.id} |
| 87 |
isDisabled={isSubStepDisabled || isStepDisabled} |
| 88 |
isVisited={subStep.isVisited} |
| 89 |
stepIndex={subStep.index} |
| 90 |
onClick={() => goToStepByIndex(subStep.index)} |
| 91 |
status={subStep.status} |
| 92 |
{...subStep.navItem} |
| 93 |
/> |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
}); |
| 98 |
const hasEnabledChildren = Children.toArray(subNavItems).some( |
| 99 |
(child) => isValidElement(child) && !(child.props as any).isDisabled |
| 100 |
); |
| 101 |
|
| 102 |
if (!isProgressive || (isProgressive && step.index <= activeStep.index)) { |
| 103 |
return ( |
| 104 |
customStepNavItem || ( |
| 105 |
<WizardNavItem |
| 106 |
key={step.id} |
| 107 |
id={step.id} |
| 108 |
content={step.name} |
| 109 |
isExpandable={step.isExpandable} |
| 110 |
isCurrent={hasActiveChild} |
| 111 |
isDisabled={!hasEnabledChildren || isStepDisabled} |
| 112 |
isVisited={step.isVisited} |
| 113 |
stepIndex={firstSubStepIndex} |
| 114 |
onClick={() => goToStepByIndex(firstSubStepIndex)} |
| 115 |
status={step.status} |
| 116 |
{...step.navItem} |
| 117 |
> |
| 118 |
<WizardNav {...wizardNavProps} isInnerList> |
| 119 |
{subNavItems} |
| 120 |
</WizardNav> |
| 121 |
</WizardNavItem> |
| 122 |
) |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if ( |
| 128 |
isWizardBasicStep(step) && |
| 129 |
!step.isHidden && |
| 130 |
(!isProgressive || (isProgressive && step.index <= activeStep.index)) |
| 131 |
) { |
| 132 |
return ( |
| 133 |
customStepNavItem || ( |
| 134 |
<WizardNavItem |
| 135 |
key={step.id} |
| 136 |
id={step.id} |
| 137 |
content={step.name} |
| 138 |
isCurrent={activeStep?.id === step.id} |
| 139 |
isDisabled={isStepDisabled} |
| 140 |
isVisited={step.isVisited} |
| 141 |
stepIndex={step.index} |
| 142 |
onClick={() => goToStepByIndex(step.index)} |
| 143 |
status={step.status} |
| 144 |
{...step.navItem} |
| 145 |
/> |
| 146 |
) |
| 147 |
); |
| 148 |
} |
| 149 |
})} |
| 150 |
</WizardNav> |
| 151 |
); |
| 152 |
}; |