`isDefaultValue` is wrong when a field-level `defaultValue` is `null`
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 76/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- typescript
- Área
- frontend
Línea de trabajo
Lee la derivación del valor predeterminado en packages/form-core/src/FormApi.ts alrededor de las líneas enlazadas 1236–1241 y compárala con el manejo de valores predeterminados a nivel de campo descrito para FieldApi y form.reset(). Reproduce el problema con el fragmento de TypeScript que usa @tanstack/form-core. Se considera terminado cuando isDefaultValue es true exactamente para el valor que restaura reset, incluso cuando el valor predeterminado del campo es null.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
When a field has defaultValue: null and the form-level default for that field is anything other than null (missing, undefined, or a different value), isDefaultValue does not match what form.reset() restores:
- The field holds its default (
null) butfield.state.meta.isDefaultValueandform.state.isDefaultValuearefalse, right after mount and afterform.reset(). - Setting the value to the form-level default (or to
undefinedwhen there is none) reportsisDefaultValue: true, althoughform.reset()then changes the value back tonull.
Field-level defaults of 0, false, '' or any non-null value work as expected.
Your minimal, reproducible example
The snippet under "Steps to reproduce" is self-contained and only needs @tanstack/form-core.
Steps to reproduce
import { FieldApi, FormApi } from '@tanstack/form-core'
const form = new FormApi({
defaultValues: { name: 'form' as string | null },
})
form.mount()
const field = new FieldApi({ form, name: 'name', defaultValue: null })
field.mount()
console.log(field.state.value, field.state.meta.isDefaultValue)
// null false (expected: null true)
field.setValue('form')
console.log(field.state.meta.isDefaultValue)
// true (expected: false)
form.reset()
console.log(field.state.value, field.state.meta.isDefaultValue)
// null false (expected: null true)
Without a form-level default for the field (defaultValues: {} as { name?: string | null }), the result is the same, and field.setValue(undefined) reports isDefaultValue: true.
Expected behavior
isDefaultValue should be true exactly when the current value is the one form.reset() restores, as established in #1973 and #2006. With a field-level defaultValue: null that value is null.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
macOS, reproduced in Node with Vitest (no browser involved)
TanStack Form adapter
vanilla
TanStack Form version
v1.33.5
TypeScript version
v5.9.3
Additional context
The derivation resolves the default with fieldDefault ?? formDefault, so a field-level null falls through to the form-level default:
https://github.com/TanStack/form/blob/2216fdedf0a5ff17e4ef8567e1321442639b96ca/packages/form-core/src/FormApi.ts#L1236-L1241
form.reset() and FieldApi treat a field-level default as set when it is !== undefined, so null is applied as a default there. This was introduced in #2006 (1.32.0) and noted in an unanswered review comment: https://github.com/TanStack/form/pull/2006#discussion_r3215386716
Related: #2425 fixes the same ?? in resetField. This issue is separate, since it also affects mount and form.reset().
Happy to open a PR if a fix is wanted on the 1.x line.
- Lenguaje dominante
- TypeScript
- Estrellas
- 6.7k
- Forks
- 693
- Merge medio
- 1 d 9 h
- PR fusionados (30 d)
- 12
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de TanStack/form
-
Async validator doesn't re-trigger `isValidating` after first failed validationPosiblemente ocupada @drewsephski la tomó hace 5 días. Abierto
Dificultad 2/5 1-2 días Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
[v2 alpha] devtools are always light modePosiblemente ocupada @dikshit-n la tomó hace 39 días. Abiertoarea: runtime scope: devtools type: bug v2
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
FormGroupApi.getRelatedFields and FieldApi.validate() use unguarded startsWith for group membership, matching unrelated fields with a shared name prefixPosiblemente ocupada @HDPark95 la tomó hace 58 días. Abiertoarea: runtime scope: core type: bug v1
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
TanStack/form#2337 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
FormApi: onSubmit/onServer error cleared on any non-matching validation cause, not just 'change'Quizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abiertov1 v2: needs investigation
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
setFieldValue clears errorMap.onMount but leaves errorSourceMap.onMount stalePosiblemente ocupada @alliasgher la tomó hace 45 días. Abiertov1 v2: needs investigation
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
TanStack/form#2294 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de TanStack/form
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
db-ux-design-system/core-web#8461 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Cannot run an integration on windows when power shell is selected as the default profile terminalAbiertoPriority/Blocker Type/Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
wso2/product-integrator#2767 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
-
severity:low
Dificultad 1/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día