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

Simplify form reset on record change, and forward supported props from useAugmentedForm to useForm #8911

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export * from './useFormGroup';
export * from './useFormGroups';
export * from './useFormGroupContext';
export * from './useGetValidationErrorMessage';
export * from './useInitializeFormWithRecord';
export * from './useNotifyIsFormInvalid';
export * from './useAugmentedForm';
export * from './useInput';
Expand Down
23 changes: 3 additions & 20 deletions packages/ra-core/src/form/useAugmentedForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
delayError,
formRootPathname,
mode,
resetOptions,
resolver,
reValidateMode = 'onChange',
onSubmit,
Expand Down Expand Up @@ -81,7 +82,8 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
const form = useForm({
context,
criteriaMode,
defaultValues: defaultValuesIncludingRecord,
values: defaultValuesIncludingRecord,
resetOptions,
delayError,
mode,
reValidateMode,
Expand All @@ -93,25 +95,6 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {

const formRef = useRef(form);

// initialize form with record
/* eslint-disable react-hooks/exhaustive-deps */
useEffect(() => {
if (!record) {
return;
}
const initialValues = getFormInitialValues(defaultValues, record);
form.reset(initialValues);
}, [
JSON.stringify({
defaultValues:
typeof defaultValues === 'function'
? 'function'
: defaultValues,
record,
}),
]);
/* eslint-enable react-hooks/exhaustive-deps */

// notify on invalid form
useNotifyIsFormInvalid(form.control);

Expand Down
18 changes: 0 additions & 18 deletions packages/ra-core/src/form/useInitializeFormWithRecord.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ describe('<AutocompleteInput />', () => {
<SimpleForm
mode="onBlur"
onSubmit={jest.fn()}
defaultValues={{ role: 2 }}
defaultValues={{ language: 'ang' }}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me these tests have nothing to do with changing the defaultValues (I think it's a copy paste error), hence I changed that to keep the same defaultValues across rerenders.

>
<AutocompleteInput
source="language"
Expand Down Expand Up @@ -1184,7 +1184,7 @@ describe('<AutocompleteInput />', () => {
<SimpleForm
mode="onBlur"
onSubmit={jest.fn()}
defaultValues={{ role: 2 }}
defaultValues={{ language: 'ang' }}
>
<AutocompleteInput
source="language"
Expand Down Expand Up @@ -1250,7 +1250,7 @@ describe('<AutocompleteInput />', () => {
<SimpleForm
mode="onBlur"
onSubmit={jest.fn()}
defaultValues={{ role: 2 }}
defaultValues={{ language: 'ang' }}
>
<AutocompleteInput
source="language"
Expand Down