Skip to content
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

Moving fields with different shapes, some fields' functions gone #51

Open
ArnoZx opened this issue Apr 22, 2020 · 1 comment
Open

Moving fields with different shapes, some fields' functions gone #51

ArnoZx opened this issue Apr 22, 2020 · 1 comment

Comments

@ArnoZx
Copy link

ArnoZx commented Apr 22, 2020

Are you submitting a bug report or a feature request?

Bug report

What is the current behavior?

After moving fields with different shapes, some fields' change, blur and focus functions were gone.

What is the expected behavior?

Maybe these functions should be restored or recreated.

test case below would fail with TypeError: state.fields.foo[1].cat.change is not a function

it('should preserve functions in field state when fields with different shapes', () => {
  // implementation of changeValue taken directly from Final Form
  const changeValue = (state, name, mutate) => {
    const before = getIn(state.formState.values, name)
    const after = mutate(before)
    state.formState.values = setIn(state.formState.values, name, after) || {}
  }
  const state = {
    formState: {
      values: {
        foo: [{ dog: 'apple dog', cat: 'apple cat' }, { dog: 'banana dog' }]
      }
    },
    fields: {
      'foo[0].dog': {
        name: 'foo[0].dog',
        touched: true,
        error: 'Error A Dog',
        change: () => 'A Dog'
      },
      'foo[0].cat': {
        name: 'foo[0].cat',
        touched: false,
        error: 'Error A Cat',
        change: () => 'A Cat'
      },
      'foo[1].dog': {
        name: 'foo[1].dog',
        touched: true,
        error: 'Error B Dog',
        change: () => 'B Dog',
      }
    }
  }
  move(['foo', 0, 1], state, { changeValue })
  expect(state.formState).toMatchObject({values: {
    foo: [{ dog: 'banana dog' }, { dog: 'apple dog', cat: 'apple cat' }]
  }})
  expect(state.fields['foo[0].dog'].change()).toBe('A Dog')
  expect(state.fields['foo[1].dog'].change()).toBe('B Dog')
  
  // this expect would fail with `TypeError: state.fields.foo[1].cat.change is not a function`
  expect(state.fields['foo[1].cat'].change()).toBe('A Cat')
})

What's your environment?

"final-form-arrays": "3.0.2",

Other information

@SeqviriouM
Copy link

SeqviriouM commented May 22, 2020

I have the same bug with unshift method.

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

No branches or pull requests

2 participants