-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathReferenceManyField.tsx
179 lines (169 loc) · 4.89 KB
/
ReferenceManyField.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { FC, ReactElement, ReactNode } from 'react';
import PropTypes from 'prop-types';
import {
FilterPayload,
SortPayload,
useReferenceManyFieldController,
ListContextProvider,
ListControllerResult,
ResourceContextProvider,
useRecordContext,
RaRecord,
} from 'ra-core';
import { fieldPropTypes, FieldProps } from './types';
/**
* Render related records to the current one.
*
* You must define the fields to be passed to the iterator component as children.
*
* @example Display all the comments of the current post as a datagrid
* <ReferenceManyField reference="comments" target="post_id">
* <Datagrid>
* <TextField source="id" />
* <TextField source="body" />
* <DateField source="created_at" />
* <EditButton />
* </Datagrid>
* </ReferenceManyField>
*
* @example Display all the books by the current author, only the title
* <ReferenceManyField reference="books" target="author_id">
* <SingleFieldList>
* <ChipField source="title" />
* </SingleFieldList>
* </ReferenceManyField>
*
* By default, restricts the displayed values to 25. You can extend this limit
* by setting the `perPage` prop.
*
* @example
* <ReferenceManyField perPage={10} reference="comments" target="post_id">
* ...
* </ReferenceManyField>
*
* By default, orders the possible values by id desc. You can change this order
* by setting the `sort` prop (an object with `field` and `order` properties).
*
* @example
* <ReferenceManyField sort={{ field: 'created_at', order: 'DESC' }} reference="comments" target="post_id">
* ...
* </ReferenceManyField>
*
* Also, you can filter the query used to populate the possible values. Use the
* `filter` prop for that.
*
* @example
* <ReferenceManyField filter={{ is_published: true }} reference="comments" target="post_id">
* ...
* </ReferenceManyField>
*/
export const ReferenceManyField = <
RecordType extends RaRecord = RaRecord,
ReferenceRecordType extends RaRecord = RaRecord
>(
props: ReferenceManyFieldProps<RecordType>
) => {
const {
children,
filter = defaultFilter,
page = 1,
pagination = null,
perPage = 25,
reference,
resource,
sort = defaultSort,
source = 'id',
target,
} = props;
const record = useRecordContext(props);
const controllerProps = useReferenceManyFieldController<
RecordType,
ReferenceRecordType
>({
filter,
page,
perPage,
record,
reference,
resource,
sort,
source,
target,
});
return (
<ResourceContextProvider value={reference}>
<ListContextProvider value={controllerProps}>
{children}
{pagination}
</ListContextProvider>
</ResourceContextProvider>
);
};
export interface ReferenceManyFieldProps<
RecordType extends Record<string, any> = Record<string, any>
> extends FieldProps<RecordType> {
children: ReactNode;
filter?: FilterPayload;
page?: number;
pagination?: ReactElement;
perPage?: number;
reference: string;
sort?: SortPayload;
target: string;
}
ReferenceManyField.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
filter: PropTypes.object,
label: fieldPropTypes.label,
perPage: PropTypes.number,
record: PropTypes.any,
reference: PropTypes.string.isRequired,
resource: PropTypes.string,
sortBy: PropTypes.string,
sortByOrder: fieldPropTypes.sortByOrder,
source: PropTypes.string,
sort: PropTypes.exact({
field: PropTypes.string,
order: PropTypes.string,
}),
target: PropTypes.string.isRequired,
};
// FIXME kept for backwards compatibility, unused, to be removed in v5
export const ReferenceManyFieldView: FC<ReferenceManyFieldViewProps> = props => {
const { children, pagination } = props;
if (process.env.NODE_ENV !== 'production') {
console.error(
'<ReferenceManyFieldView> is deprecated, use <ReferenceManyField> directly'
);
}
return (
<>
{children}
{pagination && props.total !== undefined ? pagination : null}
</>
);
};
export interface ReferenceManyFieldViewProps
extends Omit<
ReferenceManyFieldProps,
'resource' | 'page' | 'perPage' | 'sort'
>,
ListControllerResult {
children: ReactElement;
}
ReferenceManyFieldView.propTypes = {
children: PropTypes.element,
className: PropTypes.string,
sort: PropTypes.exact({
field: PropTypes.string,
order: PropTypes.string,
}),
data: PropTypes.any,
isLoading: PropTypes.bool,
pagination: PropTypes.element,
reference: PropTypes.string,
setSort: PropTypes.func,
};
const defaultFilter = {};
const defaultSort = { field: 'id', order: 'DESC' };