Skip to content

Commit 47d4307

Browse files
authored
Merge pull request #8477 from stesie/usereference-meta
Fix useReferenceInputController to pass meta to useReference
2 parents 35ba5ba + d7b4dea commit 47d4307

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
116116
reference,
117117
options: {
118118
enabled: currentValue != null && currentValue !== '',
119+
meta,
119120
},
120121
});
121122
// add current value to possible sources

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ export interface UseReferenceResult<RecordType extends RaRecord = any> {
4646
export const useReference = <RecordType extends RaRecord = any>({
4747
reference,
4848
id,
49-
options,
49+
options = {},
5050
}: UseReferenceProps<RecordType>): UseReferenceResult<RecordType> => {
51+
const { meta, ...otherQueryOptions } = options;
5152
const { data, error, isLoading, isFetching, refetch } = useGetManyAggregate<
5253
RecordType
53-
>(reference, { ids: [id] }, options);
54+
>(reference, { ids: [id], meta }, otherQueryOptions);
5455
return {
5556
referenceRecord: error ? undefined : data ? data[0] : undefined,
5657
refetch,

packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ describe('<ReferenceInput />', () => {
131131
});
132132
});
133133

134+
it('should use meta when fetching current value', async () => {
135+
const getMany = jest
136+
.fn()
137+
.mockImplementationOnce(() => Promise.resolve({ data: [] }));
138+
const dataProvider = testDataProvider({ getMany });
139+
render(
140+
<CoreAdminContext dataProvider={dataProvider}>
141+
<Form record={{ post_id: 23 }}>
142+
<ReferenceInput
143+
{...defaultProps}
144+
queryOptions={{ meta: { foo: 'bar' } }}
145+
/>
146+
</Form>
147+
</CoreAdminContext>
148+
);
149+
await waitFor(() => {
150+
expect(getMany).toHaveBeenCalledWith('posts', {
151+
ids: [23],
152+
meta: { foo: 'bar' },
153+
});
154+
});
155+
});
156+
134157
it('should convert empty values to null with AutocompleteInput', async () => {
135158
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
136159
const dataProvider = {

0 commit comments

Comments
 (0)