Debian Codegraph @

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

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3 import { useEffect, useMemo, useRef, useState } from 'react';
4 import { css } from '@patternfly/react-styles';
5 import styles from '@patternfly/react-styles/css/components/Wizard/wizard.mjs';
6 import wizardHeightToken from '@patternfly/react-tokens/dist/esm/c_wizard_Height';
7 import { isWizardParentStep, isCustomWizardNav, WizardStepChangeScope } from './types';
8 import { buildSteps, isStepEnabled } from './utils';
9 import { useWizardContext, WizardContextProvider } from './WizardContext';
10 import { WizardToggle } from './WizardToggle';
11 import { WizardNavInternal } from './WizardNavInternal';
12 export const Wizard = (_a) => {
13 var { children, footer, height, width, className, header, nav, navAriaLabel, startIndex = 1, isVisitRequired = false, isProgressive = false, onStepChange, onSave, onClose, shouldFocusContent = true, isPlain = false, isNoPlainOnGlass = false } = _a, wrapperProps = __rest(_a, ["children", "footer", "height", "width", "className", "header", "nav", "navAriaLabel", "startIndex", "isVisitRequired", "isProgressive", "onStepChange", "onSave", "onClose", "shouldFocusContent", "isPlain", "isNoPlainOnGlass"]);
14 const [activeStepIndex, setActiveStepIndex] = useState(startIndex);
15 const initialSteps = buildSteps(children);
16 const firstStepRef = useRef(initialSteps[startIndex - 1]);
17 const wrapperRef = useRef(null);
18 // When the startIndex maps to a parent step, focus on the first sub-step
19 useEffect(() => {
20 if (isWizardParentStep(firstStepRef.current)) {
21 setActiveStepIndex(startIndex + 1);
22 }
23 }, [startIndex]);
24 // When the number of steps changes and pushes activeStepIndex out of bounds, reset back to startIndex
25 useEffect(() => {
26 if (activeStepIndex > initialSteps.length) {
27 setActiveStepIndex(startIndex);
28 }
29 }, [initialSteps, activeStepIndex, startIndex]);
30 const focusMainContentElement = () => setTimeout(() => {
31 var _a;
32 ((_a = wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current) === null || _a === void 0 ? void 0 : _a.focus) && wrapperRef.current.focus();
33 }, 0);
34 const goToNextStep = (event, steps = initialSteps) => {
35 const newStep = steps.find((step) => step.index > activeStepIndex && isStepEnabled(steps, step));
36 if (activeStepIndex >= steps.length || !(newStep === null || newStep === void 0 ? void 0 : newStep.index)) {
37 return onSave ? onSave(event) : onClose === null || onClose === void 0 ? void 0 : onClose(event);
38 }
39 setActiveStepIndex(newStep === null || newStep === void 0 ? void 0 : newStep.index);
40 onStepChange === null || onStepChange === void 0 ? void 0 : onStepChange(event, newStep, steps[activeStepIndex - 1], WizardStepChangeScope.Next);
41 shouldFocusContent && focusMainContentElement();
42 };
43 const goToPrevStep = (event, steps = initialSteps) => {
44 const newStep = [...steps]
45 .reverse()
46 .find((step) => step.index < activeStepIndex && isStepEnabled(steps, step));
47 setActiveStepIndex(newStep === null || newStep === void 0 ? void 0 : newStep.index);
48 onStepChange === null || onStepChange === void 0 ? void 0 : onStepChange(event, newStep, steps[activeStepIndex - 1], WizardStepChangeScope.Back);
49 shouldFocusContent && focusMainContentElement();
50 };
51 const goToStepByIndex = (event, steps = initialSteps, index) => {
52 const lastStepIndex = steps.length + 1;
53 // Handle index when out of bounds or hidden
54 if (index < 1) {
55 index = 1;
56 }
57 else if (index > lastStepIndex) {
58 index = lastStepIndex;
59 }
60 const currStep = steps[index - 1];
61 const prevStep = steps[activeStepIndex - 1];
62 setActiveStepIndex(index);
63 onStepChange === null || onStepChange === void 0 ? void 0 : onStepChange(event, currStep, prevStep, WizardStepChangeScope.Nav);
64 };
65 const goToStepById = (steps = initialSteps, id) => {
66 const step = steps.find((step) => step.id === id);
67 const stepIndex = step === null || step === void 0 ? void 0 : step.index;
68 const lastStepIndex = steps.length + 1;
69 if (stepIndex > 0 && stepIndex < lastStepIndex && !step.isDisabled && !step.isHidden) {
70 setActiveStepIndex(stepIndex);
71 }
72 };
73 const goToStepByName = (steps = initialSteps, name) => {
74 const step = steps.find((step) => step.name === name);
75 const stepIndex = step === null || step === void 0 ? void 0 : step.index;
76 const lastStepIndex = steps.length + 1;
77 if (stepIndex > 0 && stepIndex < lastStepIndex && !step.isDisabled && !step.isHidden) {
78 setActiveStepIndex(stepIndex);
79 }
80 };
81 return (_jsx(WizardContextProvider, { steps: initialSteps, activeStepIndex: activeStepIndex, footer: footer, onNext: goToNextStep, onBack: goToPrevStep, onClose: onClose, goToStepById: goToStepById, goToStepByName: goToStepByName, goToStepByIndex: goToStepByIndex, shouldFocusContent: shouldFocusContent, mainWrapperRef: wrapperRef, children: _jsxs("div", Object.assign({ className: css(styles.wizard, isPlain && styles.modifiers.plain, isNoPlainOnGlass && styles.modifiers.noPlainOnGlass, className), style: Object.assign(Object.assign({}, (height ? { [wizardHeightToken.name]: typeof height === 'number' ? `${height}px` : height } : {})), (width ? { width } : {})) }, wrapperProps, { children: [header, _jsx(WizardInternal, { nav: nav, navAriaLabel: navAriaLabel, isVisitRequired: isVisitRequired, isProgressive: isProgressive })] })) }));
82 };
83 const WizardInternal = ({ nav, navAriaLabel, isVisitRequired, isProgressive }) => {
84 const { activeStep, steps, footer, goToStepByIndex } = useWizardContext();
85 const [isNavExpanded, setIsNavExpanded] = useState(false);
86 const wizardNav = useMemo(() => {
87 if (isCustomWizardNav(nav)) {
88 return typeof nav === 'function' ? nav(isNavExpanded, steps, activeStep, goToStepByIndex) : nav;
89 }
90 return (_jsx(WizardNavInternal, { nav: nav, navAriaLabel: navAriaLabel, isNavExpanded: isNavExpanded, isVisitRequired: isVisitRequired, isProgressive: isProgressive }));
91 }, [activeStep, isVisitRequired, isProgressive, goToStepByIndex, isNavExpanded, nav, navAriaLabel, steps]);
92 return (_jsx(WizardToggle, { nav: wizardNav, footer: footer, steps: steps, activeStep: activeStep, isNavExpanded: isNavExpanded, toggleNavExpanded: () => setIsNavExpanded((prevIsExpanded) => !prevIsExpanded) }));
93 };
94 Wizard.displayName = 'Wizard';
95 //# sourceMappingURL=Wizard.js.map