`isDefaultValue` is `true` when field value is `undefined`, and also for two distinct values #1973
Describe the bug#
Currently isDefaultValue reports true when the field value matches the form-level default, or the field-level default, with the following consequences:
When the two defaults disagree, isDefaultValue reports true for both of them.
When only one default is specified, the other (missing) default is still tested against. As a result, undefined is always treated as a default value, which is unfortunate for forms that want to use undefined as one of the possible field values.
Proposal#
The goal is for isDefaultValue to be true if and only if form.reset() would make no changes. form.reset() picks a value to use. isDefaultValue could do the same: select a default first, then evaluate(curFieldValue, selectedDefault).
In my testing, when both defaults are specified the initial render uses field-default, and field-default also "wins" in form.reset(). So I'm thinking to change the current implementation from this:
isDefaultValue = evalute(curValue, formDefaultValue) || evaluate(curValue, fieldDefaultValue)
to this:
isDefaultValue = evalute(curValue, fieldDefaultValue ?? formDefaultValue)
This addresses both scenarios: the (somewhat nonsensical) disagreeing defaults, and the unwanted isDefaultValue=true when the field value is undefined.
I could have a go at a PR if this change is acceptable.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/tanstack-form-isdefaultvalue-1-n9f6zn
Steps to reproduce#
In Example 1 on CodeSandbox, entering <undefined> sets the form value to undefined, and isDefaultValue becomes true. However clicking form.reset() changes it back to foo, which is the true default - showing that isDefaultValue=true was a lie. In my real code I ran into this while attempting to map an empty string to a luxon DateTime | undefined field.
In Example 2, entering both foo-field and foo-form results in isDefaultValue=true. But the initial value is foo-field, and that is also what we get if we form.reset(). So it should be the only value for which isDefaultValue returns true, even if this example is a bit of a misuse of default values.
Expected behavior#
isDefaultValue should be true if and only if form.reset() has no changes to make, because every value is already the default value.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
Windows, Firefox 146
TanStack Form adapter#
None
TanStack Form version#
1.27.7
TypeScript version#
5.9.3
Additional context#
No response
LeCarbonator(GitHub profile, opens in a new tab) added labelstype: bug
Since
evaluateis exported, I thought this might be a good temporary workaround:const isDefaultValue = useStore(form.store, s => evaluate(s.values, form.options.defaultValues));
Unfortunately this does not work because of https://github.com/TanStack/form/issues/1051 - or more specifically, it works fine until the form is submitted, at which point there is no way to get an updated version of
form.options.defaultValues.crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Reasonable proposal! I'll see if I can make a PR for it this week.