Debian Codegraph @

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

raw
1 import { jsx as _jsx } from "react/jsx-runtime";
2 import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
3 import { isCustomWizardFooter, isWizardSubStep } from './types';
4 import { WizardFooter } from './WizardFooter';
5 export const WizardContext = createContext({});
6 export const WizardContextProvider = ({ steps: initialSteps, footer: initialFooter, activeStepIndex, children, onNext, onBack, onClose, goToStepById, goToStepByName, goToStepByIndex, shouldFocusContent, mainWrapperRef }) => {
7 const [currentSteps, setCurrentSteps] = useState(initialSteps);
8 const [currentFooter, setCurrentFooter] = useState();
9 // Callback to update steps if the overall step number changes
10 useEffect(() => {
11 if (currentSteps.length !== initialSteps.length) {
12 const newSteps = initialSteps.map((newStep) => {
13 const currentStepMatch = currentSteps.find((step) => step.id === newStep.id);
14 // If an existing step has the same id as a new step, carry over props
15 if (currentStepMatch) {
16 return Object.assign(Object.assign({}, currentStepMatch), newStep);
17 }
18 return newStep;
19 });
20 setCurrentSteps(newSteps);
21 }
22 }, [currentSteps, initialSteps]);
23 // Combined initial and current state steps
24 const steps = useMemo(() => currentSteps.map((currentStepProps, index) => (Object.assign(Object.assign({}, currentStepProps), initialSteps[index]))), [initialSteps, currentSteps]);
25 const activeStep = useMemo(() => steps.find((step) => step.index === activeStepIndex), [activeStepIndex, steps]);
26 const close = useCallback(() => onClose === null || onClose === void 0 ? void 0 : onClose(null), [onClose]);
27 const goToNextStep = useCallback(() => onNext(null, steps), [onNext, steps]);
28 const goToPrevStep = useCallback(() => onBack(null, steps), [onBack, steps]);
29 const footer = useMemo(() => {
30 const wizardFooter = (activeStep === null || activeStep === void 0 ? void 0 : activeStep.footer) || currentFooter || initialFooter;
31 if (isCustomWizardFooter(wizardFooter)) {
32 const customFooter = wizardFooter;
33 return typeof customFooter === 'function'
34 ? customFooter(activeStep, goToNextStep, goToPrevStep, close)
35 : customFooter;
36 }
37 return (_jsx(WizardFooter, Object.assign({ activeStep: activeStep, onNext: goToNextStep, onBack: goToPrevStep, onClose: close, isBackDisabled: (activeStep === null || activeStep === void 0 ? void 0 : activeStep.index) === 1 || (isWizardSubStep(activeStep) && (activeStep === null || activeStep === void 0 ? void 0 : activeStep.index) === 2) }, wizardFooter)));
38 }, [currentFooter, initialFooter, activeStep, goToNextStep, goToPrevStep, close]);
39 const getStep = useCallback((stepId) => steps.find((step) => step.id === stepId), [steps]);
40 const setStep = useCallback((step) => setCurrentSteps((prevSteps) => prevSteps.map((prevStep) => {
41 if (prevStep.id === step.id) {
42 return Object.assign(Object.assign({}, prevStep), step);
43 }
44 return prevStep;
45 })), []);
46 return (_jsx(WizardContext.Provider, { value: {
47 steps,
48 activeStep,
49 footer,
50 close,
51 getStep,
52 setStep,
53 goToNextStep,
54 goToPrevStep,
55 setFooter: setCurrentFooter,
56 goToStepById: useCallback((id) => goToStepById(steps, id), [goToStepById, steps]),
57 goToStepByName: useCallback((name) => goToStepByName(steps, name), [goToStepByName, steps]),
58 goToStepByIndex: useCallback((index) => goToStepByIndex(null, steps, index), [goToStepByIndex, steps]),
59 shouldFocusContent,
60 mainWrapperRef
61 }, children: children }));
62 };
63 WizardContextProvider.displayName = 'WizardContextProvider';
64 export const useWizardContext = () => useContext(WizardContext);
65 //# sourceMappingURL=WizardContext.js.map