Skip to content

Commit aa94fa8

Browse files
authored
Merge pull request #8895 from WiXSL/rf-queryoptions
Add `queryOptions` prop to `ReferenceField`
2 parents 906af62 + 6f591c8 commit aa94fa8

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

docs/ReferenceField.md

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ With this configuration, `<ReferenceField>` wraps the user's name in a link to t
7979
| `emptyText` | Optional | `string` | '' | Defines a text to be shown when the field has no value or when the reference is missing |
8080
| `label` | Optional | `string | Function` | `resources.[resource].fields.[source]` | Label to use for the field when rendered in layout components |
8181
| `link` | Optional | `string | Function` | `edit` | Target of the link wrapping the rendered child. Set to `false` to disable the link. |
82+
| `queryOptions` | Optional | [`UseQueryOptions`](https://tanstack.com/query/v4/docs/reference/useQuery?from=reactQueryV3&original=https://react-query-v3.tanstack.com/reference/useQuery) | `{}` | `react-query` client options |
8283
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |
8384

8485
`<ReferenceField>` also accepts the [common field props](./Fields.md#common-field-props).
@@ -143,6 +144,22 @@ You can also use a custom `link` function to get a custom path for the children.
143144
/>
144145
```
145146

147+
## `queryOptions`
148+
149+
Use the `queryOptions` prop to pass options to the `dataProvider.getMany()` query that fetches the referenced record.
150+
151+
For instance, to pass [a custom `meta`](./Actions.md#meta-parameter):
152+
153+
```jsx
154+
<ReferenceField
155+
source="user_id"
156+
reference="users"
157+
queryOptions={{ meta: { foo: 'bar' } }}
158+
>
159+
<TextField source="name" />
160+
</ReferenceField>
161+
```
162+
146163
## `reference`
147164

148165
The resource to fetch for the related record.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { UseQueryOptions } from 'react-query';
55
interface UseReferenceProps<RecordType extends RaRecord = any> {
66
id: Identifier;
77
reference: string;
8-
options?: UseQueryOptions<RecordType[], Error>;
8+
options?: UseQueryOptions<RecordType[], Error> & { meta?: any };
99
}
1010

1111
export interface UseReferenceResult<RecordType extends RaRecord = any> {
@@ -40,6 +40,7 @@ export interface UseReferenceResult<RecordType extends RaRecord = any> {
4040
* @param {Object} option
4141
* @param {string} option.reference The linked resource name
4242
* @param {string} option.id The id of the reference
43+
* @param {Object} option.options Options passed to the dataProvider
4344
*
4445
* @returns {UseReferenceResult} The reference record
4546
*/

packages/ra-ui-materialui/src/field/ReferenceField.spec.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,33 @@ describe('<ReferenceField />', () => {
583583

584584
expect(screen.findByText('Not found')).not.toBeNull();
585585
});
586+
587+
it('should accept a queryOptions prop', async () => {
588+
const dataProvider = testDataProvider({
589+
getMany: jest.fn().mockResolvedValue({
590+
data: [{ id: 123, title: 'foo' }],
591+
}),
592+
});
593+
render(
594+
<ThemeProvider theme={theme}>
595+
<CoreAdminContext dataProvider={dataProvider}>
596+
<ReferenceField
597+
record={record}
598+
resource="comments"
599+
source="postId"
600+
reference="posts"
601+
queryOptions={{ meta: { foo: 'bar' } }}
602+
>
603+
<TextField source="title" />
604+
</ReferenceField>
605+
</CoreAdminContext>
606+
</ThemeProvider>
607+
);
608+
await waitFor(() => {
609+
expect(dataProvider.getMany).toHaveBeenCalledWith('posts', {
610+
ids: [123],
611+
meta: { foo: 'bar' },
612+
});
613+
});
614+
});
586615
});

packages/ra-ui-materialui/src/field/ReferenceField.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { LinearProgress } from '../layout';
2424
import { Link } from '../Link';
2525
import { PublicFieldProps, fieldPropTypes, InjectedFieldProps } from './types';
26+
import { UseQueryOptions } from 'react-query';
2627

2728
/**
2829
* Fetch reference record, and render its representation, or delegate rendering to child component.
@@ -106,6 +107,7 @@ export interface ReferenceFieldProps<RecordType extends RaRecord = any>
106107
extends PublicFieldProps,
107108
InjectedFieldProps<RecordType> {
108109
children?: ReactNode;
110+
queryOptions?: UseQueryOptions<RecordType[], Error> & { meta?: any };
109111
reference: string;
110112
resource?: string;
111113
source: string;
@@ -120,7 +122,7 @@ export interface ReferenceFieldProps<RecordType extends RaRecord = any>
120122
*/
121123
export const NonEmptyReferenceField: FC<
122124
Omit<ReferenceFieldProps, 'source'> & { id: Identifier }
123-
> = ({ children, id, record, reference, link, ...props }) => {
125+
> = ({ children, id, record, reference, link, queryOptions, ...props }) => {
124126
const createPath = useCreatePath();
125127
const resourceDefinition = useResourceDefinition({ resource: reference });
126128

@@ -146,6 +148,7 @@ export const NonEmptyReferenceField: FC<
146148
{...useReference({
147149
reference,
148150
id,
151+
options: queryOptions,
149152
})}
150153
resourceLinkPath={resourceLinkPath}
151154
>

0 commit comments

Comments
 (0)