form icon indicating copy to clipboard operation
form copied to clipboard

`withFieldGroup` array handling with nested

Open TheYoxy opened this issue 5 months ago • 3 comments

Describe the bug

When combining the withFieldGroup for a complex array handling, the nested array element seems out of sync with the actual value of the group.

I've just tested while passing the root form to the SingleFieldWithReorder and the nested elements keep being out of sync (regarding the json value displayed)


Also, this is unrelated but when looking at the extra props of the render function, it seems to pass the fields and the form. Idk if this is expected or if the props must be sanitized when rendering to remove theses values before passing the props to the render method. (They are undocumented and not typed)

Your minimal, reproducible example

https://codesandbox.io/p/devbox/misty-monad-8t29lx?workspaceId=ws_PkVpfRWHUp5vMscgD59gVj

Steps to reproduce

  1. Try to move the value with the move down|move up buttons.
  2. See that the values from the base array mapping are correct, but the values rendered in the element aren't.

Expected behavior

I think the nested group value should remain sync with the array mapping from the parent group

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Browser: Tested on chrome 138.0.7204.183 (Official Build) (64-bit)

TanStack Form adapter

react-form

TanStack Form version

v1.15.1

TypeScript version

No response

Additional context

No response

TheYoxy avatar Jul 31 '25 10:07 TheYoxy

I encountered the same issue. Looks like this happens even without nesting withFieldGroup components. https://codesandbox.io/p/devbox/suspicious-elgamal-79j4tr?workspaceId=ws_MqxYqe2Tbvb7TuAH8jpm52

Seems like the reason for this is that useFieldGroup doesn't react to changes to opts.fields. I suppose FieldGroupApi should have something similar to FieldApi's update method, which will then be used in useFieldGroup:

  useIsomorphicLayoutEffect(() => {
    formLensApi.update(opts)
  })

@LeCarbonator can you confirm?

vkhalikov avatar Aug 26 '25 03:08 vkhalikov

I suppose that reactive fields should be possible and is not considered by the field group at the moment.

I was initially hesitant to check this since there's array-related issues that should be solved first before determining that this is field group related instead of a core issue of arrays.

The examples help though! I'll have a look sometime.

LeCarbonator avatar Aug 26 '25 05:08 LeCarbonator

In case someone needs a quick solution: Adding a key prop to a withFieldsGroup component when rendering mitigates the issue by forcing it to remount.

Here's an updated sandbox with side by side comparison. https://codesandbox.io/p/devbox/condescending-wind-2p6qk4?workspaceId=ws_MqxYqe2Tbvb7TuAH8jpm52

I also tried using useMemo instead of constant-like useState in useFieldGroup and it solved the issue in my case. I am willing to investigate it further and hopefully open a PR when I have free time.

vkhalikov avatar Aug 26 '25 13:08 vkhalikov