Support for ImmutableJS setIn array paths #1716
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
ImmutableJS's
setIn
(and therebyupdateIn
/deleteIn
/ etc...) do not seem architected to work with a mixture ofMap
andList
objects. The call assumes that an "unset" value can only be one thing, and only has one resolution path for when that value is empty. (Namely, add it as an empty map) See Immutable-js SourceThis leads to the issue found in #1574 where we get the infamous error:
When using immutable-js with
FieldArray
. The focus / blur events are callingsetIn
onfields.fieldName[0].touched
etc... which causes thefieldName
part of the tree to be instantiated asMap
instead of aList
.{ "fields": { "fieldName": { "0": { "touched": true } } } }
{ "fields": { "fieldName": [ { "touched": true } ] } }
Solution
We need to pre-seed a mixed type path like
fields.thing[0].visisted
with emptyList
objects at each array breakout point. This allows immutable to simply update the existingList
with a call toset()
instead of trying to instantiate it as aMap
I have broken the
structure/immutable
'ssetIn
method out into it's own file to allow us to take some liberties with it.Extra
Along with this PR I've included regression tests for the
setIn
behaviors I fixed as well as some tests for some of the expected vanillasetIn
behavior the library depends on.