Skip to content

Commit 701ac7c

Browse files
authored
Merge pull request #11067 from marmelab/fix-form-value-override-from-location
Fix values from location are not applied on Forms
2 parents 30580c1 + 95537ad commit 701ac7c

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

packages/ra-core/src/form/Form.stories.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,24 @@ export const MultiRoutesForm = ({
426426
initialRecord?: Partial<RaRecord>;
427427
defaultValues?: Partial<RaRecord>;
428428
}) => (
429-
<TestMemoryRouter key={url} initialEntries={[url]}>
430-
<CoreAdminContext i18nProvider={defaultI18nProvider}>
431-
<Routes>
432-
<Route
433-
path="/form/*"
434-
element={
435-
<RecordContextProvider value={initialRecord}>
436-
<FormWithSubRoutes defaultValues={defaultValues} />
437-
</RecordContextProvider>
438-
}
439-
/>
440-
</Routes>
441-
</CoreAdminContext>
442-
</TestMemoryRouter>
429+
<React.StrictMode>
430+
<TestMemoryRouter key={url} initialEntries={[url]}>
431+
<CoreAdminContext i18nProvider={defaultI18nProvider}>
432+
<Routes>
433+
<Route
434+
path="/form/*"
435+
element={
436+
<RecordContextProvider value={initialRecord}>
437+
<FormWithSubRoutes
438+
defaultValues={defaultValues}
439+
/>
440+
</RecordContextProvider>
441+
}
442+
/>
443+
</Routes>
444+
</CoreAdminContext>
445+
</TestMemoryRouter>
446+
</React.StrictMode>
443447
);
444448

445449
MultiRoutesForm.args = {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,28 @@ export const useAugmentedForm = <RecordType = any>(
8383
});
8484

8585
const formRef = useRef(form);
86-
const { reset } = form;
86+
const { reset, formState } = form;
87+
const { isReady } = formState;
8788

8889
useEffect(() => {
90+
if (!isReady) return;
8991
reset(defaultValuesIncludingRecord);
90-
}, [defaultValuesIncludingRecord, reset]);
92+
}, [defaultValuesIncludingRecord, reset, isReady]);
9193

9294
// notify on invalid form
9395
useNotifyIsFormInvalid(form.control, !disableInvalidFormNotification);
9496

9597
const recordFromLocation = useRecordFromLocation();
9698
const recordFromLocationApplied = useRef(false);
9799
useEffect(() => {
100+
if (!isReady) return;
98101
if (recordFromLocation && !recordFromLocationApplied.current) {
99102
reset(merge({}, defaultValuesIncludingRecord, recordFromLocation), {
100103
keepDefaultValues: true,
101104
});
102105
recordFromLocationApplied.current = true;
103106
}
104-
}, [defaultValuesIncludingRecord, recordFromLocation, reset]);
107+
}, [defaultValuesIncludingRecord, recordFromLocation, reset, isReady]);
105108

106109
// submit callbacks
107110
const handleSubmit = useCallback(

packages/ra-ui-materialui/src/button/SaveButton.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('<SaveButton />', () => {
7272

7373
it('should render an enabled button when the form is dirty', async () => {
7474
render(<Dirty />);
75+
fireEvent.click(await screen.findByText('Make change'));
7576
await waitFor(() =>
7677
expect(screen.getByLabelText('ra.action.save')['disabled']).toEqual(
7778
false

packages/ra-ui-materialui/src/button/SaveButton.stories.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { TextInput } from '../input/TextInput';
1010
import { SimpleFormIterator } from '../input/ArrayInput/SimpleFormIterator';
1111
import { AdminContext } from '../AdminContext';
1212
import { Edit } from '../detail';
13+
import { Button } from './Button';
1314

1415
export default {
1516
title: 'ra-ui-materialui/button/SaveButton',
@@ -26,15 +27,11 @@ export const Basic = () => (
2627
);
2728

2829
const MakeFormChange = () => {
29-
const {
30-
setValue,
31-
formState: { isReady },
32-
} = useFormContext();
33-
React.useEffect(() => {
34-
if (!isReady) return;
30+
const { setValue } = useFormContext();
31+
const handleClick = () => {
3532
setValue('name', 'test', { shouldDirty: true });
36-
}, [isReady, setValue]);
37-
return null;
33+
};
34+
return <Button label="Make change" onClick={handleClick} />;
3835
};
3936

4037
export const Dirty = () => (

0 commit comments

Comments
 (0)