-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugfix/1912-Support-Page-Forms-Validation #1961
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useFormikContext } from 'formik' | ||
import { useEffect, useRef } from 'react' | ||
import { Steps } from './helpers/support-form.types' | ||
|
||
const STEPS_VALIDATION_MAP = { | ||
[Steps.ROLES]: ['roles'], | ||
[Steps.QUESTIONS]: ['benefactor', 'partner', 'volunteer', 'associationMember', 'company'], | ||
[Steps.PERSON]: ['person'], | ||
} | ||
|
||
const ValidationObserver = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some comment stating the need to turn off individual field validation first time the step is submitted but then turning it back on so the user gets immediate feedback when filling the correct/expected value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And finally - this new pattern might benefit greatly from a few unit tests. Benefit in terms of readability. |
||
setCurrentValidatedField, | ||
activeStep, | ||
}: { | ||
setCurrentValidatedField: React.Dispatch<React.SetStateAction<number | null>> | ||
activeStep: number | ||
}) => { | ||
const activeStepRef = useRef(0) | ||
const { errors, submitCount } = useFormikContext() | ||
|
||
useEffect(() => { | ||
if (activeStepRef.current !== activeStep) { | ||
activeStepRef.current = activeStep | ||
setCurrentValidatedField(null) | ||
} | ||
|
||
const value = Object.entries(STEPS_VALIDATION_MAP).find(([, value]) => { | ||
return value.includes(Object.keys(errors)?.[0]) | ||
}) | ||
|
||
if (submitCount > 0 && value) { | ||
setCurrentValidatedField(Number(value?.[0])) | ||
} | ||
}, [errors, submitCount, activeStep]) | ||
|
||
return null | ||
} | ||
|
||
export default ValidationObserver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentValidateField
sounds like it pertains to a field, but in here it is used to toggle on/off of individual field validation on a step.How about
validateInidividualFieldsOnStep
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to make the pattern more explicit and readable by making the value that comes out of the ValidationObserver a map such that individual field validation is turned on/off explicitly