Skip to content

Commit 64980a0

Browse files
committed
Simplify form reset on record change
1 parent b95ae6f commit 64980a0

4 files changed

Lines changed: 6 additions & 42 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 & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
4444
delayError,
4545
formRootPathname,
4646
mode,
47+
resetOptions,
4748
resolver,
4849
reValidateMode = 'onChange',
4950
onSubmit,
@@ -81,7 +82,8 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
8182
const form = useForm({
8283
context,
8384
criteriaMode,
84-
defaultValues: defaultValuesIncludingRecord,
85+
values: defaultValuesIncludingRecord,
86+
resetOptions,
8587
delayError,
8688
mode,
8789
reValidateMode,
@@ -93,25 +95,6 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
9395

9496
const formRef = useRef(form);
9597

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-
11598
// notify on invalid form
11699
useNotifyIsFormInvalid(form.control);
117100

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)