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 Form Initialization from Record #5146

Merged
merged 2 commits into from
Aug 21, 2020
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
82 changes: 82 additions & 0 deletions packages/ra-core/src/form/FormWithRedirect.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as React from 'react';
import { cleanup } from '@testing-library/react';

import { renderWithRedux } from '../util';
import FormWithRedirect from './FormWithRedirect';
import useInput from './useInput';

describe('FormWithRedirect', () => {
afterEach(cleanup);
const Input = props => {
const { input } = useInput(props);

return <input type="text" {...input} />;
};

it('Does not make the form dirty when reinitialized from a record', () => {
const renderProp = jest.fn(() => (
<Input source="name" defaultValue="Bar" />
));
const { getByDisplayValue, rerender } = renderWithRedux(
<FormWithRedirect
save={jest.fn()}
redirect={false}
saving={false}
version={0}
render={renderProp}
/>
);

expect(renderProp.mock.calls[0][0].pristine).toEqual(true);
expect(getByDisplayValue('Bar')).not.toBeNull();

rerender(
<FormWithRedirect
save={jest.fn()}
redirect={false}
saving={false}
version={0}
render={renderProp}
record={{ id: 1, name: 'Foo' }}
/>
);

expect(renderProp.mock.calls[1][0].pristine).toEqual(true);
expect(getByDisplayValue('Foo')).not.toBeNull();
expect(renderProp).toHaveBeenCalledTimes(2);
});

it('Does not make the form dirty when reinitialized from a different record', () => {
const renderProp = jest.fn(() => (
<Input source="name" defaultValue="Bar" />
));
const { getByDisplayValue, rerender } = renderWithRedux(
<FormWithRedirect
save={jest.fn()}
redirect={false}
saving={false}
version={0}
record={{ id: 1, name: 'Foo' }}
render={renderProp}
/>
);

expect(renderProp.mock.calls[0][0].pristine).toEqual(true);
expect(getByDisplayValue('Foo')).not.toBeNull();

rerender(
<FormWithRedirect
save={jest.fn()}
redirect={false}
saving={false}
version={0}
record={{ id: 1, name: 'Foo', anotherServerAddedProp: 'Bar' }}
render={renderProp}
/>
);

expect(renderProp.mock.calls[1][0].pristine).toEqual(true);
expect(getByDisplayValue('Foo')).not.toBeNull();
expect(renderProp).toHaveBeenCalledTimes(2);
});
});
41 changes: 4 additions & 37 deletions packages/ra-core/src/form/useInitializeFormWithRecord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';
import { useForm } from 'react-final-form';
import { isObject } from '../inference/assertions';
import { merge } from 'lodash';

/**
* Restore the record values which should override any default values specified on the form.
Expand All @@ -13,42 +13,9 @@ const useInitializeFormWithRecord = 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 => {
// 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)) {
if (Array.isArray(record[key])) {
// array of values
record[key].forEach((value, index) => {
if (
isObject(value) &&
Object.keys(value).length > 0
) {
// 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);
}
});
});
const initialValues = form.getState().initialValues;
const initialValuesMergedWithRecord = merge({}, initialValues, record);
form.initialize(initialValuesMergedWithRecord);
Copy link
Member

Choose a reason for hiding this comment

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

This is neat but there is a difference with the previous code: you initialize values including the ones for which there is no associated input. You put that restriction in 3e56efe. Are you sure you're not reintroducing a bug?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did not find any

Copy link
Member

Choose a reason for hiding this comment

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

Including the one from #3721?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll add some tests tomorrow

}, [form, JSON.stringify(record)]); // eslint-disable-line react-hooks/exhaustive-deps
};

Expand Down