Skip to content

Commit 40e3208

Browse files
authored
Merge pull request #7707 from marmelab/fix-reference-input
Fix ReferenceInput Stuck On Loading State
2 parents 78847bc + 9b88572 commit 40e3208

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ describe('useReferenceInputController', () => {
129129
});
130130
});
131131

132+
it('should not fetch current value using getMany if it is empty', async () => {
133+
const children = jest.fn().mockReturnValue(<p>child</p>);
134+
render(
135+
<CoreAdminContext dataProvider={dataProvider}>
136+
<Form defaultValues={{ post_id: '' }}>
137+
<ReferenceInputController {...defaultProps}>
138+
{children}
139+
</ReferenceInputController>
140+
</Form>
141+
</CoreAdminContext>
142+
);
143+
144+
await waitFor(() => {
145+
expect(dataProvider.getList).toBeCalledTimes(1);
146+
});
147+
await new Promise(resolve => setTimeout(resolve, 100));
148+
expect(dataProvider.getMany).not.toHaveBeenCalled();
149+
});
150+
132151
it('should pass possibleValues and record to child', async () => {
133152
const children = jest.fn().mockReturnValue(<p>child</p>);
134153
render(

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

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
111111
} = useReference<RecordType>({
112112
id: currentValue,
113113
reference,
114+
options: {
115+
enabled: currentValue != null && currentValue !== '',
116+
},
114117
});
115118
// add current value to possible sources
116119
let finalData: RecordType[], finalTotal: number;

packages/ra-core/src/controller/useReference.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { RaRecord, Identifier } from '../types';
22
import { UseGetManyHookValue, useGetManyAggregate } from '../dataProvider';
3+
import { UseQueryOptions } from 'react-query';
34

4-
interface UseReferenceProps {
5+
interface UseReferenceProps<RecordType extends RaRecord = any> {
56
id: Identifier;
67
reference: string;
8+
options?: UseQueryOptions<RecordType[], Error>;
79
}
810

911
export interface UseReferenceResult<RecordType extends RaRecord = any> {
@@ -44,10 +46,11 @@ export interface UseReferenceResult<RecordType extends RaRecord = any> {
4446
export const useReference = <RecordType extends RaRecord = any>({
4547
reference,
4648
id,
47-
}: UseReferenceProps): UseReferenceResult<RecordType> => {
49+
options,
50+
}: UseReferenceProps<RecordType>): UseReferenceResult<RecordType> => {
4851
const { data, error, isLoading, isFetching, refetch } = useGetManyAggregate<
4952
RecordType
50-
>(reference, { ids: [id] });
53+
>(reference, { ids: [id] }, options);
5154
return {
5255
referenceRecord: error ? undefined : data ? data[0] : undefined,
5356
refetch,

0 commit comments

Comments
 (0)