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

Add queryOptions support for ReferenceArrayField #9275

Merged
merged 1 commit into from
Sep 14, 2023
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 @@ -4,9 +4,11 @@ import { RaRecord, SortPayload } from '../../types';
import { useGetManyAggregate } from '../../dataProvider';
import { ListControllerResult, useList } from '../list';
import { useNotify } from '../../notification';
import { UseQueryOptions } from 'react-query';

export interface UseReferenceArrayFieldControllerParams<
RecordType extends RaRecord = RaRecord
RecordType extends RaRecord = RaRecord,
ReferenceRecordType extends RaRecord = RaRecord
> {
filter?: any;
page?: number;
Expand All @@ -16,6 +18,7 @@ export interface UseReferenceArrayFieldControllerParams<
resource: string;
sort?: SortPayload;
source: string;
queryOptions?: UseQueryOptions<ReferenceRecordType[], Error>;
}

const emptyArray = [];
Expand Down Expand Up @@ -49,7 +52,10 @@ export const useReferenceArrayFieldController = <
RecordType extends RaRecord = RaRecord,
ReferenceRecordType extends RaRecord = RaRecord
>(
props: UseReferenceArrayFieldControllerParams<RecordType>
props: UseReferenceArrayFieldControllerParams<
RecordType,
ReferenceRecordType
>
): ListControllerResult => {
const {
filter = defaultFilter,
Expand All @@ -59,9 +65,11 @@ export const useReferenceArrayFieldController = <
reference,
sort = defaultSort,
source,
queryOptions = {},
} = props;
const notify = useNotify();
const value = get(record, source);
const { meta, ...otherQueryOptions } = queryOptions;

const ids = useMemo(() => {
if (Array.isArray(value)) return value;
Expand All @@ -73,7 +81,7 @@ export const useReferenceArrayFieldController = <
ReferenceRecordType
>(
reference,
{ ids },
{ ids, meta },
{
onError: error =>
notify(
Expand All @@ -92,6 +100,7 @@ export const useReferenceArrayFieldController = <
},
}
),
...otherQueryOptions,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ const fakeData = {

export default { title: 'ra-ui-materialui/fields/ReferenceArrayField' };

const dataProvider = fakeRestProvider(fakeData, false);

export const DifferentIdTypes = () => {
return (
<AdminContext dataProvider={fakeRestProvider(fakeData, false)}>
<AdminContext dataProvider={dataProvider}>
<CardContent>
<Show resource="bands" id={1} sx={{ width: 600 }}>
<TextField source="name" fullWidth />
Expand All @@ -41,3 +43,35 @@ export const DifferentIdTypes = () => {
</AdminContext>
);
};

const dataProviderWithLog = {
...dataProvider,
getMany: (resource, params) => {
console.log('getMany', resource, params);
return dataProvider.getMany(resource, params);
},
} as any;

export const WithMeta = () => {
return (
<AdminContext dataProvider={dataProviderWithLog}>
<CardContent>
<Show resource="bands" id={1} sx={{ width: 600 }}>
<TextField source="name" />
<ReferenceArrayField
source="members"
reference="artists"
queryOptions={{
meta: { foo: 'bar' },
}}
>
<Datagrid bulkActionButtons={false}>
<TextField source="id" />
<TextField source="name" />
</Datagrid>
</ReferenceArrayField>
</Show>
</CardContent>
</AdminContext>
);
};
12 changes: 9 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { fieldPropTypes, FieldProps } from './types';
import { LinearProgress } from '../layout';
import { SingleFieldList } from '../list/SingleFieldList';
import { ChipField } from './ChipField';
import { UseQueryOptions } from 'react-query';

/**
* A container component that fetches records from another resource specified
Expand Down Expand Up @@ -81,7 +82,7 @@ export const ReferenceArrayField = <
RecordType extends RaRecord = RaRecord,
ReferenceRecordType extends RaRecord = RaRecord
>(
props: ReferenceArrayFieldProps<RecordType>
props: ReferenceArrayFieldProps<RecordType, ReferenceRecordType>
) => {
const {
filter,
Expand All @@ -91,6 +92,7 @@ export const ReferenceArrayField = <
resource,
sort,
source,
queryOptions,
} = props;
const record = useRecordContext(props);
const controllerProps = useReferenceArrayFieldController<
Expand All @@ -105,6 +107,7 @@ export const ReferenceArrayField = <
resource,
sort,
source,
queryOptions,
});
return (
<ResourceContextProvider value={reference}>
Expand All @@ -126,10 +129,12 @@ ReferenceArrayField.propTypes = {
sortBy: PropTypes.string,
sortByOrder: fieldPropTypes.sortByOrder,
source: PropTypes.string.isRequired,
queryOptions: PropTypes.any,
};

export interface ReferenceArrayFieldProps<
RecordType extends RaRecord = RaRecord
RecordType extends RaRecord = RaRecord,
ReferenceRecordType extends RaRecord = RaRecord
> extends FieldProps<RecordType> {
children?: ReactNode;
filter?: FilterPayload;
Expand All @@ -139,11 +144,12 @@ export interface ReferenceArrayFieldProps<
reference: string;
sort?: SortPayload;
sx?: SxProps;
queryOptions?: UseQueryOptions<ReferenceRecordType[], Error>;
}

export interface ReferenceArrayFieldViewProps
extends Omit<ReferenceArrayFieldProps, 'resource' | 'page' | 'perPage'>,
ListControllerProps {}
Omit<ListControllerProps, 'queryOptions'> {}

export const ReferenceArrayFieldView: FC<ReferenceArrayFieldViewProps> = props => {
const { children, pagination, reference, className, sx } = props;
Expand Down
Loading