-
-
Notifications
You must be signed in to change notification settings - Fork 40
Fix and optimize all mutators #61
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #61 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 15 13 -2
Lines 165 175 +10
Branches 36 37 +1
=========================================
+ Hits 165 175 +10
Continue to review full report at Codecov.
|
Don't merge yet. I realize I can simplify the logic and get rid of the sorting on fieldIndex |
Revert changes from final-form#39 that moves `fieldSubscribers`, causing the wrong field to be notified.
985a7d3
to
bba52f4
Compare
Change `remove` and `removeBatch` behavior to set array value to undefined when all items have been removed.
… field or looping over fields many times.
bba52f4
to
71bebc7
Compare
I hope that it'll be merged soon. |
@erikras Could you tell if you gonna review/merge it soon? Because right now shift and insert methods are broken and I really need them in my application. |
@erikras pinging you on this. There seem to be some pretty fundamental issues that need to be addressed in this lib. Thanks for all the work! |
This change broke our app because suddenly field array value was undefined instead of empty array when last item was removed. I don't understand the logic. |
The main bug fix in this PR fixed all the mutators except push that were broken and basically unusable. The case @huan086 made was quite valid that the arrays behave differently when you clear them all other fields default to undefined in the empty state and this leads to pristine being wrong when your do not define initial values and then add and remove all elements from the array. Note that the new release was a minor change and not just a patch which is guaranteed to be compatible. To get the behaviour as it was previously you can have a look at an identity function for the parse callback. |
But if you have defined an initial value of |
The strings in FF have been working with this approach. If you define an empty string as an initial value and then add something to the field and remove it you end up with an undefined value after the change. The behaviour you want is not implemented for strings either (why should arrays be treated differently?) The change is quite simple and is documented in how to accomplish the alternative you need at https://final-form.org/docs/react-final-form/types/FieldProps#parse Compare the sequence below in the codesandbox in the firstName field and lastName field. https://codesandbox.io/s/zealous-julien-4qlo0l?file=/index.js The initial value for both: |
Thanks for the info! I agree arrays should work the same way as strings, though I guess that just means I would also like strings to work differently. 😅 (Though not expecting that.) Thanks for pointing out Maybe there could be an option in e.g. |
Actually, it came to my mind that arrays are different — we even have this separate package for dealing with them. I believe in the most common use case, the form value contains values for a collection of inputs, not for a single input. So it's an added layer that doesn't correspond to a native input, and therefore I think a different behavior would be justifiable. |
Hi @peruukki, as the author of this pull request, I don't like the current behaviour actually. The decision to make values undefined dates back to redux-forms, and I think it was designed this way so that pristine state gets computed correctly, where initial state is set to Ideally, the initial value should be consulted, so that pristine calculation is even more correct. E.g. Initial I am not sure how to implement the ideal behaviour while keeping the performance of the code. The current react-final-forms already performs poorly compared to redux-forms where I keyed array items with an id. Please contribute to make this library better when you have the time, thanks! |
This pull request depends on final-form/final-form#393
Fixes #58
Closes #43
Closes #45
I have edited the unit tests to trigger bugs, or to remove testing of implementation details.
Potentially breaking change
Change
remove
andremoveBatch
behavior to set array value to undefined when all items have been removedI think this change would make pristine calculation more correct