-
Notifications
You must be signed in to change notification settings - Fork 222
Fix Formstate.Nested in Formstate.List #698
Conversation
2131ed3
to
25aa97d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I need a bit of an explanation as to how this is hit, and I'd like @TheMallen to review.
let initialFieldValue: any; | ||
if (initialValue) { | ||
initialFieldValue = initialValue[fieldPath]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would probably be a bit cleaner to do const initialFieldValue = initialValue && initialValue[fieldPath]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit confused how this could ever happen if the field type is not an optional type; that is, in the example case you provided, why is initialValue
undefined? It looks like initialValue
should be {brand: 'brandName1'}
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure - but I think it's du to the fact that an added list item is not present in the initial data passed to the FormState.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the example you provided (very helpful btw), the initial values are definitely present. I wonder if this is really more of a bug in List
not passing down the value to the render prop correctly? Can you log out what fields.description
is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error happens only when you add a new element with the Add variant button via arrayUtils
to the list - this new, second item is not present in the data initially passed to <FormState initialValues=..
, which has only one element in the variants
list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR :)
Code change makes sense (though I agree with chris's point about clarity), can we add a test to make sure this doesn't regress and update the CHANGELOG.md before this ships?
25aa97d
to
e883b36
Compare
e883b36
to
9f2a380
Compare
Using a nested property in a list would result in a "Cannot read property 'brand' of undefined" error when adding new items to the list.
9f2a380
to
9e53b5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I added a test and a changelog entry |
Using a nested property in a list would result in a "Cannot read property
'brand' of undefined" error when adding new items to the list.
The error looks something like this:
This may also address #571
Minimal broken example of broken