Skip to content

Commit 802b047

Browse files
authored
🦘 feat: reset keepFieldsRef options keep fields reference (#12923)
* 🦘 feat: reset keepFieldsRef options keep fields reference * remove additional logic * update test * update api contract
1 parent e56fcee commit 802b047

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

reports/api-extractor.md.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ export type KeepStateOptions = Partial<{
409409
keepIsValidating: boolean;
410410
keepIsValid: boolean;
411411
keepSubmitCount: boolean;
412+
keepFieldsRef: boolean;
412413
}>;
413414

414415
// @public (undocumented)
@@ -901,7 +902,7 @@ export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartia
901902

902903
// Warnings were encountered during analysis:
903904
//
904-
// src/types/form.ts:485:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts
905+
// src/types/form.ts:486:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts
905906

906907
// (No @packageDocumentation comment for this package)
907908

src/__tests__/useForm/watch.test.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,10 @@ describe('watch', () => {
521521
expect(await screen.findByText('1234')).toBeVisible();
522522

523523
expect(watchedData).toEqual([
524+
{},
524525
{},
525526
{
526527
test: '1234',
527-
data: '1234',
528-
},
529-
{
530-
test: '1234',
531-
data: '1234',
532528
},
533529
]);
534530
});

src/logic/createFormControl.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function createFormControl<
129129
errors: _options.errors || {},
130130
disabled: _options.disabled || false,
131131
};
132-
const _fields: FieldRefs = {};
132+
let _fields: FieldRefs = {};
133133
let _defaultValues =
134134
isObject(_options.defaultValues) || isObject(_options.values)
135135
? cloneObject(_options.defaultValues || _options.values) || {}
@@ -1355,20 +1355,23 @@ export function createFormControl<
13551355
}
13561356
}
13571357

1358-
for (const fieldName of _names.mount) {
1359-
const value = get(values, fieldName, get(_defaultValues, fieldName));
1360-
1361-
if (!isUndefined(value)) {
1362-
set(values, fieldName, value);
1358+
if (keepStateOptions.keepFieldsRef) {
1359+
for (const fieldName of _names.mount) {
13631360
setValue(
13641361
fieldName as FieldPath<TFieldValues>,
13651362
get(values, fieldName),
13661363
);
13671364
}
1365+
} else {
1366+
_fields = {};
13681367
}
13691368
}
13701369

1371-
_formValues = cloneObject(values) as TFieldValues;
1370+
_formValues = _options.shouldUnregister
1371+
? keepStateOptions.keepDefaultValues
1372+
? (cloneObject(_defaultValues) as TFieldValues)
1373+
: ({} as TFieldValues)
1374+
: (cloneObject(values) as TFieldValues);
13721375

13731376
_subjects.array.next({
13741377
values: { ...values },

src/types/form.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export type KeepStateOptions = Partial<{
177177
keepIsValidating: boolean;
178178
keepIsValid: boolean;
179179
keepSubmitCount: boolean;
180+
keepFieldsRef: boolean;
180181
}>;
181182

182183
export type SetFieldValue<TFieldValues extends FieldValues> =

src/useForm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ export function useForm<
153153

154154
React.useEffect(() => {
155155
if (props.values && !deepEqual(props.values, _values.current)) {
156-
control._reset(props.values, control._options.resetOptions);
156+
control._reset(props.values, {
157+
keepFieldsRef: true,
158+
...control._options.resetOptions,
159+
});
157160
_values.current = props.values;
158161
updateFormState((state) => ({ ...state }));
159162
} else {

0 commit comments

Comments
 (0)