| 1 |
import { Children, isValidElement } from 'react'; |
| 2 |
import { isWizardParentStep, isWizardSubStep, WizardStepType } from './types'; |
| 3 |
import { WizardStep, WizardStepProps } from './WizardStep'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Accumulate list of step & sub-step props pulled from child components |
| 7 |
* @param children |
| 8 |
* @returns WizardStepType[] |
| 9 |
*/ |
| 10 |
export const buildSteps = (children: React.ReactNode) => |
| 11 |
Children.toArray(children).reduce((acc: WizardStepType[], child: React.ReactNode, index: number) => { |
| 12 |
if (isWizardStep(child)) { |
| 13 |
const { props: childProps } = child; |
| 14 |
const { steps: childStepComponents, id } = childProps; |
| 15 |
const stepIndex = acc.length + 1; |
| 16 |
const subSteps: WizardStepType[] = []; |
| 17 |
|
| 18 |
acc.push( |
| 19 |
{ |
| 20 |
index: stepIndex, |
| 21 |
component: child, |
| 22 |
...(index === 0 && !childStepComponents?.length && { isVisited: true }), |
| 23 |
...(childStepComponents && { |
| 24 |
subStepIds: childStepComponents?.map((childStepComponent, subStepIndex) => { |
| 25 |
subSteps.push({ |
| 26 |
index: stepIndex + subStepIndex + 1, |
| 27 |
component: childStepComponent, |
| 28 |
parentId: id, |
| 29 |
...(index === 0 && subStepIndex === 0 && { isVisited: true }), |
| 30 |
...normalizeStepProps(childStepComponent.props) |
| 31 |
}); |
| 32 |
|
| 33 |
return childStepComponent.props.id; |
| 34 |
}) |
| 35 |
}), |
| 36 |
...normalizeStepProps(childProps) |
| 37 |
}, |
| 38 |
...subSteps |
| 39 |
); |
| 40 |
} else { |
| 41 |
throw new Error('Wizard only accepts children of type WizardStep.'); |
| 42 |
} |
| 43 |
|
| 44 |
return acc; |
| 45 |
}, []); |
| 46 |
|
| 47 |
export function isWizardStep( |
| 48 |
child: any | React.ReactElement<WizardStepProps> |
| 49 |
): child is React.ReactElement<WizardStepProps> { |
| 50 |
return isValidElement(child) && (child as React.ReactElement<WizardStepProps>).type === WizardStep; |
| 51 |
} |
| 52 |
|
| 53 |
// Omit "children" and "steps" when building steps for the Wizard's context |
| 54 |
export const normalizeStepProps = ({ |
| 55 |
children: _children, |
| 56 |
steps: _steps, |
| 57 |
...controlStep |
| 58 |
}: WizardStepProps): Omit<WizardStepType, 'index'> => controlStep; |
| 59 |
|
| 60 |
/** |
| 61 |
* Determines whether a step is navigable based on disabled/hidden properties |
| 62 |
* @param steps All steps |
| 63 |
* @param step |
| 64 |
* @returns boolean |
| 65 |
*/ |
| 66 |
export const isStepEnabled = (steps: WizardStepType[], step: WizardStepType): boolean => { |
| 67 |
// Skip over parent steps since they are merely containers of sub-steps |
| 68 |
if (!isWizardParentStep(step) && !step.isHidden && !step.isDisabled) { |
| 69 |
if (isWizardSubStep(step)) { |
| 70 |
const parentStep = steps.find((otherStep) => otherStep.id === step.parentId); |
| 71 |
|
| 72 |
if (!parentStep.isHidden && !parentStep.isDisabled) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
} else { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
}; |