Skip to content

Commit e562676

Browse files
committed
docs: fix useForm API - remove resetField/resetUntouched
These methods are not exported by useForm hook, only by useFormMethods. Docs incorrectly claimed useForm returns resetField and resetUntouched.
1 parent 61a33ab commit e562676

21 files changed

Lines changed: 227 additions & 188 deletions

.editorconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ indent_size = 2
77
end_of_line = lf
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10-
quote_type = single
1110
max_line_length = 120

docs-site/docs/api/useForm.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ function useForm(formName?: string): {
2929
change: EventCallable<IValuePayload>;
3030
erase: EventCallable<void>;
3131
reset: EventCallable<void>;
32-
resetField: EventCallable<string>;
33-
resetUntouched: EventCallable<string[]>;
3432
setActive: EventCallable<IBooleanPayload>;
3533
setValues: EventCallable<IRValues>;
3634
setErrors: EventCallable<IRErrors>;

playwright/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import React from 'react';
2+
3+
// @ts-expect-error css
14
import './index.css';
25

36
import { beforeMount } from '@playwright/experimental-ct-react/hooks';

src/FieldComponent.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import React, { useCallback, useEffect, useMemo, memo } from 'react';
1+
import React, { useCallback, useLayoutEffect, useMemo, memo } from 'react';
22
import type { ComponentType } from 'react';
33
import { useUnit } from 'effector-react';
44
import pickBy from 'lodash/pickBy';
55

66
import { FIELD_CONFIG } from './constants';
7-
import type { IRFieldProps, IValue } from './types';
7+
import type { IFieldProps, IForm, IRFieldProps, IValue } from './types';
88
import { useFormInstance } from './useFormInstance';
99
import { useStoreProp } from './useStoreProp';
1010

11+
const useValue = (form: IForm, name: string) => {
12+
const fieldValue = useStoreProp(form.$values, name);
13+
const format = useStoreProp(form.$fieldsConfig, `${name}.format`, FIELD_CONFIG.format) as IFieldProps['format'];
14+
15+
return useMemo(() => format(fieldValue), [fieldValue, name]);
16+
};
17+
1118
const InternalFieldInst = ({ Field, name, formName, ...rest }: {
1219
name: string;
1320
formName?: string;
@@ -16,7 +23,6 @@ const InternalFieldInst = ({ Field, name, formName, ...rest }: {
1623
const form = useFormInstance(formName);
1724
const [onFieldBlur, onFieldChange] = useUnit([form.onBlur, form.onChange]);
1825

19-
const fieldValue = useStoreProp(form.$values, name);
2026
const error = useStoreProp(form.$error, name, null);
2127
const errors = useStoreProp(form.$errors, name, null);
2228

@@ -28,10 +34,7 @@ const InternalFieldInst = ({ Field, name, formName, ...rest }: {
2834
onFieldBlur({ name, value });
2935
}, [name, onFieldBlur]);
3036

31-
const value = useMemo(() => {
32-
const format = form.configs?.[name]?.format || FIELD_CONFIG.format!;
33-
return format(fieldValue);
34-
}, [fieldValue, form.configs, name]);
37+
const value = useValue(form, name);
3538

3639
return (<Field {...{ error, errors, name, value, onChange, onBlur, ...rest }} />);
3740
};
@@ -57,9 +60,15 @@ export const Field = ({
5760
}: IRFieldProps) => {
5861
const form = useFormInstance(formName);
5962

60-
const [setActive, resetUntouched] = useUnit([form.setActive, form.resetUntouched]);
63+
const formDisableFieldsReinit = useStoreProp(form.$formConfig, 'disableFieldsReinit');
64+
65+
const [setActive, resetUntouched, setFieldConfig] = useUnit([
66+
form.setActive,
67+
form.resetUntouched,
68+
form.setFieldConfig,
69+
]);
6170

62-
useEffect(() => {
71+
useLayoutEffect(() => {
6372
const config = pickBy(
6473
{
6574
parse,
@@ -72,7 +81,7 @@ export const Field = ({
7281
},
7382
(val) => val !== undefined,
7483
);
75-
!passive && form.setFieldConfig({ name, ...config });
84+
!passive && setFieldConfig({ name, ...config });
7685
}, [
7786
disableFieldReinit,
7887
validateOnChange,
@@ -86,7 +95,7 @@ export const Field = ({
8695
form,
8796
]);
8897

89-
useEffect(() => {
98+
useLayoutEffect(() => {
9099
!passive && setActive({ name, value: true });
91100
return () => {
92101
!passive && setActive({ name, value: false });
@@ -96,9 +105,9 @@ export const Field = ({
96105
const reinitDisabled =
97106
!passive && disableFieldReinit !== undefined
98107
? disableFieldReinit
99-
: form.config.disableFieldsReinit;
108+
: formDisableFieldsReinit;
100109

101-
useEffect(() => {
110+
useLayoutEffect(() => {
102111
if (!reinitDisabled && initialValue !== undefined) {
103112
Promise.resolve().then(() => resetUntouched([name]));
104113
}

src/FormComponent.tsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import React, { useLayoutEffect } from 'react';
12
import type { SubmitEvent } from 'react';
2-
import React, { useMemo, useEffect } from 'react';
33
import { useUnit } from 'effector-react';
44
import pickBy from 'lodash/pickBy';
55
import isEmpty from 'lodash/isEmpty';
66

77
import { FORM_CONFIG } from './constants';
8-
import { getForm } from './forms';
9-
import type { IForm, IRFormProps } from './types';
108
import { FormProvider } from './context';
9+
import type { IRFormProps } from './types';
10+
import { useFormInstance } from './useFormInstance';
1111

1212
/**
1313
* Efx Form component
@@ -26,33 +26,19 @@ export const Form = ({
2626
serialize,
2727
...props
2828
}: IRFormProps) => {
29-
const form: IForm = useMemo(() => {
30-
const config = pickBy(
31-
{
32-
keepOnUnmount,
33-
skipClientValidation,
34-
initialValues,
35-
validateOnBlur,
36-
validateOnChange,
37-
validators,
38-
disableFieldsReinit,
39-
serialize,
40-
},
41-
(val) => val !== undefined,
42-
);
43-
return getForm({ name, ...config });
44-
}, [name]);
29+
const form = useFormInstance(name);
4530

46-
const [formSubmit, formReset, resetUntouched] = useUnit([
31+
const [formSubmit, formReset, resetUntouched, setConfig] = useUnit([
4732
form.submit,
4833
form.reset,
4934
form.resetUntouched,
35+
form.setConfig,
5036
]);
5137

5238
/**
5339
* Set config on config props changes
5440
*/
55-
useEffect(() => {
41+
useLayoutEffect(() => {
5642
const config = pickBy(
5743
{
5844
serialize,
@@ -66,7 +52,7 @@ export const Form = ({
6652
},
6753
(val) => val !== undefined,
6854
);
69-
form.setConfig({ name, ...config });
55+
setConfig({ name, ...config });
7056
}, [
7157
skipClientValidation,
7258
disableFieldsReinit,
@@ -83,13 +69,13 @@ export const Form = ({
8369
/**
8470
* Reset form on unmount if enabled
8571
*/
86-
useEffect(() => {
72+
useLayoutEffect(() => {
8773
return () => {
8874
!keepOnUnmount && formReset();
8975
};
9076
}, [formReset, keepOnUnmount]);
9177

92-
useEffect(() => {
78+
useLayoutEffect(() => {
9379
if (!disableFieldsReinit && !isEmpty(initialValues)) {
9480
resetUntouched(Object.keys(initialValues));
9581
}

0 commit comments

Comments
 (0)