-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWizard.context.tsx
More file actions
62 lines (54 loc) · 1.41 KB
/
Copy pathWizard.context.tsx
File metadata and controls
62 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
type Dispatch,
type SetStateAction,
createContext,
useEffect,
useMemo,
useState,
} from 'react';
import { SingleWizardStep } from './Wizard.types';
export interface WizardState {
initialStep?: string;
activeStepId?: string;
steps: SingleWizardStep[];
isBackwardNavigationEnabled: boolean;
update: Dispatch<SetStateAction<WizardState>>;
}
export const WizardContext = createContext<WizardState>({
initialStep: undefined,
activeStepId: undefined,
steps: [],
isBackwardNavigationEnabled: false,
update: () => null,
});
interface WizardProviderProps {
children: React.ReactNode;
initialStep: string;
isBackwardNavigationEnabled: boolean;
onStepChange: (step: SingleWizardStep) => void;
}
export const WizardProvider = ({
children,
initialStep,
isBackwardNavigationEnabled,
onStepChange,
}: WizardProviderProps) => {
const [state, update] = useState<WizardState>({
activeStepId: undefined,
update: () => null,
initialStep,
steps: [],
isBackwardNavigationEnabled,
});
const activeStep = state.steps.find((item) => item.id === state.activeStepId);
useEffect(() => {
if (activeStep) {
onStepChange(activeStep);
}
// eslint-disable-next-line
}, [activeStep?.name, onStepChange]);
const value = useMemo(() => ({ ...state, update }), [state]);
return (
<WizardContext.Provider value={value}>{children}</WizardContext.Provider>
);
};