TanStack / form

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
https://tanstack.com/form
MIT License
3.74k stars 338 forks source link

Moving subfields of array field mix their values #692

Open pedro757 opened 6 months ago

pedro757 commented 6 months ago

Describe the bug

When the input is still idle (I mean undefined) and you try to change its position it takes the value of other subfields. It messes things up.

Codesandbox

https://codesandbox.io/p/sandbox/focused-pond-s96spf?file=%2Fsrc%2FApp.js%3A169%2C8

Steps to reproduce

  1. Try to move the empty subfields dragging and dropping in other position
  2. Now try to move subfields with non-empty values

Expected behavior

I expect it to keep its value.

crutchcorn commented 6 months ago

I think this is a bug with our moveValue method. I'll investigate in a bit

t-rosa commented 4 months ago

I think this is a bug with our moveValue method. I'll investigate in a bit

If it saves a little time, after some research, I wrote a test to check whether there was a problem with the moveValue function, but the test went well.

I wonder if the problem isn't the collision between an uncontrolled and a controlled field, and that screws up the ui.

  it('should correctly move an array value, even with undefined values', () => {
    const form = new FormApi({
      defaultValues: {
        users: [
          {
            name: undefined,
          },
          {
            name: 'one',
          },
          { name: 'two' },
          {
            name: 'three',
          },
          {
            name: undefined,
          },
        ],
      },
    })

    const field = new FieldApi({
      form,
      name: 'users',
    })

    field.moveValue(3, 4)

    expect(field.getValue()).toStrictEqual([
      {
        name: undefined,
      },
      { name: 'one' },
      { name: 'two' },
      {
        name: undefined,
      },
      {
        name: 'three',
      },
    ])
  })
crutchcorn commented 4 months ago

Hey @Balastrong, was this fixed when you updated your move and insert stuff?

Balastrong commented 4 months ago

Unfortunately I think there's still something not behaving here. I made a short example: https://stackblitz.com/edit/tanstack-form-grdmqr?file=src%2Findex.tsx&preset=node

First issue is that indeed having <input value={subField.state.value} ... /> can't work if value changes from string to undefined and vice-versa when moving values, but this may be on React and subField.state.value ?? '' can help.

The second issue is that if we use the move button and then we submit, the old value "one" remains in the input, but again this is probably still related to undefined being put there.

Balastrong commented 4 months ago

This test on react-form fails because magically the second inputs gets back the value 'one' on submit. The same operation (move then submit) on form-core passes.

Failing Test

```tsx it('should correctly move an array value, even with undefined values and not alter the state after submit', async () => { function Comp() { const form = useForm({ defaultValues: { people: [ { name: undefined, }, { name: 'one', }, ], }, }) return (

{ e.preventDefault() e.stopPropagation() form.handleSubmit() }} > {(field) => { return (
{field.state.value.map((_, i) => { return ( {(subField) => { return (
) }}
) })}
) }}
[state.canSubmit, state.isSubmitting]} children={([canSubmit, isSubmitting]) => ( <> )} />
) } const { getByText, findByLabelText, queryByText, findByText } = render( , ) const input = await findByLabelText('Name for person 0') expect(input).toBeInTheDocument() expect(input).toHaveValue('') const input2 = await findByLabelText('Name for person 1') expect(input2).toBeInTheDocument() expect(input2).toHaveValue('one') await user.click(getByText('Move 1 to 0')) expect(input).toHaveValue('one') expect(input2).toHaveValue('') await user.click(await findByText('Submit')) expect(input).toHaveValue('one') expect(input2).toHaveValue('') // This gets value 'one' }) ```