form icon indicating copy to clipboard operation
form copied to clipboard

form.Field options does not inherit from form options

Open ljukas opened this issue 1 year ago • 3 comments

Describe the bug

DefaultValue from form instatiation is not inherited by form.field options inside render function

Your minimal, reproducible example

Se code below

Steps to reproduce

const form = useForm({
   defaultValues: {
       name: 'Hello',
   }
})


return (
   <form.Field name="name" children={({ options }) => {
       console.log(options.defaultValue)

      return <View />
   }} />
)

Will log undefined

Expected behavior

This value is only set when setting the defaultValue on the form.Field directly. The defaultValue is retrievable via form.options.defaultValues?.name but I think the value should be Hello when accessing the options object in the render function on the form.Field

Or maybe the best solution would be that the value is

  1. defaultValue defined on the form.Field,
  2. If unassigned in 1, it inherits from the form instantiation

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

React Native

TanStack Form adapter

react-form

TanStack Form version

0.33.0

TypeScript version

No response

Additional context

No response

ljukas avatar Sep 25 '24 09:09 ljukas

@ljukas You can access if you write like that options.form.options.defaultValues

burhanyilmaz avatar Sep 30 '24 08:09 burhanyilmaz

Thats the same workaround using form.options.defaultValues?.name, there still is the options.defaultValue which is the most obvious value to use but that also does not map to the defaultValue set for the form.

ljukas avatar Sep 30 '24 08:09 ljukas

Thats the same workaround using form.options.defaultValues?.name, there still is the options.defaultValue which is the most obvious value to use but that also does not map to the defaultValue set for the form.

we should access the default value via options.defaultValue. I don't know why it is empty

burhanyilmaz avatar Oct 03 '24 08:10 burhanyilmaz

<form.Field name="name" children={({ options }) => {
       console.log(options.defaultValue)

      return <View />
   }} />

This prints undefined correctly because there's no defaultValue passed as prop to the form.Field component. form.options.defaultValues?.name is where you defined the default in this case.

Balastrong avatar Nov 04 '24 21:11 Balastrong