Skip to content

Commit f3bdf10

Browse files
committed
Merge branch 'main' into portal-frontend-40
2 parents a3c663a + f0596b1 commit f3bdf10

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

CHANGELOG.md

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## [2.6.10](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.9...v2.6.10) (2025-02-25)
2+
3+
4+
### Bug Fixes
5+
6+
* non field errors ([dddf8d5](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/dddf8d5d1990c201ef72eff74b2d4f260ce4c7a4))
7+
8+
## [2.6.9](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.8...v2.6.9) (2025-02-25)
9+
10+
11+
### Bug Fixes
12+
13+
* rename property ([35827d7](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/35827d7452023f3a1ea8723b6b31fd9b84173cdd))
14+
15+
## [2.6.8](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.7...v2.6.8) (2025-02-24)
16+
17+
18+
### Bug Fixes
19+
20+
* form submission behavior ([#79](https://github.com/ocadotechnology/codeforlife-package-javascript/issues/79)) ([9b1ac2d](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/9b1ac2df0049cf473266798b6d210106045a86ee))
21+
122
## [2.6.7](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.6...v2.6.7) (2025-02-19)
223

324

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "codeforlife",
33
"description": "Common frontend code",
44
"private": false,
5-
"version": "2.6.7",
5+
"version": "2.6.10",
66
"type": "module",
77
"scripts": {
88
"dev": "vite",

src/components/form/Form.tsx

+25-23
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"> & {
@@ -56,43 +56,45 @@ type BaseFormProps<Values> = Omit<FormikConfig<Values>, "children"> & {
5656
children: ReactNode | ((props: _FormikProps<Values>) => ReactNode)
5757
scrollIntoViewOptions?: ScrollIntoViewOptions
5858
nonFieldErrorsProps?: Omit<NonFieldErrorsProps, "children">
59-
order?: Array<{ name: string; inputRef: RefObject<HTMLInputElement> }>
59+
fieldRefs?: Array<{ name: string; inputRef: RefObject<HTMLInputElement> }>
6060
}
6161

6262
const BaseForm = <Values extends FormValues>({
6363
children,
6464
scrollIntoViewOptions = SCROLL_INTO_VIEW_OPTIONS,
6565
nonFieldErrorsProps,
66-
order,
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-
} else if (order && order.length) {
81-
const errorNames = getKeyPaths(formik.errors)
82-
83-
const inputRef = order.find(({ name }) =>
84-
errorNames.includes(name),
85-
)?.inputRef
86-
87-
if (inputRef?.current) {
88-
inputRef.current.scrollIntoView(scrollIntoViewOptions)
89-
}
90-
}
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)
9189
}
9290

9391
return (
9492
<>
95-
{nonFieldErrors}
93+
{hasNonFieldErrors && (
94+
<NonFieldErrors {...nonFieldErrorsProps}>
95+
{formik.errors.__all__ as string}
96+
</NonFieldErrors>
97+
)}
9698
<FormikForm>
9799
{typeof children === "function" ? children(formik) : children}
98100
</FormikForm>

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)