Skip to content

form.resetFieldValue() doesn't work for nested fields #1496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LeCarbonator opened this issue May 9, 2025 · 1 comment · May be fixed by #1497
Open

form.resetFieldValue() doesn't work for nested fields #1496

LeCarbonator opened this issue May 9, 2025 · 1 comment · May be fixed by #1497

Comments

@LeCarbonator
Copy link
Contributor

Describe the bug

When calling form.resetFieldValue() on a nested field ('some.nested.field'), it will not reset.

The reason because it doesn't acknowledge the nested field name as a field path, and instead treats it as the literal key in the form values. See this line in FormApi.ts.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-3nazz9pj?file=src%2FApp.tsx

Steps to reproduce

  1. Go to the repro
  2. Change the shallow field and try to reset it. This works.
  3. Change the nested fied and try to reset it. It won't work.

Expected behavior

As a user, I expect the field name to be translated into the nested field value as that's what the typing implies.

How often does this bug happen?

Every time

Screenshots or Videos

Here's the outcome of what's actually happening behind the scenes:

Image

Platform

Any browser

TanStack Form adapter

None

TanStack Form version

v1.10.0

TypeScript version

v5.8.3

Additional context

No response

@LeCarbonator
Copy link
Contributor Author

Failing unit test:

it('should reset nested fields', () => {
  const defaultValues = {
    shallow: '',
    nested: {
      field: {
        name: '',
      },
    },
  }

  const form = new FormApi({
    defaultValues,
  })
  form.mount()

  form.setFieldValue('shallow', 'Shallow')
  form.setFieldValue('nested.field.name', 'Nested')

  expect(form.state.values.shallow).toEqual('Shallow')
  expect(form.state.values.nested.field.name).toEqual('Nested')

  form.resetField('shallow')
  expect(form.state.values.shallow).toEqual('')

  form.resetField('nested.field.name')
  expect(form.state.values.nested.field.name).toEqual('')
})

LeCarbonator added a commit to LeCarbonator/tanstack-form that referenced this issue May 9, 2025
LeCarbonator added a commit to LeCarbonator/tanstack-form that referenced this issue May 9, 2025
LeCarbonator added a commit to LeCarbonator/tanstack-form that referenced this issue May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant