F #3951: Add stepper clickable (#595)

This commit is contained in:
Sergio Betanzos authored and GitHub committed 2021-01-07 17:59:11 +01:00
1 parent cad10c55cc
commit 439dec4f93
2 files changed
+86 -45

No files matched your search

@@ -7,7 +7,8 @@ import {
Step,
StepLabel,
Box,
Typography
Typography,
StepButton
} from '@material-ui/core'
import { makeStyles, fade } from '@material-ui/core/styles'
@@ -46,6 +47,7 @@ const CustomStepper = ({
activeStep,
lastStep,
disabledBack,
handleStep,
handleNext,
handleBack,
errors,
@@ -55,25 +57,31 @@ const CustomStepper = ({
return (
<>
<Stepper activeStep={activeStep} className={classes.root}>
{steps?.map(({ id, label }) => (
<Stepper nonLinear activeStep={activeStep} className={classes.root}>
{steps?.map(({ id, label }, stepIdx) => (
<Step key={id}>
<StepLabel
StepIconProps={{
classes: {
root: classes.icon,
completed: classes.completed,
active: classes.active,
error: classes.error
}
}}
{...(Boolean(errors[id]) && { error: true })}
<StepButton
onClick={() => handleStep(stepIdx)}
completed={activeStep > stepIdx}
disabled={activeStep + 1 < stepIdx}
optional={
<Typography variant="caption" color="error">
<Typography variant='caption' color='error'>
{errors[id]?.message}
</Typography>
}
>{Tr(label)}</StepLabel>
>
<StepLabel
StepIconProps={{
classes: {
root: classes.icon,
completed: classes.completed,
active: classes.active,
error: classes.error
}
}}
{...(Boolean(errors[id]) && { error: true })}
>{Tr(label)}</StepLabel>
</StepButton>
</Step>
))}
</Stepper>
@@ -83,7 +91,7 @@ const CustomStepper = ({
</Button>
<SubmitButton
color='secondary'
data-cy="stepper-next-button"
data-cy='stepper-next-button'
onClick={handleNext}
isSubmitting={isSubmitting}
label={activeStep === lastStep ? Tr(T.Finish) : Tr(T.Next)}
@@ -107,6 +115,7 @@ CustomStepper.propTypes = {
lastStep: PropTypes.number.isRequired,
disabledBack: PropTypes.bool.isRequired,
isSubmitting: PropTypes.bool,
handleStep: PropTypes.func,
handleNext: PropTypes.func,
handleBack: PropTypes.func,
errors: PropTypes.shape({
@@ -119,6 +128,7 @@ CustomStepper.defaultProps = {
activeStep: 0,
lastStep: 0,
disabledBack: false,
handleStep: () => undefined,
handleNext: () => undefined,
handleBack: () => undefined,
errors: undefined,
@@ -25,46 +25,76 @@ const FormStepper = ({ steps, schema, onSubmit }) => {
reset({ ...formData }, { errors: false })
}, [formData])
const handleNext = () => {
const { id, resolver, optionsValidate } = steps[activeStep]
const currentData = watch(id)
const validateSchema = step => {
const { id, resolver, optionsValidate } = steps[step]
const stepData = watch(id)
const stepSchema = typeof resolver === 'function' ? resolver() : resolver
stepSchema
.validate(currentData, optionsValidate)
.then(() => {
if (activeStep === lastStep) {
onSubmit(schema().cast({ ...formData, [id]: currentData }))
} else {
setFormData(prev => ({ ...prev, [id]: currentData }))
setActiveStep(prevActiveStep => prevActiveStep + 1)
}
return stepSchema
.validate(stepData, optionsValidate)
.then(() => ({ id, data: stepData }))
}
const setErrors = ({ inner: errors, ...rest }) => {
const errorsByPath = groupBy(errors, 'path') ?? {}
const totalErrors = Object.keys(errorsByPath).length
totalErrors > 0
? setError(id, {
type: 'manual',
message: `${totalErrors} error(s) occurred`
})
.catch(({ inner, ...err }) => {
const errorsByPath = groupBy(inner, 'path') ?? {}
const totalErrors = Object.keys(errorsByPath).length
: setError(id, rest)
totalErrors > 0
? setError(id, {
type: 'manual',
message: `${totalErrors} error(s) occurred`
errors?.forEach(({ path, type, message }) =>
setError(`${id}.${path}`, { type, message })
)
}
const handleStep = stepToAdvance => {
const isBackAction = activeStep > stepToAdvance
isBackAction && handleBack(isBackAction)
steps
.slice(FIRST_STEP, stepToAdvance)
.forEach((_, step, stepsToValidate) => {
validateSchema(step)
.then(({ id, data }) => {
activeStep === step &&
setFormData(prev => ({ ...prev, [id]: data }))
step === stepsToValidate.length - 1 &&
Number.isInteger(stepToAdvance) &&
setActiveStep(stepToAdvance)
})
: setError(id, err)
inner?.forEach(({ path, type, message }) =>
setError(`${id}.${path}`, { type, message })
)
.catch(setErrors)
})
}
const handleBack = useCallback(() => {
if (activeStep <= FIRST_STEP) return
const handleNext = () => {
validateSchema(activeStep)
.then(({ id, data }) => {
if (activeStep === lastStep) {
onSubmit(schema().cast({ ...formData, [id]: data }))
} else {
setFormData(prev => ({ ...prev, [id]: data }))
setActiveStep(prevActiveStep => prevActiveStep + 1)
}
})
.catch(setErrors)
}
const handleBack = useCallback(stepToBack => {
if (activeStep < FIRST_STEP) return
const { id } = steps[activeStep]
const currentData = watch(id)
const stepData = watch(id)
setFormData(prev => ({ ...prev, [id]: currentData }))
setActiveStep(prevActiveStep => prevActiveStep - 1)
setFormData(prev => ({ ...prev, [id]: stepData }))
setActiveStep(prevActiveStep =>
Number.isInteger(stepToBack) ? stepToBack : (prevActiveStep - 1)
)
}, [activeStep])
const { id, content: Content } = useMemo(() => steps[activeStep], [
@@ -92,6 +122,7 @@ const FormStepper = ({ steps, schema, onSubmit }) => {
activeStep={activeStep}
lastStep={lastStep}
disabledBack={disabledBack}
handleStep={handleStep}
handleNext={handleNext}
handleBack={handleBack}
errors={errors}