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 MUI Autocomplete warning on deselection with ReferenceInput #8788

Merged
merged 2 commits into from
Apr 3, 2023
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 @@ -165,7 +165,7 @@ describe('useReferenceInputController', () => {
);

await waitFor(() => {
expect(children).toBeCalledTimes(4);
expect(children.mock.calls.length).toBeGreaterThanOrEqual(4);
});
expect(children).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useWatch } from 'react-hook-form';
import { useGetList } from '../../dataProvider';
import { FilterPayload, RaRecord, SortPayload } from '../../types';
Expand Down Expand Up @@ -106,7 +106,7 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(

// fetch current value
const {
referenceRecord,
referenceRecord: currentReferenceRecord,
refetch: refetchReference,
error: referenceError,
isLoading: referenceLoading,
Expand All @@ -119,6 +119,15 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
meta,
},
});

// We need to delay the update of the referenceRecord and the finalData
// to the next React state update, because otherwise it can raise a warning
// with AutocompleteInput saying the current value is not in the list of choices
const [referenceRecord, setReferenceRecord] = useState(null);
useEffect(() => {
setReferenceRecord(currentReferenceRecord);
}, [currentReferenceRecord]);

// add current value to possible sources
let finalData: RecordType[], finalTotal: number;
if (
Expand Down