Skip to content

Commit 9bb8bbd

Browse files
authored
Merge pull request #169 from workfloworchestrator/2101-refactor-rfh
2101 refactor rfh to reactHookForm
2 parents ff9a2fb + 7fec930 commit 9bb8bbd

File tree

12 files changed

+71
-60
lines changed

12 files changed

+71
-60
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Refactors rhf const to reactHookForm for better readability

frontend/packages/pydantic-forms/src/components/fields/ArrayField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { disableField, itemizeArrayItem } from '@/utils';
99
import { RenderFields } from '../render';
1010

1111
export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
12-
const { rhf, config } = usePydanticFormContext();
12+
const { reactHookForm, config } = usePydanticFormContext();
1313

1414
const disabled = pydanticFormField.attributes?.disabled || false;
15-
const { control } = rhf;
15+
const { control } = reactHookForm;
1616
const { id: arrayName, arrayItem } = pydanticFormField;
1717
const { fields, append, remove } = useFieldArray({
1818
control,

frontend/packages/pydantic-forms/src/components/fields/FieldWrap.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ interface FieldWrapProps {
2020
}
2121

2222
export const FieldWrap = ({ pydanticFormField, children }: FieldWrapProps) => {
23-
const { errorDetails, rhf, config } = usePydanticFormContext();
23+
const { errorDetails, reactHookForm, config } = usePydanticFormContext();
2424
const RowRenderer = config?.rowRenderer ? config.rowRenderer : FormRow;
25-
const fieldState = rhf.getFieldState(pydanticFormField.id);
25+
const fieldState = reactHookForm.getFieldState(pydanticFormField.id);
2626
const errorMsg =
2727
errorDetails?.mapped?.[pydanticFormField.id]?.msg ??
2828
fieldState.error?.message;

frontend/packages/pydantic-forms/src/components/fields/HiddenField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { PydanticFormElementProps } from '@/types';
66
export const HiddenField = ({
77
pydanticFormField,
88
}: PydanticFormElementProps) => {
9-
const { rhf } = usePydanticFormContext();
9+
const { reactHookForm } = usePydanticFormContext();
1010
return (
1111
<input
1212
type="hidden"
1313
data-testid={pydanticFormField.id}
14-
{...rhf.register(pydanticFormField.id)}
14+
{...reactHookForm.register(pydanticFormField.id)}
1515
/>
1616
);
1717
};

frontend/packages/pydantic-forms/src/components/form/Footer.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { usePydanticFormContext } from '@/core';
1212
const Footer = () => {
1313
const {
1414
resetForm,
15-
rhf,
15+
reactHookForm,
1616
onCancel,
1717
onPrevious,
1818
cancelButton,
@@ -66,26 +66,17 @@ const Footer = () => {
6666
};
6767

6868
const SubmitButton = () => (
69-
<button
70-
type="submit"
71-
style={{ padding: '12px' }}
72-
disabled={
73-
!rhf.formState.isValid ||
74-
(!allowUntouchedSubmit &&
75-
!rhf.formState.isDirty &&
76-
!rhf.formState.isSubmitting)
77-
}
78-
>
69+
<button type="submit" style={{ padding: '12px' }}>
7970
{submitButtonLabel}
8071
</button>
8172
);
8273

8374
return (
8475
<div style={{ height: '200px', marginTop: '24px' }}>
8576
<div style={{ display: 'flex', gap: '16px' }}>
86-
{rhf.formState.isValid &&
77+
{reactHookForm.formState.isValid &&
8778
!allowUntouchedSubmit &&
88-
!rhf.formState.isDirty && (
79+
!reactHookForm.formState.isDirty && (
8980
<div>Het formulier is nog niet aangepast</div>
9081
)}
9182
{formInputData && formInputData.length > 0 && (
@@ -106,9 +97,10 @@ const Footer = () => {
10697
fontSize: '24px',
10798
}}
10899
>
109-
{!rhf.formState.isValid && rhf.formState.isDirty && (
110-
<div>{t('notFilledYet')}</div>
111-
)}
100+
{!reactHookForm.formState.isValid &&
101+
reactHookForm.formState.isDirty && (
102+
<div>{t('notFilledYet')}</div>
103+
)}
112104
</div>
113105
</div>
114106
);

frontend/packages/pydantic-forms/src/components/form/ResetNullableFieldTrigger.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import { PydanticFormField } from '@/types';
66
import { preventDefault } from '@/utils';
77

88
const ResetNullableFieldTrigger = ({ field }: { field: PydanticFormField }) => {
9-
const { rhf } = usePydanticFormContext();
9+
const { reactHookForm } = usePydanticFormContext();
1010

1111
const setNullValue = useCallback(() => {
12-
rhf.setValue(field.id, null);
13-
rhf.trigger(field.id);
14-
}, [rhf, field]);
12+
reactHookForm.setValue(field.id, null);
13+
reactHookForm.trigger(field.id);
14+
}, [reactHookForm, field]);
1515

16-
if (!isNullableField(field) || rhf.getValues()?.[field.id] === null) {
16+
if (
17+
!isNullableField(field) ||
18+
reactHookForm.getValues()?.[field.id] === null
19+
) {
1720
return null;
1821
}
1922

frontend/packages/pydantic-forms/src/components/render/RenderReactHookFormErrors.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import { usePydanticFormContext } from '@/core';
99
import { getFieldLabelById } from '@/core/helper';
1010

1111
export default function RenderReactHookFormErrors() {
12-
const { rhf, pydanticFormSchema } = usePydanticFormContext();
12+
const { reactHookForm, pydanticFormSchema } = usePydanticFormContext();
1313
const [showDetails, setShowDetails] = useState(false);
1414

1515
const toggleDetails = useCallback(() => {
1616
setShowDetails((state) => !state);
1717
}, []);
1818

19-
if (rhf.formState.isValid) {
19+
if (reactHookForm.formState.isValid) {
2020
return <></>;
2121
}
2222

23-
const numErrors = Object.keys(rhf.formState.errors).length;
23+
const numErrors = Object.keys(reactHookForm.formState.errors).length;
2424
const multiMistakes = numErrors > 1;
2525

2626
return (
@@ -29,7 +29,7 @@ export default function RenderReactHookFormErrors() {
2929
// variant={HelpContainerVariant.ERROR}
3030
// title="Het formulier bevat tenminste één niet correct ingevulde rubriek, waardoor het niet opgeslagen kan worden."
3131
>
32-
{!!Object.keys(rhf.formState.errors).length && (
32+
{!!Object.keys(reactHookForm.formState.errors).length && (
3333
<>
3434
<div className="d-flex align-items-center">
3535
Er {multiMistakes ? 'zijn' : 'is'} {numErrors} rubriek
@@ -44,10 +44,12 @@ export default function RenderReactHookFormErrors() {
4444
</div>
4545
{showDetails && (
4646
<ul className="error-list mb-2">
47-
{Object.keys(rhf.formState.errors).map(
47+
{Object.keys(reactHookForm.formState.errors).map(
4848
(fieldKey) => {
4949
const fieldError =
50-
rhf.formState?.errors[fieldKey];
50+
reactHookForm.formState?.errors[
51+
fieldKey
52+
];
5153

5254
const fieldName = getFieldLabelById(
5355
fieldKey,

frontend/packages/pydantic-forms/src/core/PydanticFormContextProvider.tsx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function PydanticFormContextProvider({
154154
]);
155155

156156
// initialize the react-hook-form
157-
const rhf = useForm({
157+
const reactHookForm = useForm({
158158
resolver: zodResolver(zodSchema),
159159
mode: 'all',
160160
defaultValues: initialData,
@@ -163,10 +163,10 @@ function PydanticFormContextProvider({
163163

164164
const awaitReset = useCallback(
165165
async (payLoad: FieldValues = {}) => {
166-
rhf.reset(payLoad);
166+
reactHookForm.reset(payLoad);
167167
await new Promise((resolve) => setTimeout(resolve, 0)); // wait one tick
168168
},
169-
[rhf],
169+
[reactHookForm],
170170
);
171171

172172
const addFormInputData = useCallback(
@@ -185,35 +185,44 @@ function PydanticFormContextProvider({
185185

186186
const submitFormFn = useCallback(() => {
187187
setIsSending(true);
188-
const rhfValues = _.cloneDeep(rhf.getValues());
188+
const reactHookFormValues = _.cloneDeep(reactHookForm.getValues());
189189
awaitReset();
190-
// Note. If we don't use cloneDeep here we are adding a reference to the rhfValues
190+
// Note. If we don't use cloneDeep here we are adding a reference to the reactHookFormValues
191191
// that changes on every change in the form and triggering effects before we want to.
192-
addFormInputData(rhfValues, !!errorDetails);
192+
addFormInputData(reactHookFormValues, !!errorDetails);
193193
window.scrollTo(0, 0);
194-
}, [rhf, errorDetails, addFormInputData, awaitReset, setIsSending]);
194+
}, [
195+
reactHookForm,
196+
errorDetails,
197+
addFormInputData,
198+
awaitReset,
199+
setIsSending,
200+
]);
195201

196202
const onClientSideError = useCallback(
197203
(data?: FieldValues) => {
198204
// TODO implement save with errors toggle
199205
if (data) {
200-
rhf.clearErrors();
206+
reactHookForm.clearErrors();
201207
submitFormFn();
202208
}
203209
},
204-
[rhf, submitFormFn],
210+
[reactHookForm, submitFormFn],
205211
);
206212

207-
const submitForm = rhf.handleSubmit(submitFormFn, onClientSideError);
213+
const submitForm = reactHookForm.handleSubmit(
214+
submitFormFn,
215+
onClientSideError,
216+
);
208217

209218
const resetForm = useCallback(
210219
(e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
211220
e.preventDefault();
212221
setErrorDetails(undefined);
213222
awaitReset();
214-
rhf.trigger();
223+
reactHookForm.trigger();
215224
},
216-
[awaitReset, rhf],
225+
[awaitReset, reactHookForm],
217226
);
218227

219228
const resetErrorDetails = useCallback(() => {
@@ -238,10 +247,10 @@ function PydanticFormContextProvider({
238247
// we check both the SWR hook state as our manual state
239248
isSending: isSending && isLoadingSchema,
240249
isLoading,
241-
rhf,
250+
reactHookForm,
242251
pydanticFormSchema,
243252
loadingComponent,
244-
onPrevious: () => goToPreviousStep(rhf?.getValues()),
253+
onPrevious: () => goToPreviousStep(reactHookForm?.getValues()),
245254
onCancel,
246255
title,
247256
sendLabel,
@@ -319,7 +328,7 @@ function PydanticFormContextProvider({
319328
}
320329

321330
if (onSuccess) {
322-
const values = rhf.getValues();
331+
const values = reactHookForm.getValues();
323332
if (skipSuccessNotice) {
324333
onSuccess(values, apiResponse || {});
325334
} else {
@@ -369,7 +378,7 @@ function PydanticFormContextProvider({
369378
awaitReset(currentStepFromHistory);
370379
}
371380
});
372-
}, [awaitReset, formInputData, formInputHistory, rhf]);
381+
}, [awaitReset, formInputData, formInputHistory, reactHookForm]);
373382

374383
return (
375384
<PydanticFormContext.Provider value={PydanticFormContextState}>

frontend/packages/pydantic-forms/src/core/WrapFieldElement.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ export const WrapFieldElement = ({
1414
pydanticFormField: PydanticFormField;
1515
extraTriggerFields?: string[];
1616
}) => {
17-
const { rhf } = usePydanticFormContext();
17+
const { reactHookForm } = usePydanticFormContext();
1818
return (
1919
<Controller
2020
name={pydanticFormField.id}
21-
control={rhf.control}
21+
control={reactHookForm.control}
2222
render={({ field }) => {
2323
const { onChange, onBlur, value, name } = field;
2424
const onChangeHandle = (val: string) => {
2525
onChange(val);
2626

2727
extraTriggerFields?.forEach((extraField) => {
28-
rhf.trigger(extraField);
28+
reactHookForm.trigger(extraField);
2929
});
3030

3131
// it seems we need this because the 2nd error would get stale..
3232
// https://github.com/react-hook-form/react-hook-form/issues/8170
3333
// https://github.com/react-hook-form/react-hook-form/issues/10832
34-
rhf.trigger(field.name);
34+
reactHookForm.trigger(field.name);
3535
};
3636

3737
return (

frontend/packages/pydantic-forms/src/core/helper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,13 @@ export const getFieldAttributes = function (
449449
* This function can be used as the onValueChange handler in a react hook form form element component.
450450
* When used, it will trigger the related validations whenever the field changes
451451
*
452-
* @param rhf
452+
* @param reactHookForm
453453
* @param field
454454
* @returns
455455
*/
456-
export const rhfTriggerValidationsOnChange =
456+
export const ReactHookFormTriggerValidationsOnChange =
457457
(
458-
rhf: ReturnType<typeof useForm>,
458+
reactHookForm: ReturnType<typeof useForm>,
459459
field: ControllerRenderProps<FieldValues, string>,
460460
) =>
461461
(value: string) => {
@@ -464,7 +464,7 @@ export const rhfTriggerValidationsOnChange =
464464
// it seems we need this because the 2nd error would get stale..
465465
// https://github.com/react-hook-form/react-hook-form/issues/8170
466466
// https://github.com/react-hook-form/react-hook-form/issues/10832
467-
rhf.trigger(field.name);
467+
reactHookForm.trigger(field.name);
468468
};
469469

470470
export const getMatcher = (

0 commit comments

Comments
 (0)