| 1 |
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
| 2 |
import { isCustomWizardFooter, isWizardSubStep, WizardStepType, WizardFooterType } from './types'; |
| 3 |
import { WizardFooter, WizardFooterProps } from './WizardFooter'; |
| 4 |
|
| 5 |
export interface WizardContextProps { |
| 6 |
/** List of steps */ |
| 7 |
steps: WizardStepType[]; |
| 8 |
/** Current step */ |
| 9 |
activeStep: WizardStepType; |
| 10 |
/** Footer element */ |
| 11 |
footer: React.ReactElement<any>; |
| 12 |
/** Close the wizard */ |
| 13 |
close: () => void; |
| 14 |
/** Navigate to the next step */ |
| 15 |
goToNextStep: () => void | Promise<void>; |
| 16 |
/** Navigate to the previous step */ |
| 17 |
goToPrevStep: () => void | Promise<void>; |
| 18 |
/** Navigate to step by ID */ |
| 19 |
goToStepById: (id: number | string) => void; |
| 20 |
/** Navigate to step by name */ |
| 21 |
goToStepByName: (name: string) => void; |
| 22 |
/** Navigate to step by index */ |
| 23 |
goToStepByIndex: (index: number) => void; |
| 24 |
/** Update the footer with any react element */ |
| 25 |
setFooter: (footer: React.ReactElement<any> | Partial<WizardFooterProps>) => void; |
| 26 |
/** Get step by ID */ |
| 27 |
getStep: (stepId: number | string) => WizardStepType; |
| 28 |
/** Set step by ID */ |
| 29 |
setStep: (step: Pick<WizardStepType, 'id'> & Partial<WizardStepType>) => void; |
| 30 |
/** Flag indicating whether the wizard content should be focused after the onNext or onBack callbacks |
| 31 |
* are called. |
| 32 |
*/ |
| 33 |
shouldFocusContent: boolean; |
| 34 |
/** Ref for main wizard content element. */ |
| 35 |
mainWrapperRef: React.RefObject<HTMLElement | null>; |
| 36 |
} |
| 37 |
|
| 38 |
export const WizardContext = createContext({} as WizardContextProps); |
| 39 |
|
| 40 |
export interface WizardContextProviderProps { |
| 41 |
steps: WizardStepType[]; |
| 42 |
activeStepIndex: number; |
| 43 |
footer: WizardFooterType; |
| 44 |
children: React.ReactElement<any>; |
| 45 |
onNext(event: React.MouseEvent<HTMLButtonElement>, steps: WizardStepType[]): void; |
| 46 |
onBack(event: React.MouseEvent<HTMLButtonElement>, steps: WizardStepType[]): void; |
| 47 |
onClose?(event: React.MouseEvent<HTMLButtonElement>): void; |
| 48 |
goToStepById(steps: WizardStepType[], id: number | string): void; |
| 49 |
goToStepByName(steps: WizardStepType[], name: string): void; |
| 50 |
goToStepByIndex( |
| 51 |
event: React.MouseEvent<HTMLButtonElement> | React.MouseEvent<HTMLAnchorElement>, |
| 52 |
steps: WizardStepType[], |
| 53 |
index: number |
| 54 |
): void; |
| 55 |
shouldFocusContent: boolean; |
| 56 |
mainWrapperRef: React.RefObject<HTMLElement | null>; |
| 57 |
} |
| 58 |
|
| 59 |
export const WizardContextProvider: React.FunctionComponent<WizardContextProviderProps> = ({ |
| 60 |
steps: initialSteps, |
| 61 |
footer: initialFooter, |
| 62 |
activeStepIndex, |
| 63 |
children, |
| 64 |
onNext, |
| 65 |
onBack, |
| 66 |
onClose, |
| 67 |
goToStepById, |
| 68 |
goToStepByName, |
| 69 |
goToStepByIndex, |
| 70 |
shouldFocusContent, |
| 71 |
mainWrapperRef |
| 72 |
}) => { |
| 73 |
const [currentSteps, setCurrentSteps] = useState<WizardStepType[]>(initialSteps); |
| 74 |
const [currentFooter, setCurrentFooter] = useState<WizardFooterType>(); |
| 75 |
|
| 76 |
// Callback to update steps if the overall step number changes |
| 77 |
useEffect(() => { |
| 78 |
if (currentSteps.length !== initialSteps.length) { |
| 79 |
const newSteps = initialSteps.map((newStep) => { |
| 80 |
const currentStepMatch = currentSteps.find((step) => step.id === newStep.id); |
| 81 |
// If an existing step has the same id as a new step, carry over props |
| 82 |
if (currentStepMatch) { |
| 83 |
return { ...currentStepMatch, ...newStep }; |
| 84 |
} |
| 85 |
return newStep; |
| 86 |
}); |
| 87 |
setCurrentSteps(newSteps); |
| 88 |
} |
| 89 |
}, [currentSteps, initialSteps]); |
| 90 |
|
| 91 |
// Combined initial and current state steps |
| 92 |
const steps = useMemo( |
| 93 |
() => |
| 94 |
currentSteps.map((currentStepProps, index) => ({ |
| 95 |
...currentStepProps, |
| 96 |
...initialSteps[index] |
| 97 |
})), |
| 98 |
[initialSteps, currentSteps] |
| 99 |
); |
| 100 |
|
| 101 |
const activeStep = useMemo(() => steps.find((step) => step.index === activeStepIndex), [activeStepIndex, steps]); |
| 102 |
|
| 103 |
const close = useCallback(() => onClose?.(null), [onClose]); |
| 104 |
const goToNextStep = useCallback(() => onNext(null, steps), [onNext, steps]); |
| 105 |
const goToPrevStep = useCallback(() => onBack(null, steps), [onBack, steps]); |
| 106 |
|
| 107 |
const footer = useMemo(() => { |
| 108 |
const wizardFooter = activeStep?.footer || currentFooter || initialFooter; |
| 109 |
|
| 110 |
if (isCustomWizardFooter(wizardFooter)) { |
| 111 |
const customFooter = wizardFooter; |
| 112 |
|
| 113 |
return typeof customFooter === 'function' |
| 114 |
? customFooter(activeStep, goToNextStep, goToPrevStep, close) |
| 115 |
: customFooter; |
| 116 |
} |
| 117 |
|
| 118 |
return ( |
| 119 |
<WizardFooter |
| 120 |
activeStep={activeStep} |
| 121 |
onNext={goToNextStep} |
| 122 |
onBack={goToPrevStep} |
| 123 |
onClose={close} |
| 124 |
isBackDisabled={activeStep?.index === 1 || (isWizardSubStep(activeStep) && activeStep?.index === 2)} |
| 125 |
{...wizardFooter} |
| 126 |
/> |
| 127 |
); |
| 128 |
}, [currentFooter, initialFooter, activeStep, goToNextStep, goToPrevStep, close]); |
| 129 |
|
| 130 |
const getStep = useCallback((stepId: string | number) => steps.find((step) => step.id === stepId), [steps]); |
| 131 |
|
| 132 |
const setStep = useCallback( |
| 133 |
(step: Pick<WizardStepType, 'id'> & Partial<WizardStepType>) => |
| 134 |
setCurrentSteps((prevSteps) => |
| 135 |
prevSteps.map((prevStep) => { |
| 136 |
if (prevStep.id === step.id) { |
| 137 |
return { ...prevStep, ...step }; |
| 138 |
} |
| 139 |
return prevStep; |
| 140 |
}) |
| 141 |
), |
| 142 |
[] |
| 143 |
); |
| 144 |
|
| 145 |
return ( |
| 146 |
<WizardContext.Provider |
| 147 |
value={{ |
| 148 |
steps, |
| 149 |
activeStep, |
| 150 |
footer, |
| 151 |
close, |
| 152 |
getStep, |
| 153 |
setStep, |
| 154 |
goToNextStep, |
| 155 |
goToPrevStep, |
| 156 |
setFooter: setCurrentFooter, |
| 157 |
goToStepById: useCallback((id) => goToStepById(steps, id), [goToStepById, steps]), |
| 158 |
goToStepByName: useCallback((name) => goToStepByName(steps, name), [goToStepByName, steps]), |
| 159 |
goToStepByIndex: useCallback((index: number) => goToStepByIndex(null, steps, index), [goToStepByIndex, steps]), |
| 160 |
shouldFocusContent, |
| 161 |
mainWrapperRef |
| 162 |
}} |
| 163 |
> |
| 164 |
{children} |
| 165 |
</WizardContext.Provider> |
| 166 |
); |
| 167 |
}; |
| 168 |
|
| 169 |
WizardContextProvider.displayName = 'WizardContextProvider'; |
| 170 |
|
| 171 |
export const useWizardContext = () => useContext(WizardContext); |