Skip to content

Commit c441872

Browse files
committed
Backport <ReferenceManyToManyInput queryOptions mutationOptions> doc + fix linter issues
1 parent 3b80f76 commit c441872

File tree

1 file changed

+64
-10
lines changed

1 file changed

+64
-10
lines changed

docs/ReferenceManyToManyInput.md

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "The ReferenceManyToManyInput Component"
55

66
# `<ReferenceManyToManyInput>`
77

8-
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="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)<img class="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.
99

1010
<video controls autoplay playsinline muted loop width="100%">
1111
<source src="https://react-admin-ee.marmelab.com/assets/reference-many-to-many-input.mp4" type="video/mp4" />
@@ -18,7 +18,7 @@ This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon"
1818

1919
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.
2020

21-
```
21+
```txt
2222
┌─────────┐ ┌──────────────┐ ┌───────────────┐
2323
│ bands │ │ performances │ │ venues │
2424
│---------│ │--------------│ │---------------│
@@ -29,7 +29,7 @@ Let's imagine that you're writing an app managing concerts for artists. The data
2929
└─────────┘ └──────────────┘ └───────────────┘
3030
```
3131

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`.
3333

3434
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:
3535

@@ -60,7 +60,7 @@ export const BandEdit = () => (
6060

6161
Note that although all possible child components support a `defaultValue` prop, it will only be applied on create views.
6262

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>`.
6464

6565
![Screenshot showing the use of ReferenceManyInput instead of ReferenceManyToManyInput](./img/reference-many-input-band-edit.png)
6666

@@ -107,16 +107,18 @@ const BandEdit = () => (
107107
| `through` | Required | `string` | - | Name of the resource for the associative table, e.g. 'book_authors' |
108108
| `filter` | Optional | `object` | `{}` | Filter for the associative table (passed to the `getManyReference()` call) |
109109
| `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. |
110111
| `perPage` | Optional | `number` | 25 | Limit for the number of results fetched from the associative table |
111112
| `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 |
112114
| `sort` | Optional | `{ field: string, order: 'ASC' or 'DESC' }` | `{ field: 'id', order: 'DESC' }` | Sort for the associative table (passed to the `getManyReference()` call) |
113115
| `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) |
114116
| `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. |
115117
| `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 |
116118

117119
## `children`
118120

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).
120122

121123
For instance, to allow user to choose `performances` using a `<SelectArrayInput>` instead of an `<AutocompleteArrayInput>`, you can write:
122124

@@ -152,6 +154,7 @@ export const BandEdit = () => (
152154
You can filter the records of the associative table (e.g. `performances`) using the `filter` prop. This `filter` is passed to the `getManyReference()` call.
153155

154156
{% raw %}
157+
155158
```tsx
156159
<ReferenceManyToManyInput
157160
reference="venues"
@@ -162,6 +165,7 @@ You can filter the records of the associative table (e.g. `performances`) using
162165
{/* ... */}
163166
</ReferenceManyToManyInput>
164167
```
168+
165169
{% endraw %}
166170

167171
## `filterChoices`
@@ -171,6 +175,7 @@ You can filter the records of the associative table (e.g. `performances`) using
171175
You can filter the possible values of the reference table using the `filterChoices` prop. This `filterChoices` is passed to the `getList()` call.
172176

173177
{% raw %}
178+
174179
```tsx
175180
<ReferenceManyToManyInput
176181
reference="venues"
@@ -181,6 +186,28 @@ You can filter the possible values of the reference table using the `filterChoic
181186
{/* ... */}
182187
</ReferenceManyToManyInput>
183188
```
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.
197+
198+
{% raw %}
199+
200+
```tsx
201+
<ReferenceManyToManyInput
202+
reference="venues"
203+
through="performances"
204+
using="band_id,venue_id"
205+
mutationOptions={{ meta: { myParameter: 'value' } }}
206+
>
207+
{/* ... */}
208+
</ReferenceManyToManyInput>
209+
```
210+
184211
{% endraw %}
185212

186213
## `perPage`
@@ -214,6 +241,28 @@ By default, react-admin displays at most 25 possible values from the reference t
214241
{/* ... */}
215242
</ReferenceManyToManyInput>
216243
```
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+
217266
## `reference`
218267

219268
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
230279
{/* ... */}
231280
</ReferenceManyToManyInput>
232281
```
282+
233283
## `sort`
234284

235285
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.
236286

237287
{% raw %}
288+
238289
```tsx
239290
<ReferenceManyToManyInput
240291
reference="venues"
@@ -245,13 +296,15 @@ By default, react-admin orders the possible values by `id` desc for the associat
245296
{/* ... */}
246297
</ReferenceManyToManyInput>
247298
```
299+
248300
{% endraw %}
249301

250302
## `sortChoices`
251303

252304
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).
253305

254306
{% raw %}
307+
255308
```tsx
256309
<ReferenceManyToManyInput
257310
reference="venues"
@@ -262,6 +315,7 @@ By default, react-admin orders the possible values by `id` desc for the referenc
262315
{/* ... */}
263316
</ReferenceManyToManyInput>
264317
```
318+
265319
{% endraw %}
266320

267321
## `source`
@@ -305,16 +359,16 @@ You can specify the columns to use in the associative using the `using` prop.
305359

306360
## Limitations
307361

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.
310364

311365
## DataProvider Calls
312366

313367
When rendered, `<ReferenceManyToManyInput>` fetches the `dataProvider` three times in a row:
314368

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
318372

319373
For instance, if the user edits the band of id `123`, `<ReferenceManyToManyInput>` first issues the following query to the `dataProvider`:
320374

0 commit comments

Comments
 (0)