Skip to content

Commit 1f83114

Browse files
authored
Merge pull request #9197 from marmelab/Fix-referenceinput-queryoptions
Fix `<ReferenceInput queryOptions>` does not apply to getMany query
2 parents 9c63204 + e853847 commit 1f83114

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
114114
} = useReference<RecordType>({
115115
id: currentValue,
116116
reference,
117+
// @ts-ignore the types of the queryOptions for the getMAny and getList are not compatible
117118
options: {
118119
enabled: currentValue != null && currentValue !== '',
119120
meta,
121+
...otherQueryOptions,
120122
},
121123
});
122124

packages/ra-ui-materialui/src/detail/EditView.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ interface EditViewProps
7373
EditView.propTypes = {
7474
actions: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]),
7575
aside: PropTypes.element,
76-
children: PropTypes.element,
7776
className: PropTypes.string,
7877
component: ComponentPropType,
7978
defaultTitle: PropTypes.any,

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

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
WithSelectInput,
1818
dataProviderWithAuthors,
1919
SelfReference,
20+
QueryOptions,
2021
} from './ReferenceInput.stories';
2122

2223
describe('<ReferenceInput />', () => {
@@ -26,6 +27,10 @@ describe('<ReferenceInput />', () => {
2627
source: 'post_id',
2728
};
2829

30+
beforeAll(() => {
31+
window.scrollTo = jest.fn();
32+
});
33+
2934
it('should display an error if error is defined', async () => {
3035
jest.spyOn(console, 'error')
3136
.mockImplementationOnce(() => {})
@@ -155,6 +160,16 @@ describe('<ReferenceInput />', () => {
155160
});
156161
});
157162

163+
it('should pass queryOptions to both queries', async () => {
164+
render(<QueryOptions />);
165+
await waitFor(() => new Promise(resolve => setTimeout(resolve, 100)));
166+
expect(screen.queryByDisplayValue('Leo Tolstoy')).toBeNull();
167+
fireEvent.click(screen.getByText('Toggle queryOptions'));
168+
await waitFor(() => {
169+
expect(screen.queryByDisplayValue('Leo Tolstoy')).not.toBeNull();
170+
});
171+
});
172+
158173
it('should convert empty values to null with AutocompleteInput', async () => {
159174
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
160175
const dataProvider = {

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { QueryClient } from 'react-query';
55
import { Resource, Form, testDataProvider, useRedirect } from 'ra-core';
66
import polyglotI18nProvider from 'ra-i18n-polyglot';
77
import englishMessages from 'ra-language-english';
8-
import { Stack, Divider, Typography } from '@mui/material';
8+
import fakeRestDataProvider from 'ra-data-fakerest';
9+
import { Stack, Divider, Typography, Button } from '@mui/material';
10+
import { MemoryRouter } from 'react-router-dom';
911

1012
import { Edit } from '../detail';
1113
import { SimpleForm } from '../form';
@@ -507,3 +509,85 @@ export const SelfReference = ({ dataProvider = dataProviderWithAuthors }) => (
507509
<Resource name="books" edit={BookEditWithSelfReference} />
508510
</Admin>
509511
);
512+
513+
const BookEditQueryOptions = () => {
514+
const [enabled, setEnabled] = React.useState(false);
515+
return (
516+
<Edit mutationMode="pessimistic">
517+
<Button onClick={() => setEnabled(!enabled)}>
518+
Toggle queryOptions
519+
</Button>
520+
<SimpleForm>
521+
<TextInput source="title" />
522+
<ReferenceInput
523+
reference="authors"
524+
source="author"
525+
queryOptions={{ enabled }}
526+
/>
527+
</SimpleForm>
528+
</Edit>
529+
);
530+
};
531+
532+
export const QueryOptions = () => (
533+
<MemoryRouter initialEntries={['/books/1']}>
534+
<Admin
535+
dataProvider={fakeRestDataProvider(
536+
{
537+
books: [
538+
{
539+
id: 1,
540+
title: 'War and Peace',
541+
author: 1,
542+
summary:
543+
"War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.",
544+
year: 1869,
545+
},
546+
],
547+
authors: [
548+
{
549+
id: 1,
550+
first_name: 'Leo',
551+
last_name: 'Tolstoy',
552+
language: 'Russian',
553+
},
554+
{
555+
id: 2,
556+
first_name: 'Victor',
557+
last_name: 'Hugo',
558+
language: 'French',
559+
},
560+
{
561+
id: 3,
562+
first_name: 'William',
563+
last_name: 'Shakespeare',
564+
language: 'English',
565+
},
566+
{
567+
id: 4,
568+
first_name: 'Charles',
569+
last_name: 'Baudelaire',
570+
language: 'French',
571+
},
572+
{
573+
id: 5,
574+
first_name: 'Marcel',
575+
last_name: 'Proust',
576+
language: 'French',
577+
},
578+
],
579+
},
580+
process.env.NODE_ENV === 'development'
581+
)}
582+
>
583+
<Resource
584+
name="authors"
585+
recordRepresentation={record =>
586+
`${record.first_name} ${record.last_name}`
587+
}
588+
list={AuthorList}
589+
/>
590+
<Resource name="books" edit={BookEditQueryOptions} />
591+
</Admin>
592+
</MemoryRouter>
593+
);

0 commit comments

Comments
 (0)