Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): reset field to undefined default in resetField #2425

Openmaricastroc(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2425/headopened

Showing 3 changed files with 141 additions and 5 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/calm-fields-reset.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/form-core': patch
+---
+​
+Fix `form.resetField()` leaving the current value in place when the field's default value is `undefined`. The field is now reset to `undefined`, matching `form.reset()` and `isDefaultValue`. A field-level `defaultValue` of `null` is also restored instead of falling through to the form-level default.
7 additions and 5 deletionspackages/form-core/src/FormApi.tsModified
View file
@@ -2940,7 +2940,10 @@
const fieldDefault =
this.getFieldInfo(field).instance?.options.defaultValue
const formDefault = getBy(this.options.defaultValues, field)
- const targetValue = fieldDefault ?? formDefault
+ const targetValue =
+ fieldDefault !== undefined ? fieldDefault : formDefault
+ const hasDefault =
+ fieldDefault !== undefined || this.options.defaultValues !== undefined
Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
return {
...prev,
@@ -2948,10 +2951,9 @@
...prev.fieldMetaBase,
[field]: defaultFieldMeta,
- values:
- targetValue !== undefined
- ? setBy(prev.values, field, targetValue)
- : prev.values,