Skip to content

Commit dddf8d5

Browse files
committed
fix: non field errors
1 parent a399e38 commit dddf8d5

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/components/form/Form.tsx

+24-24
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const NonFieldErrors: FC<NonFieldErrorsProps> = ({
4545
}
4646

4747
export type FormErrors<Values> = FormikErrors<
48-
Omit<Values, "non_field_errors"> & { non_field_errors: string }
48+
Omit<Values, "__all__"> & { __all__: string }
4949
>
5050

5151
type _FormikProps<Values> = Omit<FormikProps<Values>, "errors"> & {
@@ -63,38 +63,38 @@ const BaseForm = <Values extends FormValues>({
6363
children,
6464
scrollIntoViewOptions = SCROLL_INTO_VIEW_OPTIONS,
6565
nonFieldErrorsProps,
66-
fieldRefs,
66+
fieldRefs = [],
6767
...otherFormikProps
6868
}: BaseFormProps<Values>) => (
6969
<Formik {...otherFormikProps}>
7070
{/* @ts-expect-error */}
7171
{(formik: _FormikProps<Values>) => {
72-
let nonFieldErrors: undefined | JSX.Element = undefined
73-
if (Object.keys(formik.errors).length) {
74-
if (typeof formik.errors.non_field_errors === "string") {
75-
nonFieldErrors = (
76-
<NonFieldErrors {...nonFieldErrorsProps}>
77-
{formik.errors.non_field_errors}
78-
</NonFieldErrors>
79-
)
80-
}
81-
// If a submission was attempted and refs to the fields were provided.
82-
else if (formik.isSubmitting && fieldRefs && fieldRefs.length) {
83-
const errorNames = getKeyPaths(formik.errors)
84-
85-
const inputRef = fieldRefs.find(({ name }) =>
86-
errorNames.includes(name),
87-
)?.inputRef
88-
89-
if (inputRef?.current) {
90-
inputRef.current.scrollIntoView(scrollIntoViewOptions)
91-
}
92-
}
72+
const hasErrors = Boolean(Object.keys(formik.errors).length)
73+
const hasNonFieldErrors =
74+
hasErrors && typeof formik.errors.__all__ === "string"
75+
76+
// If a submission was attempted and refs to the fields were provided.
77+
if (
78+
hasErrors &&
79+
!hasNonFieldErrors &&
80+
formik.isSubmitting &&
81+
fieldRefs.length
82+
) {
83+
const errorNames = getKeyPaths(formik.errors)
84+
85+
const input = fieldRefs.find(({ name }) => errorNames.includes(name))
86+
?.inputRef.current
87+
88+
if (input) input.scrollIntoView(scrollIntoViewOptions)
9389
}
9490

9591
return (
9692
<>
97-
{nonFieldErrors}
93+
{hasNonFieldErrors && (
94+
<NonFieldErrors {...nonFieldErrorsProps}>
95+
{formik.errors.__all__ as string}
96+
</NonFieldErrors>
97+
)}
9898
<FormikForm>
9999
{typeof children === "function" ? children(formik) : children}
100100
</FormikForm>

0 commit comments

Comments
 (0)