Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): handle null field defaults #2429

Openfenet-as(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2429/headopened

Showing 3 changed files with 48 additions and 4 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/null-field-default-value.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/form-core': patch
+---
+​
+Treat field-level `defaultValue: null` as a valid default value instead of falling back to the form-level default when resolving `isDefaultValue` and in `resetField`.
7 additions and 4 deletionspackages/form-core/src/FormApi.tsModified
View file
@@ -1233,11 +1233,13 @@
// As primitives, we don't need to aggressively persist the same referential value for performance reasons
const isFieldValid = !isNonEmptyArray(fieldErrors)
const isFieldPristine = !currBaseMeta.isDirty
+ const fieldDefault =
+ this.getFieldInfo(fieldName).instance?.options.defaultValue
const isDefaultValue = evaluate(
curFieldVal,
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
- this.getFieldInfo(fieldName)?.instance?.options.defaultValue ??
- getBy(this.options.defaultValues, fieldName),
+ fieldDefault !== undefined
+ ? fieldDefault
+ : getBy(this.options.defaultValues, fieldName),
@@ -2940,7 +2942,8 @@
const fieldDefault =
this.getFieldInfo(field).instance?.options.defaultValue
const formDefault = getBy(this.options.defaultValues, field)