Skip to content

Commit 495986a

Browse files
authored
Merge pull request #8911 from marmelab/Simplify-form-reset-on-record-change
Simplify form reset on record change, and forward supported props from `useAugmentedForm` to `useForm`
2 parents b95ae6f + ef3a0f4 commit 495986a

4 files changed

Lines changed: 6 additions & 54 deletions

File tree

packages/ra-core/src/form/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export * from './useFormGroup';
4141
export * from './useFormGroups';
4242
export * from './useFormGroupContext';
4343
export * from './useGetValidationErrorMessage';
44-
export * from './useInitializeFormWithRecord';
4544
export * from './useNotifyIsFormInvalid';
4645
export * from './useAugmentedForm';
4746
export * from './useInput';

packages/ra-core/src/form/useAugmentedForm.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,16 @@ import { sanitizeEmptyValues as sanitizeValues } from './sanitizeEmptyValues';
3838
*/
3939
export const useAugmentedForm = (props: UseAugmentedFormProps) => {
4040
const {
41-
context,
4241
criteriaMode = 'firstError',
4342
defaultValues,
44-
delayError,
4543
formRootPathname,
46-
mode,
4744
resolver,
4845
reValidateMode = 'onChange',
4946
onSubmit,
5047
sanitizeEmptyValues,
51-
shouldFocusError,
52-
shouldUnregister,
53-
shouldUseNativeValidation,
5448
warnWhenUnsavedChanges,
5549
validate,
50+
...rest
5651
} = props;
5752
const record = useRecordContext(props);
5853
const saveContext = useSaveContext();
@@ -79,39 +74,15 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
7974
: undefined;
8075

8176
const form = useForm({
82-
context,
8377
criteriaMode,
84-
defaultValues: defaultValuesIncludingRecord,
85-
delayError,
86-
mode,
78+
values: defaultValuesIncludingRecord,
8779
reValidateMode,
8880
resolver: finalResolver,
89-
shouldFocusError,
90-
shouldUnregister,
91-
shouldUseNativeValidation,
81+
...rest,
9282
});
9383

9484
const formRef = useRef(form);
9585

96-
// initialize form with record
97-
/* eslint-disable react-hooks/exhaustive-deps */
98-
useEffect(() => {
99-
if (!record) {
100-
return;
101-
}
102-
const initialValues = getFormInitialValues(defaultValues, record);
103-
form.reset(initialValues);
104-
}, [
105-
JSON.stringify({
106-
defaultValues:
107-
typeof defaultValues === 'function'
108-
? 'function'
109-
: defaultValues,
110-
record,
111-
}),
112-
]);
113-
/* eslint-enable react-hooks/exhaustive-deps */
114-
11586
// notify on invalid form
11687
useNotifyIsFormInvalid(form.control);
11788

packages/ra-core/src/form/useInitializeFormWithRecord.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ describe('<AutocompleteInput />', () => {
11191119
<SimpleForm
11201120
mode="onBlur"
11211121
onSubmit={jest.fn()}
1122-
defaultValues={{ role: 2 }}
1122+
defaultValues={{ language: 'ang' }}
11231123
>
11241124
<AutocompleteInput
11251125
source="language"
@@ -1184,7 +1184,7 @@ describe('<AutocompleteInput />', () => {
11841184
<SimpleForm
11851185
mode="onBlur"
11861186
onSubmit={jest.fn()}
1187-
defaultValues={{ role: 2 }}
1187+
defaultValues={{ language: 'ang' }}
11881188
>
11891189
<AutocompleteInput
11901190
source="language"
@@ -1250,7 +1250,7 @@ describe('<AutocompleteInput />', () => {
12501250
<SimpleForm
12511251
mode="onBlur"
12521252
onSubmit={jest.fn()}
1253-
defaultValues={{ role: 2 }}
1253+
defaultValues={{ language: 'ang' }}
12541254
>
12551255
<AutocompleteInput
12561256
source="language"

0 commit comments

Comments
 (0)