Total Complexity | 1 |
Complexity/F | 0 |
Lines of Code | 45 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { FormikProps, FormikValues } from "formik"; |
||
2 | import isEmpty from "lodash/isEmpty"; |
||
3 | import { MutableRefObject } from "react"; |
||
4 | |||
5 | /** |
||
6 | * Focuses on a element with given id. |
||
7 | * @param id |
||
8 | */ |
||
9 | export const focusOnElement = (elementId: string): void => { |
||
10 | const element = document.getElementById(elementId); |
||
11 | if (element) { |
||
12 | element.focus(); |
||
13 | } |
||
14 | }; |
||
15 | |||
16 | /** |
||
17 | * Runs validation on all forms, then returns true if they are all valid. |
||
18 | * TODO: Figure out how to focus the first (or last) invalid input, if any. |
||
19 | * @param refs |
||
20 | */ |
||
21 | export const validateAllForms = ( |
||
22 | refs: MutableRefObject<FormikProps<FormikValues>>[], |
||
23 | ): Promise<boolean> => { |
||
24 | return Promise.all( |
||
25 | refs.map((ref: MutableRefObject<FormikProps<FormikValues>>) => |
||
26 | ref.current.validateForm(), |
||
27 | ), |
||
28 | ).then((errors) => errors.every(isEmpty)); |
||
29 | }; |
||
30 | |||
31 | /** |
||
32 | * Submits all forms. |
||
33 | * @param refs |
||
34 | */ |
||
35 | export const submitAllForms = ( |
||
36 | refs: React.MutableRefObject<FormikProps<FormikValues>>[], |
||
37 | ): Promise<void[]> => { |
||
38 | return Promise.all( |
||
39 | refs.map((ref: MutableRefObject<FormikProps<FormikValues>>) => |
||
40 | // TODO: Might need to make one mass submission by combining all values into an array. |
||
41 | ref.current.submitForm(), |
||
42 | ), |
||
43 | ); |
||
44 | }; |
||
45 |