Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
fix(wizard): fix footer and header props
Browse files Browse the repository at this point in the history
  • Loading branch information
Waryen committed Nov 25, 2022
1 parent 963e419 commit da07825
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default function Index() {

return (
<WizardProvider
header={Header}
footer={Footer}
header={(props) => <Header {...props} />}
footer={(props) => <Footer {...props} />}
initialIndex={2}
onStartReached={closeWizard}
onEndReached={closeWizard}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ReactElement } from 'react';

export type WizardProps = {
header?: ReactElement;
footer?: ReactElement;
header?: (props: WizardContext) => ReactElement<WizardContext>;
footer?: (props: WizardContext) => ReactElement<WizardContext>;
initialIndex?: number;
onStartReached?: () => void;
onEndReached?: () => void;
Expand Down
80 changes: 40 additions & 40 deletions src/wizard.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,6 @@ export const WizardProvider = ({
setActiveIndex((prev) => prev + 1);
}, [activeIndex, maxAmountOfSteps, onEndReached]);

/**
*
* Render a custom footer component.
*/
const renderFooter = useCallback(() => {
if (!footer) {
return null;
}
if (!React.isValidElement(footer)) {
throw new Error('Invalid footer component.');
}
return footer;
}, [footer]);

/**
*
* Render a custom header component.
*/
const renderHeader = useCallback(() => {
if (!header) {
return null;
}
if (!React.isValidElement(header)) {
throw new Error('Invalid footer component.');
}
return header;
}, [header]);

/**
*
* Render the current step.
*/
const renderStep = useCallback(() => {
const childrenCollection = getValidChildren(children);
return childrenCollection[activeIndex - 1];
}, [children, activeIndex]);

/**
*
* Values provided to the children.
Expand Down Expand Up @@ -173,6 +136,43 @@ export const WizardProvider = ({
]
);

/**
*
* Render a header component if any and valid.
*/
const renderHeader = useMemo(() => {
if (!header) {
return null;
} else if (!React.isValidElement(header(value))) {
throw new Error('Invalid header component passed to the wizard.');
} else {
return header(value);
}
}, [header, value]);

/**
*
* Render a footer component if any and valid.
*/
const renderFooter = useMemo(() => {
if (!footer) {
return null;
} else if (!React.isValidElement(footer(value))) {
throw new Error('Invalid footer component passed to the wizard.');
} else {
return footer(value);
}
}, [footer, value]);

/**
*
* Render the current step.
*/
const renderStep = useMemo(() => {
const childrenCollection = getValidChildren(children);
return childrenCollection[activeIndex - 1];
}, [children, activeIndex]);

/**
*
* Throw an error if the `initialIndex` prop is invalid.
Expand All @@ -187,9 +187,9 @@ export const WizardProvider = ({

return (
<Wizard.Provider value={value}>
{renderHeader()}
{renderStep()}
{renderFooter()}
{renderHeader}
{renderStep}
{renderFooter}
</Wizard.Provider>
);
};
Expand Down

0 comments on commit da07825

Please sign in to comment.