-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use render prop pattern for stepper buttons
- Loading branch information
1 parent
c66490c
commit 81ffb53
Showing
10 changed files
with
231 additions
and
98 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { useStepper } from "../StepperContext"; | ||
|
||
const Buttons = ({ children }) => { | ||
const { | ||
activeStep, | ||
steps, | ||
setActiveStep, | ||
setErrors, | ||
onFinish, | ||
onChange, | ||
onSkip, | ||
stepValidator, | ||
stepErrMessage, | ||
setValidationAttempted, | ||
} = useStepper(); | ||
|
||
const isLastStep = activeStep === steps.length - 1; | ||
|
||
const goToNextStep = async () => { | ||
setValidationAttempted(true); | ||
const isNextStepAllowed = await stepValidator(steps[activeStep]?.props?.id); | ||
|
||
if (isNextStepAllowed) { | ||
if (activeStep < steps.length - 1) { | ||
setActiveStep((prev) => prev + 1); | ||
setErrors((prev) => ({ ...prev, [activeStep]: null })); | ||
onChange(steps[activeStep]?.props?.id); | ||
} else if (activeStep === steps.length - 1) { | ||
onFinish(); | ||
} | ||
setValidationAttempted(false); | ||
} else { | ||
setErrors((prev) => ({ | ||
...prev, | ||
[activeStep]: stepErrMessage, | ||
})); | ||
} | ||
}; | ||
|
||
const goToPreviousStep = () => { | ||
if (activeStep > 0) { | ||
setActiveStep((prev) => prev - 1); | ||
setValidationAttempted(false); | ||
onChange(steps[activeStep]?.props?.id); | ||
} | ||
}; | ||
|
||
const onSkipStep = () => { | ||
onSkip(steps[activeStep]?.props?.id); | ||
}; | ||
|
||
return ( | ||
<div className="stepper-buttons"> | ||
{children({ | ||
goToNextStep, | ||
goToPreviousStep, | ||
onSkipStep, | ||
isLastStep, | ||
activeStep, | ||
stepId: steps[activeStep]?.props?.id || "", | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
Buttons.displayName = "StepperButtons"; | ||
Buttons.propTypes = { | ||
children: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default Buttons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
const Step = ({ children }) => { | ||
return <>{children}</>; | ||
}; | ||
|
||
Step.displayName = "StepperStep"; | ||
|
||
Step.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
title: PropTypes.string, | ||
description: PropTypes.string, | ||
id: PropTypes.string, | ||
}; | ||
|
||
export default Step; |
Oops, something went wrong.