Debian Codegraph @

node/@patternfly/react-core/dist/esm/deprecated/components/Wizard/Wizard.js

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