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 useReferenceInputController to pass meta to useReference #8477

Merged
merged 1 commit into from
Dec 8, 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 @@ -116,6 +116,7 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
reference,
options: {
enabled: currentValue != null && currentValue !== '',
meta,
slax57 marked this conversation as resolved.
Show resolved Hide resolved
},
});
// 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