| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.Wizard = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const react_1 = require("react"); |
| 6 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 7 |
const react_2 = require("react"); |
| 8 |
const constants_1 = require("../../../helpers/constants"); |
| 9 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 10 |
const wizard_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Wizard/wizard")); |
| 11 |
const Modal_1 = require("../Modal"); |
| 12 |
const WizardFooterInternal_1 = require("./WizardFooterInternal"); |
| 13 |
const WizardToggle_1 = require("./WizardToggle"); |
| 14 |
const WizardNav_1 = require("./WizardNav"); |
| 15 |
const WizardNavItem_1 = require("./WizardNavItem"); |
| 16 |
const WizardContext_1 = require("./WizardContext"); |
| 17 |
const WizardHeader_1 = require("./WizardHeader"); |
| 18 |
class Wizard extends react_2.Component { |
| 19 |
constructor(props) { |
| 20 |
super(props); |
| 21 |
this.handleKeyClicks = (event) => { |
| 22 |
if (event.key === constants_1.KeyTypes.Escape) { |
| 23 |
if (this.state.isNavOpen) { |
| 24 |
this.setState({ isNavOpen: !this.state.isNavOpen }); |
| 25 |
} |
| 26 |
else if (this.props.isOpen) { |
| 27 |
this.props.onClose(); |
| 28 |
} |
| 29 |
} |
| 30 |
}; |
| 31 |
this.onNext = () => { |
| 32 |
const { onNext, onClose, onSave } = this.props; |
| 33 |
const { currentStep } = this.state; |
| 34 |
const flattenedSteps = this.getFlattenedSteps(); |
| 35 |
const maxSteps = flattenedSteps.length; |
| 36 |
if (currentStep >= maxSteps) { |
| 37 |
// Hit the save button at the end of the wizard |
| 38 |
if (onSave) { |
| 39 |
return onSave(); |
| 40 |
} |
| 41 |
return onClose(); |
| 42 |
} |
| 43 |
else { |
| 44 |
let newStep = currentStep; |
| 45 |
for (let nextStep = currentStep; nextStep <= maxSteps; nextStep++) { |
| 46 |
if (!flattenedSteps[nextStep]) { |
| 47 |
return; |
| 48 |
} |
| 49 |
if (!flattenedSteps[nextStep].isDisabled) { |
| 50 |
newStep = nextStep + 1; |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
this.setCurrentStep(newStep, flattenedSteps[newStep - 1]); |
| 55 |
const { id: prevId, name: prevName } = flattenedSteps[currentStep - 1]; |
| 56 |
const { id, name } = flattenedSteps[newStep - 1]; |
| 57 |
return onNext && onNext({ id, name }, { prevId, prevName }); |
| 58 |
} |
| 59 |
}; |
| 60 |
this.onBack = () => { |
| 61 |
const { onBack } = this.props; |
| 62 |
const { currentStep } = this.state; |
| 63 |
const flattenedSteps = this.getFlattenedSteps(); |
| 64 |
if (flattenedSteps.length < currentStep) { |
| 65 |
// Previous step was removed, just update the currentStep state |
| 66 |
const adjustedStep = flattenedSteps.length; |
| 67 |
this.setCurrentStep(adjustedStep, flattenedSteps[adjustedStep - 1]); |
| 68 |
} |
| 69 |
else { |
| 70 |
let newStep = currentStep; |
| 71 |
for (let prevStep = currentStep; prevStep >= 0; prevStep--) { |
| 72 |
if (!flattenedSteps[prevStep - 2]) { |
| 73 |
return; |
| 74 |
} |
| 75 |
if (!flattenedSteps[prevStep - 2].isDisabled) { |
| 76 |
newStep = prevStep - 1 <= 1 ? 1 : prevStep - 1; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
this.setCurrentStep(newStep, flattenedSteps[newStep - 1]); |
| 81 |
const { id: prevId, name: prevName } = flattenedSteps[newStep]; |
| 82 |
const { id, name } = flattenedSteps[newStep - 1]; |
| 83 |
return onBack && onBack({ id, name }, { prevId, prevName }); |
| 84 |
} |
| 85 |
}; |
| 86 |
this.goToStep = (step) => { |
| 87 |
const flattenedSteps = this.getFlattenedSteps(); |
| 88 |
if (flattenedSteps[step - 1].isDisabled) { |
| 89 |
return; |
| 90 |
} |
| 91 |
const { onGoToStep } = this.props; |
| 92 |
const { currentStep } = this.state; |
| 93 |
const maxSteps = flattenedSteps.length; |
| 94 |
if (step < 1) { |
| 95 |
step = 1; |
| 96 |
} |
| 97 |
else if (step > maxSteps) { |
| 98 |
step = maxSteps; |
| 99 |
} |
| 100 |
this.setCurrentStep(step, flattenedSteps[step - 1]); |
| 101 |
this.setState({ isNavOpen: false }); |
| 102 |
const { id: prevId, name: prevName } = flattenedSteps[currentStep - 1]; |
| 103 |
const { id, name } = flattenedSteps[step - 1]; |
| 104 |
return onGoToStep && onGoToStep({ id, name }, { prevId, prevName }); |
| 105 |
}; |
| 106 |
this.goToStepById = (stepId) => { |
| 107 |
const flattenedSteps = this.getFlattenedSteps(); |
| 108 |
let step; |
| 109 |
for (let i = 0; i < flattenedSteps.length; i++) { |
| 110 |
if (flattenedSteps[i].id === stepId) { |
| 111 |
step = i + 1; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
if (step) { |
| 116 |
this.setCurrentStep(step, flattenedSteps[step - 1]); |
| 117 |
} |
| 118 |
}; |
| 119 |
this.goToStepByName = (stepName) => { |
| 120 |
const flattenedSteps = this.getFlattenedSteps(); |
| 121 |
let step; |
| 122 |
for (let i = 0; i < flattenedSteps.length; i++) { |
| 123 |
if (flattenedSteps[i].name === stepName) { |
| 124 |
step = i + 1; |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
if (step) { |
| 129 |
this.setCurrentStep(step, flattenedSteps[step - 1]); |
| 130 |
} |
| 131 |
}; |
| 132 |
this.getFlattenedSteps = () => { |
| 133 |
const { steps } = this.props; |
| 134 |
const flattenedSteps = []; |
| 135 |
for (const step of steps) { |
| 136 |
if (step.steps) { |
| 137 |
for (const childStep of step.steps) { |
| 138 |
flattenedSteps.push(childStep); |
| 139 |
} |
| 140 |
} |
| 141 |
else { |
| 142 |
flattenedSteps.push(step); |
| 143 |
} |
| 144 |
} |
| 145 |
return flattenedSteps; |
| 146 |
}; |
| 147 |
this.getFlattenedStepsIndex = (flattenedSteps, stepName) => { |
| 148 |
for (let i = 0; i < flattenedSteps.length; i++) { |
| 149 |
if (flattenedSteps[i].name === stepName) { |
| 150 |
return i + 1; |
| 151 |
} |
| 152 |
} |
| 153 |
return 0; |
| 154 |
}; |
| 155 |
this.initSteps = (steps) => { |
| 156 |
// Set default Step values |
| 157 |
for (let i = 0; i < steps.length; i++) { |
| 158 |
if (steps[i].steps) { |
| 159 |
for (let j = 0; j < steps[i].steps.length; j++) { |
| 160 |
steps[i].steps[j] = Object.assign({ canJumpTo: true }, steps[i].steps[j]); |
| 161 |
} |
| 162 |
} |
| 163 |
steps[i] = Object.assign({ canJumpTo: true }, steps[i]); |
| 164 |
} |
| 165 |
return steps; |
| 166 |
}; |
| 167 |
this.getElement = (appendTo) => { |
| 168 |
if (typeof appendTo === 'function') { |
| 169 |
return appendTo(); |
| 170 |
} |
| 171 |
return appendTo || document.body; |
| 172 |
}; |
| 173 |
const newId = Wizard.currentId++; |
| 174 |
this.titleId = props.titleId || `pf-wizard-title-${newId}`; |
| 175 |
this.descriptionId = props.descriptionId || `pf-wizard-description-${newId}`; |
| 176 |
this.state = { |
| 177 |
currentStep: this.props.startAtStep && Number.isInteger(this.props.startAtStep) ? this.props.startAtStep : 1, |
| 178 |
isNavOpen: false |
| 179 |
}; |
| 180 |
if (props.onCurrentStepChanged) { |
| 181 |
const flattenedSteps = this.getFlattenedSteps(); |
| 182 |
if (flattenedSteps.length >= this.state.currentStep) { |
| 183 |
const currentStep = flattenedSteps[this.state.currentStep - 1]; |
| 184 |
props.onCurrentStepChanged(currentStep); |
| 185 |
} |
| 186 |
} |
| 187 |
this.drawerRef = (0, react_2.createRef)(); |
| 188 |
} |
| 189 |
setCurrentStep(currentStep, currentStepObject) { |
| 190 |
this.setState({ |
| 191 |
currentStep |
| 192 |
}); |
| 193 |
if (this.props.onCurrentStepChanged) { |
| 194 |
this.props.onCurrentStepChanged(currentStepObject); |
| 195 |
} |
| 196 |
} |
| 197 |
componentDidMount() { |
| 198 |
const target = typeof document !== 'undefined' ? document.body : null; |
| 199 |
if (target) { |
| 200 |
target.addEventListener('keydown', this.handleKeyClicks, false); |
| 201 |
} |
| 202 |
} |
| 203 |
componentWillUnmount() { |
| 204 |
const target = (typeof document !== 'undefined' && document.body) || null; |
| 205 |
if (target) { |
| 206 |
target.removeEventListener('keydown', this.handleKeyClicks, false); |
| 207 |
} |
| 208 |
} |
| 209 |
componentDidUpdate(prevProps) { |
| 210 |
if (prevProps.startAtStep !== this.props.startAtStep) { |
| 211 |
this.setState({ currentStep: this.props.startAtStep }); |
| 212 |
} |
| 213 |
} |
| 214 |
render() { |
| 215 |
const _a = this.props, { |
| 216 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 217 |
width, height, title, description, descriptionComponent, onClose, onSave, onBack, onNext, onGoToStep, className, steps, startAtStep, nextButtonText = 'Next', backButtonText = 'Back', cancelButtonText = 'Cancel', hideClose, closeButtonAriaLabel = 'Close', navAriaLabel, navAriaLabelledBy, mainAriaLabel, mainAriaLabelledBy, hasNoBodyPadding, footer, appendTo, isOpen, titleId, descriptionId, isNavExpandable, onCurrentStepChanged, hasDrawer, isDrawerExpanded, onExpandDrawer } = _a, rest = tslib_1.__rest(_a, ["width", "height", "title", "description", "descriptionComponent", "onClose", "onSave", "onBack", "onNext", "onGoToStep", "className", "steps", "startAtStep", "nextButtonText", "backButtonText", "cancelButtonText", "hideClose", "closeButtonAriaLabel", "navAriaLabel", "navAriaLabelledBy", "mainAriaLabel", "mainAriaLabelledBy", "hasNoBodyPadding", "footer", "appendTo", "isOpen", "titleId", "descriptionId", "isNavExpandable", "onCurrentStepChanged", "hasDrawer", "isDrawerExpanded", "onExpandDrawer"]) |
| 218 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 219 |
; |
| 220 |
const { currentStep } = this.state; |
| 221 |
const flattenedSteps = this.getFlattenedSteps(); |
| 222 |
const adjustedStep = flattenedSteps.length < currentStep ? flattenedSteps.length : currentStep; |
| 223 |
const activeStep = flattenedSteps[adjustedStep - 1]; |
| 224 |
const computedSteps = this.initSteps(steps); |
| 225 |
const firstStep = activeStep === flattenedSteps[0]; |
| 226 |
const isValid = activeStep && activeStep.enableNext !== undefined ? activeStep.enableNext : true; |
| 227 |
const nav = (isWizardNavOpen) => { |
| 228 |
const wizNavAProps = { |
| 229 |
isOpen: isWizardNavOpen, |
| 230 |
'aria-label': navAriaLabel, |
| 231 |
'aria-labelledby': (title || navAriaLabelledBy) && (navAriaLabelledBy || this.titleId) |
| 232 |
}; |
| 233 |
return ((0, jsx_runtime_1.jsx)(WizardNav_1.WizardNav, Object.assign({}, wizNavAProps, { children: computedSteps.map((step, index) => { |
| 234 |
if (step.isFinishedStep) { |
| 235 |
// Don't show finished step in the side nav |
| 236 |
return; |
| 237 |
} |
| 238 |
let enabled; |
| 239 |
let navItemStep; |
| 240 |
if (step.steps) { |
| 241 |
let hasActiveChild = false; |
| 242 |
let canJumpToParent = false; |
| 243 |
for (const subStep of step.steps) { |
| 244 |
if (activeStep.name === subStep.name) { |
| 245 |
// one of the children matches |
| 246 |
hasActiveChild = true; |
| 247 |
} |
| 248 |
if (subStep.canJumpTo) { |
| 249 |
canJumpToParent = true; |
| 250 |
} |
| 251 |
} |
| 252 |
navItemStep = this.getFlattenedStepsIndex(flattenedSteps, step.steps[0].name); |
| 253 |
return ((0, jsx_runtime_1.jsx)(WizardNavItem_1.WizardNavItem, { id: step.id, content: step.name, isExpandable: isNavExpandable, isCurrent: hasActiveChild, isDisabled: !canJumpToParent, step: navItemStep, onNavItemClick: this.goToStep, children: (0, jsx_runtime_1.jsx)(WizardNav_1.WizardNav, Object.assign({}, wizNavAProps, { returnList: true, children: step.steps.map((childStep, indexChild) => { |
| 254 |
if (childStep.isFinishedStep) { |
| 255 |
// Don't show finished step in the side nav |
| 256 |
return; |
| 257 |
} |
| 258 |
navItemStep = this.getFlattenedStepsIndex(flattenedSteps, childStep.name); |
| 259 |
enabled = childStep.canJumpTo && !childStep.isDisabled; |
| 260 |
return ((0, jsx_runtime_1.jsx)(WizardNavItem_1.WizardNavItem, { id: childStep.id, content: childStep.name, isCurrent: activeStep.name === childStep.name, isDisabled: !enabled, step: navItemStep, onNavItemClick: this.goToStep }, `child_${indexChild}`)); |
| 261 |
}) })) }, index)); |
| 262 |
} |
| 263 |
navItemStep = this.getFlattenedStepsIndex(flattenedSteps, step.name); |
| 264 |
enabled = step.canJumpTo && !step.isDisabled; |
| 265 |
return ((0, react_1.createElement)(WizardNavItem_1.WizardNavItem, Object.assign({}, step.stepNavItemProps, { key: index, id: step.id, content: step.name, isCurrent: activeStep.name === step.name, isDisabled: !enabled, step: navItemStep, onNavItemClick: this.goToStep }))); |
| 266 |
}) }))); |
| 267 |
}; |
| 268 |
const context = { |
| 269 |
goToStepById: this.goToStepById, |
| 270 |
goToStepByName: this.goToStepByName, |
| 271 |
onNext: this.onNext, |
| 272 |
onBack: this.onBack, |
| 273 |
onClose, |
| 274 |
activeStep |
| 275 |
}; |
| 276 |
const divStyles = Object.assign(Object.assign({}, (height ? { height } : {})), (width ? { width } : {})); |
| 277 |
const wizard = ((0, jsx_runtime_1.jsx)(WizardContext_1.WizardContextProvider, { value: context, children: (0, jsx_runtime_1.jsxs)("div", Object.assign({}, rest, { className: (0, react_styles_1.css)(wizard_1.default.wizard, activeStep && activeStep.isFinishedStep && 'pf-m-finished', className), style: Object.keys(divStyles).length ? divStyles : undefined, children: [title && ((0, jsx_runtime_1.jsx)(WizardHeader_1.WizardHeader, { titleId: this.titleId, descriptionId: this.descriptionId, onClose: onClose, title: title, description: description, descriptionComponent: descriptionComponent, closeButtonAriaLabel: closeButtonAriaLabel, hideClose: hideClose })), (0, jsx_runtime_1.jsx)(WizardToggle_1.WizardToggle, { hasDrawer: hasDrawer, isDrawerExpanded: isDrawerExpanded, onExpandDrawer: onExpandDrawer, mainAriaLabel: mainAriaLabel, isInPage: isOpen === undefined, mainAriaLabelledBy: (title || mainAriaLabelledBy) && (mainAriaLabelledBy || this.titleId), isNavOpen: this.state.isNavOpen, onNavToggle: (isNavOpen) => this.setState({ isNavOpen }), nav: nav, steps: steps, activeStep: activeStep, hasNoBodyPadding: hasNoBodyPadding, children: footer || ((0, jsx_runtime_1.jsx)(WizardFooterInternal_1.WizardFooterInternal, { onNext: this.onNext, onBack: this.onBack, onClose: onClose, isValid: isValid, firstStep: firstStep, activeStep: activeStep, nextButtonText: (activeStep && activeStep.nextButtonText) || nextButtonText, backButtonText: backButtonText, cancelButtonText: cancelButtonText })) })] })) })); |
| 278 |
if (isOpen !== undefined) { |
| 279 |
return ((0, jsx_runtime_1.jsx)(Modal_1.Modal, { width: width !== null ? width : undefined, isOpen: isOpen, variant: Modal_1.ModalVariant.large, "aria-labelledby": this.titleId, "aria-describedby": this.descriptionId, showClose: false, hasNoBodyWrapper: true, children: wizard })); |
| 280 |
} |
| 281 |
return wizard; |
| 282 |
} |
| 283 |
} |
| 284 |
exports.Wizard = Wizard; |
| 285 |
Wizard.displayName = 'Wizard'; |
| 286 |
Wizard.currentId = 0; |
| 287 |
Wizard.defaultProps = { |
| 288 |
title: null, |
| 289 |
description: '', |
| 290 |
descriptionComponent: 'p', |
| 291 |
className: '', |
| 292 |
startAtStep: 1, |
| 293 |
nextButtonText: 'Next', |
| 294 |
backButtonText: 'Back', |
| 295 |
cancelButtonText: 'Cancel', |
| 296 |
hideClose: false, |
| 297 |
closeButtonAriaLabel: 'Close', |
| 298 |
navAriaLabel: null, |
| 299 |
navAriaLabelledBy: null, |
| 300 |
mainAriaLabel: null, |
| 301 |
mainAriaLabelledBy: null, |
| 302 |
hasNoBodyPadding: false, |
| 303 |
onBack: null, |
| 304 |
onNext: null, |
| 305 |
onGoToStep: null, |
| 306 |
width: null, |
| 307 |
height: null, |
| 308 |
footer: null, |
| 309 |
onClose: () => undefined, |
| 310 |
appendTo: null, |
| 311 |
isOpen: undefined, |
| 312 |
isNavExpandable: false, |
| 313 |
hasDrawer: false, |
| 314 |
isDrawerExpanded: false, |
| 315 |
onExpandDrawer: () => undefined |
| 316 |
}; |
| 317 |
//# sourceMappingURL=Wizard.js.map |