Skip to content

Commit

Permalink
Merge pull request #8477 from stesie/usereference-meta
Browse files Browse the repository at this point in the history
Fix useReferenceInputController to pass meta to useReference
  • Loading branch information
slax57 authored Dec 8, 2022
2 parents 35ba5ba + d7b4dea commit 47d4307
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
reference,
options: {
enabled: currentValue != null && currentValue !== '',
meta,
},
});
// add current value to possible sources
Expand Down
5 changes: 3 additions & 2 deletions packages/ra-core/src/controller/useReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ export interface UseReferenceResult<RecordType extends RaRecord = any> {
export const useReference = <RecordType extends RaRecord = any>({
reference,
id,
options,
options = {},
}: UseReferenceProps<RecordType>): UseReferenceResult<RecordType> => {
const { meta, ...otherQueryOptions } = options;
const { data, error, isLoading, isFetching, refetch } = useGetManyAggregate<
RecordType
>(reference, { ids: [id] }, options);
>(reference, { ids: [id], meta }, otherQueryOptions);
return {
referenceRecord: error ? undefined : data ? data[0] : undefined,
refetch,
Expand Down
23 changes: 23 additions & 0 deletions packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ describe('<ReferenceInput />', () => {
});
});

it('should use meta when fetching current value', async () => {
const getMany = jest
.fn()
.mockImplementationOnce(() => Promise.resolve({ data: [] }));
const dataProvider = testDataProvider({ getMany });
render(
<CoreAdminContext dataProvider={dataProvider}>
<Form record={{ post_id: 23 }}>
<ReferenceInput
{...defaultProps}
queryOptions={{ meta: { foo: 'bar' } }}
/>
</Form>
</CoreAdminContext>
);
await waitFor(() => {
expect(getMany).toHaveBeenCalledWith('posts', {
ids: [23],
meta: { foo: 'bar' },
});
});
});

it('should convert empty values to null with AutocompleteInput', async () => {
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
const dataProvider = {
Expand Down

0 comments on commit 47d4307

Please sign in to comment.