Skip to content

Commit 9b1ac2d

Browse files
authored
fix: form submission behavior (#79)
1 parent 1871d28 commit 9b1ac2d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/components/form/Form.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ const BaseForm = <Values extends FormValues>({
7777
{formik.errors.non_field_errors}
7878
</NonFieldErrors>
7979
)
80-
} else if (order && order.length) {
80+
}
81+
// If a submission was attempted and refs to the fields were provided.
82+
else if (formik.isSubmitting && order && order.length) {
8183
const errorNames = getKeyPaths(formik.errors)
8284

8385
const inputRef = order.find(({ name }) =>

src/components/form/SubmitButton.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ const SubmitButton: FC<SubmitButtonProps> = ({
3232
type="button"
3333
onClick={() => {
3434
form.setTouched(getTouched(form.values), true).then(errors => {
35-
if (!errors || !Object.keys(errors).length) form.submitForm()
35+
const hasErrors = Boolean(errors && Object.keys(errors).length)
36+
// If has errors, set isSubmitting=true so fields in the form are
37+
// aware that a submission was attempted. Else, set
38+
// isSubmitting=false as it will be set to true when calling
39+
// submitForm().
40+
form.setSubmitting(hasErrors)
41+
if (!hasErrors) form.submitForm()
3642
})
3743
}}
3844
{...otherButtonProps}

0 commit comments

Comments
 (0)