Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReferenceInput Stuck On Loading State #7707

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ describe('useReferenceInputController', () => {
});
});

it('should not fetch current value using getMany if it is empty', async () => {
const children = jest.fn().mockReturnValue(<p>child</p>);
render(
<CoreAdminContext dataProvider={dataProvider}>
<Form defaultValues={{ post_id: '' }}>
<ReferenceInputController {...defaultProps}>
{children}
</ReferenceInputController>
</Form>
</CoreAdminContext>
);

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(<p>child</p>);
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
} = useReference<RecordType>({
id: currentValue,
reference,
options: {
enabled: currentValue != null && currentValue !== '',
},
});
// add current value to possible sources
let finalData: RecordType[], finalTotal: number;
Expand Down
9 changes: 6 additions & 3 deletions packages/ra-core/src/controller/useReference.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { RaRecord, Identifier } from '../types';
import { UseGetManyHookValue, useGetManyAggregate } from '../dataProvider';
import { UseQueryOptions } from 'react-query';

interface UseReferenceProps {
interface UseReferenceProps<RecordType extends RaRecord = any> {
id: Identifier;
reference: string;
options?: UseQueryOptions<RecordType[], Error>;
}

export interface UseReferenceResult<RecordType extends RaRecord = any> {
Expand Down Expand Up @@ -44,10 +46,11 @@ export interface UseReferenceResult<RecordType extends RaRecord = any> {
export const useReference = <RecordType extends RaRecord = any>({
reference,
id,
}: UseReferenceProps): UseReferenceResult<RecordType> => {
options,
}: UseReferenceProps<RecordType>): UseReferenceResult<RecordType> => {
const { data, error, isLoading, isFetching, refetch } = useGetManyAggregate<
RecordType
>(reference, { ids: [id] });
>(reference, { ids: [id] }, options);
return {
referenceRecord: error ? undefined : data ? data[0] : undefined,
refetch,
Expand Down