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

Fix initialValue is ignored in children of ArrayInput #4810

Merged
merged 2 commits into from
May 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/ra-core/src/form/useInitializeFormWithRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,29 @@ const useInitializeFormWithRecord = record => {
// Using batch ensure we don't get rerenders until all our values are set
form.batch(() => {
Object.keys(record).forEach(key => {
// We have to check the record key is actually registered as a field
// We have to check that the record key is actually registered as a field
// as some record keys may not have a matching input
if (registeredFields.some(field => field === key)) {
form.change(key, record[key]);
if (Array.isArray(record[key])) {
// array of values
record[key].forEach((value, index) => {
if (value && Object.keys(value).length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks up arrays with non "Dictionary" objects, like string for example

Object.keys("cats")
[ '0', '1', '2', '3' ]

// array of objects
Object.keys(value).forEach(key2 => {
form.change(
`${key}[${index}].${key2}`,
value[key2]
);
});
} else {
// array of scalar values
form.change(`${key}[${index}]`, value);
}
});
} else {
// scalar value
form.change(key, record[key]);
}
form.resetFieldState(key);
}
});
Expand Down