-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
useAugmentedForm.ts
136 lines (124 loc) · 3.97 KB
/
useAugmentedForm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { BaseSyntheticEvent, useCallback, useMemo, useRef } from 'react';
import {
FieldValues,
SubmitHandler,
useForm,
UseFormProps,
} from 'react-hook-form';
import { RaRecord } from '../types';
import { SaveHandler, useSaveContext } from '../controller';
import { useRecordContext } from '../controller';
import getFormInitialValues from './getFormInitialValues';
import {
getSimpleValidationResolver,
ValidateForm,
} from './validation/getSimpleValidationResolver';
import { setSubmissionErrors } from './validation/setSubmissionErrors';
import { useNotifyIsFormInvalid } from './validation/useNotifyIsFormInvalid';
import { sanitizeEmptyValues as sanitizeValues } from './sanitizeEmptyValues';
/**
* Wrapper around react-hook-form's useForm
*
* This hook adds the following features to react-hook-form's useForm:
*
* - form initialization based on RecordContext
* - validation based on a validate function
* - sanitization of empty values
* - notification on invalid form
* - stop form submission event propagation
*/
export const useAugmentedForm = <RecordType = any>(
props: UseAugmentedFormProps<RecordType>
) => {
const {
criteriaMode = 'firstError',
defaultValues,
formRootPathname,
resolver,
reValidateMode = 'onChange',
onSubmit,
sanitizeEmptyValues,
validate,
disableInvalidFormNotification,
...rest
} = props;
const record = useRecordContext(props);
const saveContext = useSaveContext();
const defaultValuesIncludingRecord = useMemo(
() => getFormInitialValues(defaultValues, record),
// eslint-disable-next-line
[
// eslint-disable-next-line
JSON.stringify({
defaultValues:
typeof defaultValues === 'function'
? 'function'
: defaultValues,
record,
}),
]
);
const finalResolver = resolver
? resolver
: validate
? getSimpleValidationResolver(validate)
: undefined;
const form = useForm({
criteriaMode,
values: defaultValuesIncludingRecord,
reValidateMode,
resolver: finalResolver,
...rest,
});
const formRef = useRef(form);
// notify on invalid form
useNotifyIsFormInvalid(form.control, !disableInvalidFormNotification);
// submit callbacks
const handleSubmit = useCallback(
async (values, event) => {
let errors;
const finalValues = sanitizeEmptyValues
? sanitizeValues(values, record)
: values;
if (onSubmit) {
errors = await onSubmit(finalValues, event);
}
if (onSubmit == null && saveContext?.save) {
errors = await saveContext.save(finalValues, event);
}
if (errors != null) {
setSubmissionErrors(errors, formRef.current.setError);
}
},
[onSubmit, saveContext, sanitizeEmptyValues, record]
);
const formHandleSubmit = useCallback(
(event: BaseSyntheticEvent) => {
if (!event.defaultPrevented) {
// Prevent outer forms to receive the event
event.stopPropagation();
form.handleSubmit(handleSubmit)(event);
}
return;
},
[form, handleSubmit]
);
return {
form,
handleSubmit,
formHandleSubmit,
};
};
export type UseAugmentedFormProps<RecordType = any> =
UseFormOwnProps<RecordType> &
Omit<UseFormProps, 'onSubmit'> & {
validate?: ValidateForm;
};
export interface UseFormOwnProps<RecordType = any> {
defaultValues?: any;
formRootPathname?: string;
record?: Partial<RaRecord>;
onSubmit?: SubmitHandler<FieldValues> | SaveHandler<RecordType>;
sanitizeEmptyValues?: boolean;
disableInvalidFormNotification?: boolean;
}