From 1f3ef4622bf1b4fea8a81b1bb3e9862aae6a1253 Mon Sep 17 00:00:00 2001 From: "Beier (Bill)" Date: Wed, 13 Dec 2023 08:27:38 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix=20#11287=20disabled=20prop?= =?UTF-8?q?=20cause=20const=20dirtyFields=20update=20(#11291)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐞 fix #11287 disabled prop cause const dirtyFields update * fix issue with test --- src/logic/createFormControl.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/logic/createFormControl.ts b/src/logic/createFormControl.ts index 344ab6b66c5..640293202d5 100644 --- a/src/logic/createFormControl.ts +++ b/src/logic/createFormControl.ts @@ -298,6 +298,7 @@ export function createFormControl< const output: Partial> & { name: string } = { name, }; + const disabledField = get(_fields, name) && get(_fields, name)._f.disabled; if (!isBlurEvent || shouldDirty) { if (_proxyFormState.isDirty) { @@ -306,13 +307,11 @@ export function createFormControl< shouldUpdateField = isPreviousDirty !== output.isDirty; } - const isCurrentFieldPristine = deepEqual( - get(_defaultValues, name), - fieldValue, - ); + const isCurrentFieldPristine = + disabledField || deepEqual(get(_defaultValues, name), fieldValue); - isPreviousDirty = get(_formState.dirtyFields, name); - isCurrentFieldPristine + isPreviousDirty = !disabledField && get(_formState.dirtyFields, name); + isCurrentFieldPristine || disabledField ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true); output.dirtyFields = _formState.dirtyFields;