From 026a96345c23bbbf4a56d47ba78c75206a7b6e95 Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Wed, 18 May 2022 11:08:58 +0200
Subject: [PATCH 1/2] Fix ReferenceInput Stuck On Loading State
---
.../useReferenceInputController.spec.tsx | 19 +++++++++++++++++++
.../input/useReferenceInputController.ts | 3 +++
.../ra-core/src/controller/useReference.ts | 11 +++++++----
3 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/packages/ra-core/src/controller/input/useReferenceInputController.spec.tsx b/packages/ra-core/src/controller/input/useReferenceInputController.spec.tsx
index 6c029e0e245..c03a388ad27 100644
--- a/packages/ra-core/src/controller/input/useReferenceInputController.spec.tsx
+++ b/packages/ra-core/src/controller/input/useReferenceInputController.spec.tsx
@@ -129,6 +129,25 @@ describe('useReferenceInputController', () => {
});
});
+ it('should not fetch current value using getMany if it is empty', async () => {
+ const children = jest.fn().mockReturnValue(
child
);
+ render(
+
+
+
+ );
+
+ await waitFor(() => {
+ expect(dataProvider.getList).toBeCalledTimes(1);
+ });
+ await new Promise(resolve => setTimeout(resolve, 100));
+ expect(dataProvider.getMany).not.toHaveBeenCalled();
+ });
+
it('should pass possibleValues and record to child', async () => {
const children = jest.fn().mockReturnValue(child
);
render(
diff --git a/packages/ra-core/src/controller/input/useReferenceInputController.ts b/packages/ra-core/src/controller/input/useReferenceInputController.ts
index 4a060ad1d00..27af4b35bb6 100644
--- a/packages/ra-core/src/controller/input/useReferenceInputController.ts
+++ b/packages/ra-core/src/controller/input/useReferenceInputController.ts
@@ -111,6 +111,9 @@ export const useReferenceInputController = (
} = useReference({
id: currentValue,
reference,
+ options: {
+ enabled: currentValue != null && currentValue !== '',
+ },
});
// add current value to possible sources
let finalData: RecordType[], finalTotal: number;
diff --git a/packages/ra-core/src/controller/useReference.ts b/packages/ra-core/src/controller/useReference.ts
index 458d29197be..c69f2b8be8d 100644
--- a/packages/ra-core/src/controller/useReference.ts
+++ b/packages/ra-core/src/controller/useReference.ts
@@ -1,12 +1,14 @@
import { RaRecord, Identifier } from '../types';
import { UseGetManyHookValue, useGetManyAggregate } from '../dataProvider';
+import { UseQueryOptions } from 'react-query';
-interface UseReferenceProps {
+interface UseReferenceProps {
id: Identifier;
reference: string;
+ options?: UseQueryOptions;
}
-export interface UseReferenceResult {
+export interface UseReferenceResult {
isLoading: boolean;
isFetching: boolean;
referenceRecord?: RecordType;
@@ -44,10 +46,11 @@ export interface UseReferenceResult {
export const useReference = ({
reference,
id,
-}: UseReferenceProps): UseReferenceResult => {
+ options,
+}: UseReferenceProps): UseReferenceResult => {
const { data, error, isLoading, isFetching, refetch } = useGetManyAggregate<
RecordType
- >(reference, { ids: [id] });
+ >(reference, { ids: [id] }, options);
return {
referenceRecord: error ? undefined : data ? data[0] : undefined,
refetch,
From 9b885727236e932bf4ecc16e32fd98d04aa97199 Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Wed, 18 May 2022 14:20:14 +0200
Subject: [PATCH 2/2] Fix types
---
packages/ra-core/src/controller/useReference.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/ra-core/src/controller/useReference.ts b/packages/ra-core/src/controller/useReference.ts
index c69f2b8be8d..726711cbd0a 100644
--- a/packages/ra-core/src/controller/useReference.ts
+++ b/packages/ra-core/src/controller/useReference.ts
@@ -2,13 +2,13 @@ import { RaRecord, Identifier } from '../types';
import { UseGetManyHookValue, useGetManyAggregate } from '../dataProvider';
import { UseQueryOptions } from 'react-query';
-interface UseReferenceProps {
+interface UseReferenceProps {
id: Identifier;
reference: string;
options?: UseQueryOptions;
}
-export interface UseReferenceResult {
+export interface UseReferenceResult {
isLoading: boolean;
isFetching: boolean;
referenceRecord?: RecordType;