Skip to content

Commit 11b7192

Browse files
authored
Merge pull request #10814 from marmelab/fix-use-reference-input-controller-params
Fix useReferenceInputController params type
2 parents 73c818b + b23f597 commit 11b7192

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

packages/ra-core/src/controller/input/useReferenceInputController.spec.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import { useState, useCallback, ReactElement } from 'react';
33
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
44
import expect from 'expect';
55

6-
import { useReferenceInputController } from './useReferenceInputController';
6+
import {
7+
useReferenceInputController,
8+
UseReferenceInputControllerParams,
9+
} from './useReferenceInputController';
710
import { CoreAdminContext } from '../../core';
8-
import { Form, useInput } from '../../form';
11+
import { ChoicesContextValue, Form, useInput } from '../../form';
912
import { testDataProvider } from '../../dataProvider';
13+
import { SortPayload } from '../../types';
1014

11-
const ReferenceInputController = props => {
15+
const ReferenceInputController = (
16+
props: UseReferenceInputControllerParams & {
17+
children: (options: ChoicesContextValue) => React.ReactNode;
18+
}
19+
) => {
1220
const { children, ...rest } = props;
1321
const inputProps = useInput({
1422
...rest,
@@ -160,7 +168,6 @@ describe('useReferenceInputController', () => {
160168
<Form defaultValues={{ post_id: 1 }}>
161169
<ReferenceInputController
162170
{...defaultProps}
163-
loading
164171
sort={{ field: 'title', order: 'ASC' }}
165172
>
166173
{children}
@@ -210,7 +217,10 @@ describe('useReferenceInputController', () => {
210217
it('should refetch reference getList when its props change', async () => {
211218
const children = jest.fn().mockReturnValue(<p>child</p>);
212219
const Component = () => {
213-
const [sort, setSort] = useState({ field: 'title', order: 'ASC' });
220+
const [sort, setSort] = useState<SortPayload>({
221+
field: 'title',
222+
order: 'ASC',
223+
});
214224
const handleClick = useCallback(
215225
() => setSort({ field: 'body', order: 'DESC' }),
216226
[setSort]

packages/ra-core/src/controller/input/useReferenceInputController.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { useCallback, useEffect, useMemo, useState } from 'react';
22
import { useWatch } from 'react-hook-form';
3-
import type { UseQueryOptions } from '@tanstack/react-query';
3+
import { keepPreviousData, type UseQueryOptions } from '@tanstack/react-query';
44

55
import { useGetList } from '../../dataProvider';
66
import { useReference } from '../useReference';
77
import { useReferenceParams } from './useReferenceParams';
88
import { useWrappedSource } from '../../core';
9-
import type { FilterPayload, RaRecord, SortPayload } from '../../types';
9+
import type {
10+
FilterPayload,
11+
GetListResult,
12+
RaRecord,
13+
SortPayload,
14+
} from '../../types';
1015
import type { ChoicesContextValue } from '../../form';
1116

1217
/**
@@ -45,7 +50,7 @@ import type { ChoicesContextValue } from '../../form';
4550
* });
4651
*/
4752
export const useReferenceInputController = <RecordType extends RaRecord = any>(
48-
props: UseReferenceInputControllerParams
53+
props: UseReferenceInputControllerParams<RecordType>
4954
): ChoicesContextValue<RecordType> => {
5055
const {
5156
debounce,
@@ -100,8 +105,10 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
100105
},
101106
{
102107
enabled: isGetMatchingEnabled,
103-
placeholderData: previousData => previousData,
104-
...otherQueryOptions,
108+
placeholderData: keepPreviousData,
109+
...(otherQueryOptions as UseQueryOptions<
110+
GetListResult<RecordType>
111+
>),
105112
}
106113
);
107114

@@ -205,14 +212,11 @@ export interface UseReferenceInputControllerParams<
205212
debounce?: number;
206213
filter?: FilterPayload;
207214
queryOptions?: Omit<
208-
UseQueryOptions<{
209-
data: RecordType[];
210-
total?: number;
211-
pageInfo?: {
212-
hasNextPage?: boolean;
213-
hasPreviousPage?: boolean;
214-
};
215-
}>,
215+
UseQueryOptions<
216+
| GetListResult<RecordType>
217+
// useReference calls getManyAggregate, which returns a an array of records
218+
| RecordType[]
219+
>,
216220
'queryFn' | 'queryKey'
217221
> & { meta?: any };
218222
page?: number;

0 commit comments

Comments
 (0)