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

State-management on insert/... does not work on nested arrays #37

Closed
PerfectPixel opened this issue Aug 8, 2019 · 1 comment · Fixed by #38
Closed

State-management on insert/... does not work on nested arrays #37

PerfectPixel opened this issue Aug 8, 2019 · 1 comment · Fixed by #38

Comments

@PerfectPixel
Copy link
Contributor

PerfectPixel commented Aug 8, 2019

Bug or Feature?

BUG

What is the current behavior?

In nested arrays, state is not correctly managed for operations like insert:

1. step

In the following image I added two customers to my array, their names are set via initialValue on field level to Adam and Sam, both fields are not dirty. Then I press the + next to Adam to add another entry between both of them.
1

2. step

Now, the new second entry is:

  1. not given an initial name
  2. the field is dirty as it is for final form changed from Sam to ``
    The third entry (Sam) is:
  3. not Sam anymore but Sean
  4. if the field would have been dirty, it would be now not dirty
    Lets press the x on the new entry next.
    2

3. step

This time, we get to keep Sean (formerly known as Sam), but the field is now dirty.
3

Analysis

One of the causes I could identify, was the use of new RegExp("^" + name + "...). As our name is something[0].customers we would actually need to escape all regex special chars like [, [ and . to retrieve a functioning expression. With the current regex, tokens will always be null and, therefore, the state is never moved.
code

On a side-note, the regex is also broken for non-nested arrays:
^some.path.customers\[(\d+)\](.*) does not only match some.path.customers[0] but also someapathacustomers[0]. The . would already need escaping.

https://codesandbox.io/s/react-final-form-field-arrays-6ikh2?fontsize=14

What is the expected behavior?

State should be correctly managed. e.g. if an item is inserted between two items, the state of the original item should be moved.

Sandbox Link

https://codesandbox.io/s/react-final-form-field-arrays-6ikh2?fontsize=14

What's your environment?

Newest versions (see sandbox)

@erikras I am happy to help fixing the issues. One of the options is to escape special chars within the name or do a string comparison, maybe by using toPath and then compare the segments.

@erikras
Copy link
Member

erikras commented Nov 20, 2019

Published fix in v3.0.2.

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

Successfully merging a pull request may close this issue.

2 participants