Skip to content

Commit f4b53be

Browse files
authored
add provider logic (#1)
* add provider logic * clean comments and refactor names
1 parent 113b199 commit f4b53be

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/index.ts renamed to src/index.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useMemo, useState } from 'react';
22

33
interface WizardFlowProps<Step extends keyof any> {
44
initialStep: Step;
@@ -30,11 +30,22 @@ export function createWizardFlow<Steps extends Record<string, keyof any>>(
3030
DEFAULT_WIZARD_FLOW_CONTEXT,
3131
);
3232

33-
const Provider = () => {
34-
/* hold context of what the current step is */
35-
/* set context value for interacting with the flow state */
36-
/* render provider and current step */
37-
};
33+
function Provider({ initialStep, onClose, steps }: WizardFlowProps<Step>) {
34+
const [step, setStep] = useState<Step>(initialStep);
35+
const component = useMemo(() => steps[step], [steps, step]);
36+
const contextValue = useMemo(
37+
() => ({
38+
transition: (step: Step): void => setStep(step),
39+
close: (): void => {
40+
onClose && onClose();
41+
},
42+
}),
43+
[onClose],
44+
);
45+
return (
46+
<Context.Provider value={contextValue}>{component}</Context.Provider>
47+
);
48+
}
3849

3950
return { Provider, Context };
4051
}

0 commit comments

Comments
 (0)