Skip to content

Commit 4d78a1b

Browse files
authored
Merge pull request #8199 from marmelab/fix-form-parameters
Pass all arguments from react-hook-form to onSubmit
2 parents 8ba9ea8 + 908d254 commit 4d78a1b

12 files changed

+255
-128
lines changed

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

+44-11
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ describe('Form', () => {
210210
fireEvent.click(screen.getByText('Submit'));
211211

212212
await waitFor(() => {
213-
expect(onSubmit).toHaveBeenCalledWith({ foo: null });
213+
expect(onSubmit).toHaveBeenCalledWith(
214+
{ foo: null },
215+
expect.anything()
216+
);
214217
});
215218
});
216219

@@ -235,7 +238,10 @@ describe('Form', () => {
235238
fireEvent.click(screen.getByText('Submit'));
236239

237240
await waitFor(() => {
238-
expect(onSubmit).toHaveBeenCalledWith({ foo: { bar: null } });
241+
expect(onSubmit).toHaveBeenCalledWith(
242+
{ foo: { bar: null } },
243+
expect.anything()
244+
);
239245
});
240246
});
241247

@@ -257,7 +263,10 @@ describe('Form', () => {
257263
fireEvent.click(screen.getByText('Submit'));
258264

259265
await waitFor(() => {
260-
expect(onSubmit).toHaveBeenCalledWith({ foo: str });
266+
expect(onSubmit).toHaveBeenCalledWith(
267+
{ foo: str },
268+
expect.anything()
269+
);
261270
});
262271
});
263272
it('should accept date values', async () => {
@@ -283,7 +292,10 @@ describe('Form', () => {
283292
fireEvent.click(screen.getByText('Submit'));
284293

285294
await waitFor(() => {
286-
expect(onSubmit).toHaveBeenCalledWith({ foo: date });
295+
expect(onSubmit).toHaveBeenCalledWith(
296+
{ foo: date },
297+
expect.anything()
298+
);
287299
});
288300
});
289301

@@ -310,7 +322,10 @@ describe('Form', () => {
310322
fireEvent.click(screen.getByText('Submit'));
311323

312324
await waitFor(() => {
313-
expect(onSubmit).toHaveBeenCalledWith({ foo: arr });
325+
expect(onSubmit).toHaveBeenCalledWith(
326+
{ foo: arr },
327+
expect.anything()
328+
);
314329
});
315330
});
316331

@@ -337,7 +352,10 @@ describe('Form', () => {
337352
fireEvent.click(screen.getByText('Submit'));
338353

339354
await waitFor(() => {
340-
expect(onSubmit).toHaveBeenCalledWith({ foo: obj });
355+
expect(onSubmit).toHaveBeenCalledWith(
356+
{ foo: obj },
357+
expect.anything()
358+
);
341359
});
342360
});
343361
it('should accept deep object values', async () => {
@@ -363,7 +381,10 @@ describe('Form', () => {
363381
fireEvent.click(screen.getByText('Submit'));
364382

365383
await waitFor(() => {
366-
expect(onSubmit).toHaveBeenCalledWith({ foo: obj });
384+
expect(onSubmit).toHaveBeenCalledWith(
385+
{ foo: obj },
386+
expect.anything()
387+
);
367388
});
368389
});
369390
it('should accept object values in arrays', async () => {
@@ -389,7 +410,10 @@ describe('Form', () => {
389410
fireEvent.click(screen.getByText('Submit'));
390411

391412
await waitFor(() => {
392-
expect(onSubmit).toHaveBeenCalledWith({ foo: obj });
413+
expect(onSubmit).toHaveBeenCalledWith(
414+
{ foo: obj },
415+
expect.anything()
416+
);
393417
});
394418
});
395419
it('should accept adding objects in arrays', async () => {
@@ -418,7 +442,10 @@ describe('Form', () => {
418442
fireEvent.click(screen.getByText('Submit'));
419443

420444
await waitFor(() => {
421-
expect(onSubmit).toHaveBeenCalledWith({ foo: obj });
445+
expect(onSubmit).toHaveBeenCalledWith(
446+
{ foo: obj },
447+
expect.anything()
448+
);
422449
});
423450
});
424451
it('should accept removing objects in array of objects', async () => {
@@ -449,7 +476,10 @@ describe('Form', () => {
449476
fireEvent.click(screen.getByText('Submit'));
450477

451478
await waitFor(() => {
452-
expect(onSubmit).toHaveBeenCalledWith({ foo: obj });
479+
expect(onSubmit).toHaveBeenCalledWith(
480+
{ foo: obj },
481+
expect.anything()
482+
);
453483
});
454484
});
455485
describe('defaultValues', () => {
@@ -519,7 +549,10 @@ describe('Form', () => {
519549
fireEvent.click(screen.getByText('Submit'));
520550

521551
await waitFor(() => {
522-
expect(onSubmit).toHaveBeenCalledWith(values);
552+
expect(onSubmit).toHaveBeenCalledWith(
553+
values,
554+
expect.anything()
555+
);
523556
});
524557
});
525558
});

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as React from 'react';
22
import { ReactNode } from 'react';
3-
import { FormProvider, FieldValues, UseFormProps } from 'react-hook-form';
3+
import {
4+
FormProvider,
5+
FieldValues,
6+
UseFormProps,
7+
SubmitHandler,
8+
} from 'react-hook-form';
49

510
import { FormGroupsProvider } from './FormGroupsProvider';
611
import { RaRecord } from '../types';
@@ -77,6 +82,6 @@ export interface FormOwnProps {
7782
id?: string;
7883
record?: Partial<RaRecord>;
7984
resource?: string;
80-
onSubmit?: (data: FieldValues) => any | Promise<any>;
85+
onSubmit?: SubmitHandler<FieldValues>;
8186
warnWhenUnsavedChanges?: boolean;
8287
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { BaseSyntheticEvent, useCallback, useMemo, useEffect } from 'react';
2-
import { FieldValues, useForm, UseFormProps } from 'react-hook-form';
2+
import {
3+
FieldValues,
4+
SubmitHandler,
5+
useForm,
6+
UseFormProps,
7+
} from 'react-hook-form';
38

49
import { RaRecord } from '../types';
510
import { useSaveContext } from '../controller';
@@ -135,14 +140,14 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
135140

136141
// submit callbacks
137142
const handleSubmit = useCallback(
138-
async values => {
143+
async (values, event) => {
139144
let errors;
140145

141146
if (onSubmit) {
142-
errors = await onSubmit(values);
147+
errors = await onSubmit(values, event);
143148
}
144149
if (onSubmit == null && saveContext?.save) {
145-
errors = await saveContext.save(values);
150+
errors = await saveContext.save(values, event);
146151
}
147152
if (errors != null) {
148153
setSubmissionErrors(errors, form.setError);
@@ -179,6 +184,6 @@ export interface UseFormOwnProps {
179184
defaultValues?: any;
180185
formRootPathname?: string;
181186
record?: Partial<RaRecord>;
182-
onSubmit?: (data: FieldValues) => any | Promise<any>;
187+
onSubmit?: SubmitHandler<FieldValues>;
183188
warnWhenUnsavedChanges?: boolean;
184189
}

packages/ra-ui-materialui/src/input/ArrayInput/SimpleFormIterator.spec.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,12 @@ describe('<SimpleFormIterator />', () => {
737737
fireEvent.click(addItemElement);
738738
fireEvent.click(screen.getByText('ra.action.save'));
739739
await waitFor(() => {
740-
expect(save).toHaveBeenCalledWith({
741-
emails: [{ email: '' }],
742-
});
740+
expect(save).toHaveBeenCalledWith(
741+
{
742+
emails: [{ email: '' }],
743+
},
744+
expect.anything()
745+
);
743746
});
744747
});
745748
});

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,12 @@ describe('<CheckboxGroupInput />', () => {
244244
fireEvent.click(getByLabelText('Save'));
245245

246246
await waitFor(() => {
247-
expect(handleSubmit).toHaveBeenCalledWith({
248-
notifications: ['31', '42', '12'],
249-
});
247+
expect(handleSubmit).toHaveBeenCalledWith(
248+
{
249+
notifications: ['31', '42', '12'],
250+
},
251+
expect.anything()
252+
);
250253
});
251254
});
252255

@@ -275,9 +278,12 @@ describe('<CheckboxGroupInput />', () => {
275278
fireEvent.click(getByLabelText('Save'));
276279

277280
await waitFor(() => {
278-
expect(handleSubmit).toHaveBeenCalledWith({
279-
notifications: [31, 42, 12],
280-
});
281+
expect(handleSubmit).toHaveBeenCalledWith(
282+
{
283+
notifications: [31, 42, 12],
284+
},
285+
expect.anything()
286+
);
281287
});
282288
});
283289

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

+30-15
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ describe('<DateInput />', () => {
5050
});
5151
fireEvent.click(screen.getByLabelText('ra.action.save'));
5252
await waitFor(() => {
53-
expect(onSubmit).toHaveBeenCalledWith({
54-
publishedAt: '2021-10-22',
55-
});
53+
expect(onSubmit).toHaveBeenCalledWith(
54+
{
55+
publishedAt: '2021-10-22',
56+
},
57+
expect.anything()
58+
);
5659
});
5760
});
5861

@@ -77,9 +80,12 @@ describe('<DateInput />', () => {
7780
});
7881
fireEvent.click(screen.getByLabelText('ra.action.save'));
7982
await waitFor(() => {
80-
expect(onSubmit).toHaveBeenCalledWith({
81-
publishedAt: '2021-10-22',
82-
});
83+
expect(onSubmit).toHaveBeenCalledWith(
84+
{
85+
publishedAt: '2021-10-22',
86+
},
87+
expect.anything()
88+
);
8389
});
8490
});
8591

@@ -104,9 +110,12 @@ describe('<DateInput />', () => {
104110
});
105111
fireEvent.click(screen.getByLabelText('ra.action.save'));
106112
await waitFor(() => {
107-
expect(onSubmit).toHaveBeenCalledWith({
108-
publishedAt: '2021-10-22',
109-
});
113+
expect(onSubmit).toHaveBeenCalledWith(
114+
{
115+
publishedAt: '2021-10-22',
116+
},
117+
expect.anything()
118+
);
110119
});
111120
});
112121

@@ -131,9 +140,12 @@ describe('<DateInput />', () => {
131140
});
132141
fireEvent.click(screen.getByLabelText('ra.action.save'));
133142
await waitFor(() => {
134-
expect(onSubmit).toHaveBeenCalledWith({
135-
publishedAt: new Date('2021-10-22'),
136-
});
143+
expect(onSubmit).toHaveBeenCalledWith(
144+
{
145+
publishedAt: new Date('2021-10-22'),
146+
},
147+
expect.anything()
148+
);
137149
});
138150
});
139151

@@ -158,9 +170,12 @@ describe('<DateInput />', () => {
158170
});
159171
fireEvent.click(screen.getByLabelText('ra.action.save'));
160172
await waitFor(() => {
161-
expect(onSubmit).toHaveBeenCalledWith({
162-
publishedAt: null,
163-
});
173+
expect(onSubmit).toHaveBeenCalledWith(
174+
{
175+
publishedAt: null,
176+
},
177+
expect.anything()
178+
);
164179
});
165180
});
166181

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ describe('<DateTimeInput />', () => {
105105
).not.toBeNull();
106106
fireEvent.click(screen.getByLabelText('ra.action.save'));
107107
await waitFor(() => {
108-
expect(onSubmit).toHaveBeenCalledWith({
109-
publishedAt,
110-
});
108+
expect(onSubmit).toHaveBeenCalledWith(
109+
{
110+
publishedAt,
111+
},
112+
expect.anything()
113+
);
111114
});
112115
});
113116

@@ -138,9 +141,12 @@ describe('<DateTimeInput />', () => {
138141
).not.toBeNull();
139142
fireEvent.click(screen.getByLabelText('ra.action.save'));
140143
await waitFor(() => {
141-
expect(onSubmit).toHaveBeenCalledWith({
142-
publishedAt,
143-
});
144+
expect(onSubmit).toHaveBeenCalledWith(
145+
{
146+
publishedAt,
147+
},
148+
expect.anything()
149+
);
144150
});
145151
});
146152

0 commit comments

Comments
 (0)