Skip to content

Commit 59dbed5

Browse files
committed
Fix <ReferenceInput> don't return currently selected choice when enableGetChoices returns false
1 parent 11e2c04 commit 59dbed5

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ describe('useReferenceInputController', () => {
316316

317317
it('should fetch current value using getMany even if enableGetChoices is returning false', async () => {
318318
const children = jest.fn().mockReturnValue(<p>child</p>);
319+
319320
render(
320321
<CoreAdminContext dataProvider={dataProvider}>
321322
<Form defaultValues={{ post_id: 1 }}>
@@ -333,6 +334,19 @@ describe('useReferenceInputController', () => {
333334
expect(dataProvider.getList).toBeCalledTimes(0);
334335
expect(dataProvider.getMany).toBeCalledTimes(1);
335336
});
337+
await waitFor(() => {
338+
expect(children).toHaveBeenCalledWith(
339+
expect.objectContaining({
340+
sort: { field: 'id', order: 'ASC' },
341+
allChoices: [{ id: 1, title: 'foo' }],
342+
availableChoices: [],
343+
selectedChoices: [{ id: 1, title: 'foo' }],
344+
isPending: true,
345+
total: 1,
346+
isFromReference: true,
347+
})
348+
);
349+
});
336350
});
337351
});
338352
});

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,31 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
154154

155155
// add current value to possible sources
156156
const { finalData, finalTotal } = useMemo(() => {
157-
if (isPaused && possibleValuesData == null) {
157+
if (isPaused && possibleValuesData == null && referenceRecord == null) {
158158
return {
159159
finalData: null,
160160
finalTotal: null,
161161
};
162162
}
163163
if (
164-
!referenceRecord ||
164+
referenceRecord == null ||
165165
possibleValuesData == null ||
166166
(possibleValuesData ?? []).find(
167167
record => record.id === referenceRecord.id
168168
)
169169
) {
170+
// Here we might have the referenceRecord but no data (because of enableGetChoices for instance)
171+
const finalData = possibleValuesData ?? [];
172+
if (
173+
referenceRecord &&
174+
finalData.find(r => r.id === referenceRecord.id) == null
175+
) {
176+
finalData.push(referenceRecord);
177+
}
178+
170179
return {
171-
finalData: possibleValuesData,
172-
finalTotal: total,
180+
finalData,
181+
finalTotal: total ?? finalData.length,
173182
};
174183
} else {
175184
return {

packages/ra-ui-materialui/src/input/AutocompleteInput.stories.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,38 @@ export const InsideReferenceInput = () => (
846846
</TestMemoryRouter>
847847
);
848848

849+
const enableGetChoices = filters => filters?.q?.length > 3;
850+
export const InsideReferenceInputWithDisableChoice = () => (
851+
<TestMemoryRouter initialEntries={['/books/1']}>
852+
<Admin dataProvider={dataProviderWithAuthors}>
853+
<Resource name="authors" />
854+
<Resource
855+
name="books"
856+
edit={() => (
857+
<Edit
858+
mutationMode="pessimistic"
859+
mutationOptions={{
860+
onSuccess: data => {
861+
console.log(data);
862+
},
863+
}}
864+
>
865+
<SimpleForm>
866+
<ReferenceInput
867+
reference="authors"
868+
source="author"
869+
enableGetChoices={enableGetChoices}
870+
>
871+
<AutocompleteInput optionText="name" />
872+
</ReferenceInput>
873+
</SimpleForm>
874+
</Edit>
875+
)}
876+
/>
877+
</Admin>
878+
</TestMemoryRouter>
879+
);
880+
849881
const LanguageChangingAuthorInput = ({ onChange }) => {
850882
const { setValue } = useFormContext();
851883
const handleChange = (value, record) => {

0 commit comments

Comments
 (0)