Skip to content

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

Open
@ArnoZx

Description

@ArnoZx

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions