Skip to content

Commit 9fac1bf

Browse files
authored
Merge pull request #11166 from marmelab/referencemanyfieldcontroller-type
[TypeScript] Fix ReferenceManyField queryOptions type doesn't support error and success callbacks
2 parents e7397a4 + b0ba0b0 commit 9fac1bf

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

packages/ra-core/src/controller/field/ReferenceManyFieldBase.stories.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,46 @@ const AuthorList = ({ source }) => {
414414
</p>
415415
);
416416
};
417+
418+
// test queryOptions type, it should support onSuccess and onError
419+
export const QueryOptions = ({ dataProvider }) => (
420+
<TestMemoryRouter initialEntries={['/authors/1/show']}>
421+
<CoreAdmin dataProvider={dataProvider}>
422+
<Resource
423+
name="authors"
424+
show={
425+
<ShowBase>
426+
<ReferenceManyFieldBase
427+
target="author"
428+
source="id"
429+
reference="books"
430+
queryOptions={{
431+
onSuccess: () =>
432+
console.log('Query successful'),
433+
onError: () => console.log('Query failed'),
434+
retry: false,
435+
}}
436+
>
437+
<AuthorList source="title" />
438+
</ReferenceManyFieldBase>
439+
</ShowBase>
440+
}
441+
/>
442+
</CoreAdmin>
443+
</TestMemoryRouter>
444+
);
445+
446+
QueryOptions.args = {
447+
dataProvider: dataProviderWithAuthors,
448+
};
449+
450+
QueryOptions.argTypes = {
451+
dataProvider: {
452+
control: { type: 'radio' },
453+
options: ['No error', 'With Error'],
454+
mapping: {
455+
'No error': dataProviderWithAuthors,
456+
'With Error': dataProviderWithAuthorsError,
457+
},
458+
},
459+
};

packages/ra-core/src/controller/field/useReferenceManyFieldController.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import isEqual from 'lodash/isEqual.js';
55
import lodashDebounce from 'lodash/debounce.js';
66

77
import { removeEmpty, useEvent } from '../../util';
8-
import { useDataProvider, useGetManyReference } from '../../dataProvider';
8+
import {
9+
useDataProvider,
10+
useGetManyReference,
11+
UseGetManyReferenceHookOptions,
12+
} from '../../dataProvider';
913
import { useNotify } from '../../notification';
1014
import {
1115
Exporter,
@@ -359,7 +363,7 @@ export const useReferenceManyFieldController = <
359363

360364
export interface UseReferenceManyFieldControllerParams<
361365
RecordType extends Record<string, any> = Record<string, any>,
362-
ReferenceRecordType extends Record<string, any> = Record<string, any>,
366+
ReferenceRecordType extends RaRecord = any,
363367
ErrorType = Error,
364368
> {
365369
debounce?: number;
@@ -375,10 +379,7 @@ export interface UseReferenceManyFieldControllerParams<
375379
storeKey?: string;
376380
target: string;
377381
queryOptions?: Omit<
378-
UseQueryOptions<
379-
{ data: ReferenceRecordType[]; total: number },
380-
ErrorType
381-
>,
382+
UseGetManyReferenceHookOptions<ReferenceRecordType, ErrorType>,
382383
'queryKey' | 'queryFn'
383384
>;
384385
}

0 commit comments

Comments
 (0)