| 1 |
import { useEffect, useMemo, useRef, useState } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 4 |
import wizardHeightToken from '@patternfly/react-tokens/dist/esm/c_wizard_Height'; |
| 5 |
|
| 6 |
import { |
| 7 |
isWizardParentStep, |
| 8 |
WizardStepType, |
| 9 |
isCustomWizardNav, |
| 10 |
WizardFooterType, |
| 11 |
WizardNavType, |
| 12 |
WizardStepChangeScope |
| 13 |
} from './types'; |
| 14 |
import { buildSteps, isStepEnabled } from './utils'; |
| 15 |
import { useWizardContext, WizardContextProvider } from './WizardContext'; |
| 16 |
import { WizardToggle } from './WizardToggle'; |
| 17 |
import { WizardNavInternal } from './WizardNavInternal'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Wrapper for all steps and hosts state, including navigation helpers, within context. |
| 21 |
* The WizardContext provided by default gives any child of wizard access to those resources. |
| 22 |
*/ |
| 23 |
|
| 24 |
export interface WizardProps extends React.HTMLProps<HTMLDivElement> { |
| 25 |
/** Step components */ |
| 26 |
children: React.ReactNode; |
| 27 |
/** Wizard header */ |
| 28 |
header?: React.ReactNode; |
| 29 |
/** Wizard footer */ |
| 30 |
footer?: WizardFooterType; |
| 31 |
/** Wizard navigation */ |
| 32 |
nav?: WizardNavType; |
| 33 |
/** Aria-label for the Nav */ |
| 34 |
navAriaLabel?: string; |
| 35 |
/** The initial index the wizard is to start on (1 or higher). Defaults to 1. */ |
| 36 |
startIndex?: number; |
| 37 |
/** Additional classes spread to the wizard */ |
| 38 |
className?: string; |
| 39 |
/** Custom width of the wizard */ |
| 40 |
width?: number | string; |
| 41 |
/** Custom height of the wizard */ |
| 42 |
height?: number | string; |
| 43 |
/** Disables steps that haven't been visited. Defaults to false. */ |
| 44 |
isVisitRequired?: boolean; |
| 45 |
/** Progressively shows steps, where all steps following the active step are hidden. Defaults to false. */ |
| 46 |
isProgressive?: boolean; |
| 47 |
/** Callback function when navigating between steps */ |
| 48 |
onStepChange?: ( |
| 49 |
event: React.MouseEvent<HTMLButtonElement>, |
| 50 |
currentStep: WizardStepType, |
| 51 |
prevStep: WizardStepType, |
| 52 |
scope: WizardStepChangeScope |
| 53 |
) => void | Promise<void>; |
| 54 |
/** Callback function to save at the end of the wizard, if not specified uses onClose */ |
| 55 |
onSave?: (event: React.MouseEvent<HTMLButtonElement>) => void | Promise<void>; |
| 56 |
/** Callback function to close the wizard */ |
| 57 |
onClose?: (event: React.MouseEvent<HTMLButtonElement>) => void; |
| 58 |
/** Flag indicating whether the wizard content should be focused after the onNext or onBack callbacks |
| 59 |
* are called. |
| 60 |
*/ |
| 61 |
shouldFocusContent?: boolean; |
| 62 |
/** Adds plain styling to the wizard. */ |
| 63 |
isPlain?: boolean; |
| 64 |
/** @beta Prevents the wizard from automatically applying plain styling when glass theme is enabled. */ |
| 65 |
isNoPlainOnGlass?: boolean; |
| 66 |
} |
| 67 |
|
| 68 |
export const Wizard = ({ |
| 69 |
children, |
| 70 |
footer, |
| 71 |
height, |
| 72 |
width, |
| 73 |
className, |
| 74 |
header, |
| 75 |
nav, |
| 76 |
navAriaLabel, |
| 77 |
startIndex = 1, |
| 78 |
isVisitRequired = false, |
| 79 |
isProgressive = false, |
| 80 |
onStepChange, |
| 81 |
onSave, |
| 82 |
onClose, |
| 83 |
shouldFocusContent = true, |
| 84 |
isPlain = false, |
| 85 |
isNoPlainOnGlass = false, |
| 86 |
...wrapperProps |
| 87 |
}: WizardProps) => { |
| 88 |
const [activeStepIndex, setActiveStepIndex] = useState(startIndex); |
| 89 |
const initialSteps = buildSteps(children); |
| 90 |
const firstStepRef = useRef(initialSteps[startIndex - 1]); |
| 91 |
const wrapperRef = useRef(null); |
| 92 |
|
| 93 |
// When the startIndex maps to a parent step, focus on the first sub-step |
| 94 |
useEffect(() => { |
| 95 |
if (isWizardParentStep(firstStepRef.current)) { |
| 96 |
setActiveStepIndex(startIndex + 1); |
| 97 |
} |
| 98 |
}, [startIndex]); |
| 99 |
|
| 100 |
// When the number of steps changes and pushes activeStepIndex out of bounds, reset back to startIndex |
| 101 |
useEffect(() => { |
| 102 |
if (activeStepIndex > initialSteps.length) { |
| 103 |
setActiveStepIndex(startIndex); |
| 104 |
} |
| 105 |
}, [initialSteps, activeStepIndex, startIndex]); |
| 106 |
|
| 107 |
const focusMainContentElement = () => |
| 108 |
setTimeout(() => { |
| 109 |
wrapperRef?.current?.focus && wrapperRef.current.focus(); |
| 110 |
}, 0); |
| 111 |
|
| 112 |
const goToNextStep = (event: React.MouseEvent<HTMLButtonElement>, steps: WizardStepType[] = initialSteps) => { |
| 113 |
const newStep = steps.find((step) => step.index > activeStepIndex && isStepEnabled(steps, step)); |
| 114 |
|
| 115 |
if (activeStepIndex >= steps.length || !newStep?.index) { |
| 116 |
return onSave ? onSave(event) : onClose?.(event); |
| 117 |
} |
| 118 |
|
| 119 |
setActiveStepIndex(newStep?.index); |
| 120 |
onStepChange?.(event, newStep, steps[activeStepIndex - 1], WizardStepChangeScope.Next); |
| 121 |
shouldFocusContent && focusMainContentElement(); |
| 122 |
}; |
| 123 |
|
| 124 |
const goToPrevStep = (event: React.MouseEvent<HTMLButtonElement>, steps: WizardStepType[] = initialSteps) => { |
| 125 |
const newStep = [...steps] |
| 126 |
.reverse() |
| 127 |
.find((step: WizardStepType) => step.index < activeStepIndex && isStepEnabled(steps, step)); |
| 128 |
|
| 129 |
setActiveStepIndex(newStep?.index); |
| 130 |
onStepChange?.(event, newStep, steps[activeStepIndex - 1], WizardStepChangeScope.Back); |
| 131 |
shouldFocusContent && focusMainContentElement(); |
| 132 |
}; |
| 133 |
|
| 134 |
const goToStepByIndex = ( |
| 135 |
event: React.MouseEvent<HTMLButtonElement>, |
| 136 |
steps: WizardStepType[] = initialSteps, |
| 137 |
index: number |
| 138 |
) => { |
| 139 |
const lastStepIndex = steps.length + 1; |
| 140 |
|
| 141 |
// Handle index when out of bounds or hidden |
| 142 |
if (index < 1) { |
| 143 |
index = 1; |
| 144 |
} else if (index > lastStepIndex) { |
| 145 |
index = lastStepIndex; |
| 146 |
} |
| 147 |
|
| 148 |
const currStep = steps[index - 1]; |
| 149 |
const prevStep = steps[activeStepIndex - 1]; |
| 150 |
|
| 151 |
setActiveStepIndex(index); |
| 152 |
onStepChange?.(event, currStep, prevStep, WizardStepChangeScope.Nav); |
| 153 |
}; |
| 154 |
|
| 155 |
const goToStepById = (steps: WizardStepType[] = initialSteps, id: number | string) => { |
| 156 |
const step = steps.find((step) => step.id === id); |
| 157 |
const stepIndex = step?.index; |
| 158 |
const lastStepIndex = steps.length + 1; |
| 159 |
|
| 160 |
if (stepIndex > 0 && stepIndex < lastStepIndex && !step.isDisabled && !step.isHidden) { |
| 161 |
setActiveStepIndex(stepIndex); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
const goToStepByName = (steps: WizardStepType[] = initialSteps, name: string) => { |
| 166 |
const step = steps.find((step) => step.name === name); |
| 167 |
const stepIndex = step?.index; |
| 168 |
const lastStepIndex = steps.length + 1; |
| 169 |
|
| 170 |
if (stepIndex > 0 && stepIndex < lastStepIndex && !step.isDisabled && !step.isHidden) { |
| 171 |
setActiveStepIndex(stepIndex); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
return ( |
| 176 |
<WizardContextProvider |
| 177 |
steps={initialSteps} |
| 178 |
activeStepIndex={activeStepIndex} |
| 179 |
footer={footer} |
| 180 |
onNext={goToNextStep} |
| 181 |
onBack={goToPrevStep} |
| 182 |
onClose={onClose} |
| 183 |
goToStepById={goToStepById} |
| 184 |
goToStepByName={goToStepByName} |
| 185 |
goToStepByIndex={goToStepByIndex} |
| 186 |
shouldFocusContent={shouldFocusContent} |
| 187 |
mainWrapperRef={wrapperRef} |
| 188 |
> |
| 189 |
<div |
| 190 |
className={css( |
| 191 |
styles.wizard, |
| 192 |
isPlain && styles.modifiers.plain, |
| 193 |
isNoPlainOnGlass && styles.modifiers.noPlainOnGlass, |
| 194 |
className |
| 195 |
)} |
| 196 |
style={{ |
| 197 |
...(height ? { [wizardHeightToken.name]: typeof height === 'number' ? `${height}px` : height } : {}), |
| 198 |
...(width ? { width } : {}) |
| 199 |
}} |
| 200 |
{...wrapperProps} |
| 201 |
> |
| 202 |
{header} |
| 203 |
<WizardInternal |
| 204 |
nav={nav} |
| 205 |
navAriaLabel={navAriaLabel} |
| 206 |
isVisitRequired={isVisitRequired} |
| 207 |
isProgressive={isProgressive} |
| 208 |
/> |
| 209 |
</div> |
| 210 |
</WizardContextProvider> |
| 211 |
); |
| 212 |
}; |
| 213 |
|
| 214 |
const WizardInternal = ({ |
| 215 |
nav, |
| 216 |
navAriaLabel, |
| 217 |
isVisitRequired, |
| 218 |
isProgressive |
| 219 |
}: Pick<WizardProps, 'nav' | 'navAriaLabel' | 'isVisitRequired' | 'isProgressive'>) => { |
| 220 |
const { activeStep, steps, footer, goToStepByIndex } = useWizardContext(); |
| 221 |
const [isNavExpanded, setIsNavExpanded] = useState(false); |
| 222 |
|
| 223 |
const wizardNav = useMemo(() => { |
| 224 |
if (isCustomWizardNav(nav)) { |
| 225 |
return typeof nav === 'function' ? nav(isNavExpanded, steps, activeStep, goToStepByIndex) : nav; |
| 226 |
} |
| 227 |
|
| 228 |
return ( |
| 229 |
<WizardNavInternal |
| 230 |
nav={nav} |
| 231 |
navAriaLabel={navAriaLabel} |
| 232 |
isNavExpanded={isNavExpanded} |
| 233 |
isVisitRequired={isVisitRequired} |
| 234 |
isProgressive={isProgressive} |
| 235 |
/> |
| 236 |
); |
| 237 |
}, [activeStep, isVisitRequired, isProgressive, goToStepByIndex, isNavExpanded, nav, navAriaLabel, steps]); |
| 238 |
|
| 239 |
return ( |
| 240 |
<WizardToggle |
| 241 |
nav={wizardNav} |
| 242 |
footer={footer} |
| 243 |
steps={steps} |
| 244 |
activeStep={activeStep} |
| 245 |
isNavExpanded={isNavExpanded} |
| 246 |
toggleNavExpanded={() => setIsNavExpanded((prevIsExpanded) => !prevIsExpanded)} |
| 247 |
/> |
| 248 |
); |
| 249 |
}; |
| 250 |
|
| 251 |
Wizard.displayName = 'Wizard'; |