You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ReferenceManyToManyInput.md
+64-10Lines changed: 64 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: "The ReferenceManyToManyInput Component"
5
5
6
6
# `<ReferenceManyToManyInput>`
7
7
8
-
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<imgclass="icon"src="./img/premium.svg" /> component allows to create, edit or remove relationships between two resources sharing an associative table. The changes in the associative table are sent to the dataProvider _when the user submits the form_, so that they can cancel the changes before submission.
8
+
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<imgclass="icon"src="./img/premium.svg"alt="premium icon"/> component allows to create, edit or remove relationships between two resources sharing an associative table. The changes in the associative table are sent to the dataProvider _when the user submits the form_, so that they can cancel the changes before submission.
@@ -18,7 +18,7 @@ This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon"
18
18
19
19
Let's imagine that you're writing an app managing concerts for artists. The data model features a many-to-many relationship between the `bands` and `venues` tables through a `performances` associative table.
20
20
21
-
```
21
+
```txt
22
22
┌─────────┐ ┌──────────────┐ ┌───────────────┐
23
23
│ bands │ │ performances │ │ venues │
24
24
│---------│ │--------------│ │---------------│
@@ -29,7 +29,7 @@ Let's imagine that you're writing an app managing concerts for artists. The data
29
29
└─────────┘ └──────────────┘ └───────────────┘
30
30
```
31
31
32
-
In this example, `bands.id` matches `performances.band_id`, and `performances.venue_id` matches `venues.id`.
32
+
In this example, `bands.id` matches `performances.band_id`, and `performances.venue_id` matches `venues.id`.
33
33
34
34
To let users edit the `venues` for given `band` in an `<AutocompleteArrayInput>`, wrap that input in a `<ReferenceManyToManyInput>` where you define the relationship via the `reference`, `through` and `using` props:
35
35
@@ -60,7 +60,7 @@ export const BandEdit = () => (
60
60
61
61
Note that although all possible child components support a `defaultValue` prop, it will only be applied on create views.
62
62
63
-
**Tip**: If you need to edit the fields of the associative table (e.g. the `date` in `performances`), you can use a [`<ReferenceManyInput>`](./ReferenceManyInput.md) instead of `<ReferenceManyToManyInput>`.
63
+
**Tip**: If you need to edit the fields of the associative table (e.g. the `date` in `performances`), you can use a [`<ReferenceManyInput>`](./ReferenceManyInput.md) instead of `<ReferenceManyToManyInput>`.
64
64
65
65

66
66
@@ -107,16 +107,18 @@ const BandEdit = () => (
107
107
|`through`| Required |`string`| - | Name of the resource for the associative table, e.g. 'book_authors' |
108
108
|`filter`| Optional |`object`|`{}`| Filter for the associative table (passed to the `getManyReference()` call) |
109
109
|`filter Choices`| Optional |`object`|`{}`| Filter for the possible choices fetched from the reference table (passed to the `getList()` call) |
110
+
|`mutationOptions`| Optional |`{ meta, onError }`| - | Mutation options for the `create` and `deleteMany` calls. Only `meta` and `onError` are supported. |
110
111
|`perPage`| Optional |`number`| 25 | Limit for the number of results fetched from the associative table |
111
112
|`perPage Choices`| Optional |`number`| 25 | Limit for the number of possible choices fetched from the reference table |
113
+
|`queryOptions`| Optional |`UseQueryOptions`| - | Query options for the `getList`, `getMany` and `getManyReference` calls |
112
114
|`sort`| Optional |`{ field: string, order: 'ASC' or 'DESC' }`|`{ field: 'id', order: 'DESC' }`| Sort for the associative table (passed to the `getManyReference()` call) |
113
115
|`sort Choices`| Optional |`{ field: string, order: 'ASC' or 'DESC' }`|`{ field: 'id', order: 'DESC' }`| Sort for the possible choices fetched from the reference table (passed to the `getList()` call) |
114
116
|`source`| Optional |`string`|`'id'`| Name of the field containing the identity of the main resource. Used determine the value to look for in the associative table. |
115
117
|`using`| Optional |`string`|`'([resource]_id,[reference]_id)'`| Tuple (comma separated) of the two field names used as foreign keys, e.g 'book_id,author_id'. The tuple should start with the field pointing to the resource, and finish with the field pointing to the reference |
116
118
117
119
## `children`
118
120
119
-
`<ReferenceManyToManyInput>` expects a _select_ component as child, i.e. a component working inside a `ChoiceContext`. That means you can use a [`<SelectArrayInput>`](./SelectArrayInput.md), or a [`<AutocompleteArrayInput>`](./AutocompleteArrayInput.md).
121
+
`<ReferenceManyToManyInput>` expects a _select_ component as child, i.e. a component working inside a `ChoiceContext`. That means you can use a [`<SelectArrayInput>`](./SelectArrayInput.md), or a [`<AutocompleteArrayInput>`](./AutocompleteArrayInput.md).
120
122
121
123
For instance, to allow user to choose `performances` using a `<SelectArrayInput>` instead of an `<AutocompleteArrayInput>`, you can write:
You can filter the records of the associative table (e.g. `performances`) using the `filter` prop. This `filter` is passed to the `getManyReference()` call.
153
155
154
156
{% raw %}
157
+
155
158
```tsx
156
159
<ReferenceManyToManyInput
157
160
reference="venues"
@@ -162,6 +165,7 @@ You can filter the records of the associative table (e.g. `performances`) using
162
165
{/* ... */}
163
166
</ReferenceManyToManyInput>
164
167
```
168
+
165
169
{% endraw %}
166
170
167
171
## `filterChoices`
@@ -171,6 +175,7 @@ You can filter the records of the associative table (e.g. `performances`) using
171
175
You can filter the possible values of the reference table using the `filterChoices` prop. This `filterChoices` is passed to the `getList()` call.
172
176
173
177
{% raw %}
178
+
174
179
```tsx
175
180
<ReferenceManyToManyInput
176
181
reference="venues"
@@ -181,6 +186,28 @@ You can filter the possible values of the reference table using the `filterChoic
181
186
{/* ... */}
182
187
</ReferenceManyToManyInput>
183
188
```
189
+
190
+
{% endraw %}
191
+
192
+
## `mutationOptions`
193
+
194
+
Use the `mutationOptions` prop to customize the `create` and `deleteMany` mutations.
195
+
196
+
You can for instance use it to pass [a custom meta](./Actions.md#meta-parameter) to the dataProvider.
@@ -214,6 +241,28 @@ By default, react-admin displays at most 25 possible values from the reference t
214
241
{/* ... */}
215
242
</ReferenceManyToManyInput>
216
243
```
244
+
245
+
## `queryOptions`
246
+
247
+
Use the `queryOptions` prop to customize the queries for `getList`, `getMany` and `getManyReference`.
248
+
249
+
You can for instance use it to pass [a custom meta](./Actions.md#meta-parameter) to the dataProvider.
250
+
251
+
{% raw %}
252
+
253
+
```tsx
254
+
<ReferenceManyToManyInput
255
+
reference="venues"
256
+
through="performances"
257
+
using="band_id,venue_id"
258
+
queryOptions={{ meta: { myParameter: 'value' } }}
259
+
>
260
+
{/* ... */}
261
+
</ReferenceManyToManyInput>
262
+
```
263
+
264
+
{% endraw %}
265
+
217
266
## `reference`
218
267
219
268
The name of the target resource to fetch.
@@ -230,11 +279,13 @@ For instance, if you want to display the venues of a given bands, through perfor
230
279
{/* ... */}
231
280
</ReferenceManyToManyInput>
232
281
```
282
+
233
283
## `sort`
234
284
235
285
By default, react-admin orders the possible values by `id` desc for the associative table (e.g. `performances`). You can change this order by setting the `sort` prop (an object with `field` and `order` properties) to be applied to the associative resource.
236
286
237
287
{% raw %}
288
+
238
289
```tsx
239
290
<ReferenceManyToManyInput
240
291
reference="venues"
@@ -245,13 +296,15 @@ By default, react-admin orders the possible values by `id` desc for the associat
245
296
{/* ... */}
246
297
</ReferenceManyToManyInput>
247
298
```
299
+
248
300
{% endraw %}
249
301
250
302
## `sortChoices`
251
303
252
304
By default, react-admin orders the possible values by `id` desc for the reference table (e.g. `venues`). You can change this order by setting the `sortChoices` prop (an object with `field` and `order` properties).
253
305
254
306
{% raw %}
307
+
255
308
```tsx
256
309
<ReferenceManyToManyInput
257
310
reference="venues"
@@ -262,6 +315,7 @@ By default, react-admin orders the possible values by `id` desc for the referenc
262
315
{/* ... */}
263
316
</ReferenceManyToManyInput>
264
317
```
318
+
265
319
{% endraw %}
266
320
267
321
## `source`
@@ -305,16 +359,16 @@ You can specify the columns to use in the associative using the `using` prop.
305
359
306
360
## Limitations
307
361
308
-
-`<ReferenceManyToManyInput>` cannot be used inside an `<ArrayInput>`, a `<ReferenceOneInput>` or a `<ReferenceManyInput>`.
309
-
-`<ReferenceManyToManyInput>` does not support server side validation.
362
+
-`<ReferenceManyToManyInput>` cannot be used inside an `<ArrayInput>`, a `<ReferenceOneInput>` or a `<ReferenceManyInput>`.
363
+
-`<ReferenceManyToManyInput>` does not support server side validation.
310
364
311
365
## DataProvider Calls
312
366
313
367
When rendered, `<ReferenceManyToManyInput>` fetches the `dataProvider` three times in a row:
314
368
315
-
-once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
316
-
-once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
317
-
-once to get the possible values of the reference resource (`venues` in this case) to show as suggestions in the input, using a `getList()` call
369
+
- once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
370
+
- once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
371
+
- once to get the possible values of the reference resource (`venues` in this case) to show as suggestions in the input, using a `getList()` call
318
372
319
373
For instance, if the user edits the band of id `123`, `<ReferenceManyToManyInput>` first issues the following query to the `dataProvider`:
0 commit comments