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 Warning About Unsaved Change #5776

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions cypress/integration/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,23 @@ describe('Edit Page', () => {
.eq(2)
.should(el => expect(el).to.contain('Sed quo et et fugiat modi'));
});

it('should not display a warning about unsaved changes when an array input has been updated', () => {
ListPagePosts.navigate();
ListPagePosts.nextPage(); // Ensure the record is visible in the table

EditPostPage.navigate();
// Select first notification input checkbox
cy.get(
EditPostPage.elements.input('notifications', 'checkbox-group-input')
)
.eq(0)
.click();

EditPostPage.submit();

// If the update succeeded without display a warning about unsaved changes,
// we should have been redirected to the list
cy.url().then(url => expect(url).to.contain('/#/posts'));
});
});
3 changes: 3 additions & 0 deletions cypress/support/EditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default url => ({
if (type === 'rich-text-input') {
return `.ra-input-${name} .ql-editor`;
}
if (type === 'checkbox-group-input') {
return `.ra-input-${name} label`;
}
if (type === 'reference-array-input') {
return `.ra-input div[role=combobox]`;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-core/src/form/useInitializeFormWithRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const useInitializeFormWithRecord = record => {

const initialValues = form.getState().initialValues;
const initialValuesMergedWithRecord = merge({}, initialValues, record);
form.initialize(initialValuesMergedWithRecord);

// Disable this option when re-initializing the form because in this case, it should reset the dirty state of all fields
// We do need to keep this option for dynamically added inputs though which is why it is kept at the form level
form.setConfig('keepDirtyOnReinitialize', false);
// Ignored until next version of final-form is released. See https://github.com/final-form/final-form/pull/376
// @ts-ignore
form.restart(initialValuesMergedWithRecord);
form.setConfig('keepDirtyOnReinitialize', true);
}, [form, JSON.stringify(record)]); // eslint-disable-line react-hooks/exhaustive-deps
};

Expand Down