Debian Codegraph @

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

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