Skip to content

Commit

Permalink
Fix Form Become Dirty After Record Initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Sep 20, 2019
1 parent cbdaaa2 commit 3e56efe
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/ra-core/src/form/useInitializeFormWithRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ const useInitializeFormWithRecord = (form, record) => {
return;
}

const registeredFields = form.getRegisteredFields();

// react-final-form does not provide a way to set multiple values in one call.
// Using batch ensure we don't get rerenders until all our values are set
form.batch(() => {
Object.keys(record).forEach(key => {
form.change(key, record[key]);
// We have to check the record key is actually registered as a field
// as some of record keys may not have a matching input
if (registeredFields.some(field => field === key)) {
form.change(key, record[key]);
form.resetFieldState(key);
}
});
});
}, []); // eslint-disable-line
}, [form, record]);
};

export default useInitializeFormWithRecord;

0 comments on commit 3e56efe

Please sign in to comment.