|
| 1 | +import React, { ReactElement, ReactNode } from 'react'; |
| 2 | +import { ResourceContextProvider } from '../../core'; |
| 3 | +import { ListContextProvider } from '../list/ListContextProvider'; |
| 4 | +import { |
| 5 | + useReferenceManyFieldController, |
| 6 | + type UseReferenceManyFieldControllerParams, |
| 7 | +} from './useReferenceManyFieldController'; |
| 8 | +import type { RaRecord } from '../../types'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Render related records to the current one. |
| 12 | + * |
| 13 | + * You must define the fields to be passed to the iterator component as children. |
| 14 | + * |
| 15 | + * @example Display all the comments of the current post as a datagrid |
| 16 | + * <ReferenceManyFieldBase reference="comments" target="post_id"> |
| 17 | + * <Datagrid> |
| 18 | + * <TextField source="id" /> |
| 19 | + * <TextField source="body" /> |
| 20 | + * <DateField source="created_at" /> |
| 21 | + * <EditButton /> |
| 22 | + * </Datagrid> |
| 23 | + * </ReferenceManyFieldBase> |
| 24 | + * |
| 25 | + * @example Display all the books by the current author, only the title |
| 26 | + * <ReferenceManyFieldBase reference="books" target="author_id"> |
| 27 | + * <SingleFieldList> |
| 28 | + * <ChipField source="title" /> |
| 29 | + * </SingleFieldList> |
| 30 | + * </ReferenceManyFieldBase> |
| 31 | + * |
| 32 | + * By default, restricts the displayed values to 25. You can extend this limit |
| 33 | + * by setting the `perPage` prop. |
| 34 | + * |
| 35 | + * @example |
| 36 | + * <ReferenceManyFieldBase perPage={10} reference="comments" target="post_id"> |
| 37 | + * ... |
| 38 | + * </ReferenceManyFieldBase> |
| 39 | + * |
| 40 | + * By default, orders the possible values by id desc. You can change this order |
| 41 | + * by setting the `sort` prop (an object with `field` and `order` properties). |
| 42 | + * |
| 43 | + * @example |
| 44 | + * <ReferenceManyFieldBase sort={{ field: 'created_at', order: 'DESC' }} reference="comments" target="post_id"> |
| 45 | + * ... |
| 46 | + * </ReferenceManyFieldBase> |
| 47 | + * |
| 48 | + * Also, you can filter the query used to populate the possible values. Use the |
| 49 | + * `filter` prop for that. |
| 50 | + * |
| 51 | + * @example |
| 52 | + * <ReferenceManyFieldBase filter={{ is_published: true }} reference="comments" target="post_id"> |
| 53 | + * ... |
| 54 | + * </ReferenceManyFieldBase> |
| 55 | + */ |
| 56 | +export const ReferenceManyFieldBase = < |
| 57 | + RecordType extends RaRecord = RaRecord, |
| 58 | + ReferenceRecordType extends RaRecord = RaRecord, |
| 59 | +>( |
| 60 | + props: ReferenceManyFieldBaseProps<RecordType, ReferenceRecordType> |
| 61 | +) => { |
| 62 | + const { |
| 63 | + children, |
| 64 | + debounce, |
| 65 | + empty, |
| 66 | + filter = defaultFilter, |
| 67 | + page = 1, |
| 68 | + pagination = null, |
| 69 | + perPage = 25, |
| 70 | + record, |
| 71 | + reference, |
| 72 | + resource, |
| 73 | + sort = defaultSort, |
| 74 | + source = 'id', |
| 75 | + storeKey, |
| 76 | + target, |
| 77 | + queryOptions, |
| 78 | + } = props; |
| 79 | + |
| 80 | + const controllerProps = useReferenceManyFieldController< |
| 81 | + RecordType, |
| 82 | + ReferenceRecordType |
| 83 | + >({ |
| 84 | + debounce, |
| 85 | + filter, |
| 86 | + page, |
| 87 | + perPage, |
| 88 | + record, |
| 89 | + reference, |
| 90 | + resource, |
| 91 | + sort, |
| 92 | + source, |
| 93 | + storeKey, |
| 94 | + target, |
| 95 | + queryOptions, |
| 96 | + }); |
| 97 | + |
| 98 | + if ( |
| 99 | + // there is an empty page component |
| 100 | + empty && |
| 101 | + // there is no error |
| 102 | + !controllerProps.error && |
| 103 | + // the list is not loading data for the first time |
| 104 | + !controllerProps.isPending && |
| 105 | + // the API returned no data (using either normal or partial pagination) |
| 106 | + (controllerProps.total === 0 || |
| 107 | + (controllerProps.total == null && |
| 108 | + // @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it |
| 109 | + controllerProps.hasPreviousPage === false && |
| 110 | + // @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it |
| 111 | + controllerProps.hasNextPage === false && |
| 112 | + // @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it |
| 113 | + controllerProps.data.length === 0)) && |
| 114 | + // the user didn't set any filters |
| 115 | + !Object.keys(controllerProps.filterValues).length |
| 116 | + ) { |
| 117 | + return empty; |
| 118 | + } |
| 119 | + |
| 120 | + return ( |
| 121 | + <ResourceContextProvider value={reference}> |
| 122 | + <ListContextProvider value={controllerProps}> |
| 123 | + {children} |
| 124 | + {pagination} |
| 125 | + </ListContextProvider> |
| 126 | + </ResourceContextProvider> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export interface ReferenceManyFieldBaseProps< |
| 131 | + RecordType extends Record<string, any> = Record<string, any>, |
| 132 | + ReferenceRecordType extends Record<string, any> = Record<string, any>, |
| 133 | +> extends UseReferenceManyFieldControllerParams< |
| 134 | + RecordType, |
| 135 | + ReferenceRecordType |
| 136 | + > { |
| 137 | + children: ReactNode; |
| 138 | + empty?: ReactNode; |
| 139 | + pagination?: ReactElement; |
| 140 | +} |
| 141 | + |
| 142 | +const defaultFilter = {}; |
| 143 | +const defaultSort = { field: 'id', order: 'DESC' as const }; |
0 commit comments