feat(form-core): field meta isDefaultValue #1456
Mergedharry-whorlow(GitHub profile, opens in a new tab)merged 16 commits into
main fromrefs/pull/1456/headmerged by harry-whorlow(GitHub profile, opens in a new tab) - packages/form-core/tests/FieldApi.spec.ts line 81Resolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -77,5 +78,3 @@7778 name: 'name',7879 defaultMeta: {7980 isTouched: true,80- isDirty: true,81- isPristine: false,
- packages/form-core/src/utils.ts line 8OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -5,4 +5,3 @@55 ValidationSource,66 } from './types'77 import type { FormValidators } from './FormApi'8-import type { AnyFieldMeta, FieldValidators } from './FieldApi'
- packages/form-core/src/utils.ts line 447OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -443,3 +443,5 @@443443444444 return { newErrorValue: undefined, newSource: undefined }445445 }446+447+export const deepEqual = <T>(a: T, b: T): boolean => {
Balastrong(GitHub profile, opens in a new tab) requested changes
packages/form-core/tests/FieldApi.spec.ts line 102OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -85,16 +85,18 @@85858686 field.mount()878788- expect(field.getMeta()).toEqual({88+ expect(field.getMeta()).toMatchObject({8989 isTouched: true,9090 isBlurred: true,91- isValidating: false,9291 isDirty: true,93- isPristine: false,92+93+ // derived meta data9494 isValid: true,95+ isValidating: false,9596 errors: [],9697 errorMap: {},9798 errorSourceMap: {},9899 })99100 })100101102+ it('should update the fields meta isDefaultValue', () => {packages/form-core/src/FormApi.ts line 929OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -912,5 +922,8 @@912922 }913923914924 // As primitives, we don't need to aggressively persist the same referential value for performance reasons915- const isFieldPristine = !currBaseVal.isDirty916925 const isFieldValid = !isNonEmptyArray(fieldErrors ?? [])926+ const isFieldPristine = !currBaseMeta.isDirty927+ const isDefaultValue = deepEqual(928+ curFieldVal,929+ this.options.defaultValues?.[fieldName as never],packages/form-core/src/FormApi.ts line 902OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -883,14 +888,15 @@883888 for (const fieldName of Object.keys(884889 currBaseStore.fieldMetaBase,885890 ) as Array<keyof typeof currBaseStore.fieldMetaBase>) {886- const currBaseVal = currBaseStore.fieldMetaBase[891+ const currBaseMeta = currBaseStore.fieldMetaBase[887892 fieldName as never888893 ] as AnyFieldMetaBase889894890- const prevBaseVal = prevBaseStore?.fieldMetaBase[895+ const prevBaseMeta = prevBaseStore?.fieldMetaBase[891896 fieldName as never892897 ] as AnyFieldMetaBase | undefined893898894899 const prevFieldInfo =895900 prevVal?.[fieldName as never as keyof typeof prevVal]896901902+ const curFieldVal = currBaseStore.values[fieldName as never]docs/framework/react/guides/basic-concepts.md lines 97 to 101OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -92,4 +92,10 @@9292 } = field.state9393 ```949495-There are three field states that can be useful to see how the user interacts with a field: A field is _"touched"_ when the user clicks/tabs into it, _"pristine"_ until the user changes value in it, and _"dirty"_ after the value has been changed. You can check these states via the `isTouched`, `isPristine` and `isDirty` flags, as seen below.95+There are three states in the metadata that can be useful to see how the user interacts with a field:96+97+_"touched"_, after the user clicks/tabs into the field98+_"pristine"_, until the user changes the field value99+_"dirty"_, after the fields value has been changed100+101+You can check these states via the `isTouched`, `isPristine`, and `isDirty` flags, as seen below.- packages/form-core/src/FormApi.ts lines 1007 to 1009OutdatedResolvedby harry-whorlow(GitHub profile, opens in a new tab)@@ -981,3 +1004,6 @@98110049821005 const isTouched = fieldMetaValues.some((field) => field.isTouched)9831006 const isBlurred = fieldMetaValues.some((field) => field.isBlurred)1007+ const isDefaultValue = fieldMetaValues.some(1008+ (field) => field.isDefaultValue,1009+ )
Balastrong(GitHub profile, opens in a new tab) approved these changes
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 12b94be9ca888f2840616f0e884cea848a37d764.
nx affected --targets=test:sherif,test:knip,tes...nx run-many --target=build --exclude=examples/**☁️ Nx Cloud last updated this comment at
2025-05-07 17:43:24UTC