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

Update handling-relations.md with an example of ReferenceArrayInput #1144

Open
wants to merge 1 commit into
base: 2.5
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions admin/handling-relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ const ReviewsEdit = props => (
>
<AutocompleteInput optionText="title" />
</ReferenceInput>
// or
<ReferenceInput
source="book.@id"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using the format prop is clearer than having this source.

reference="books"
label="Books"
filterToQuery={searchText => ({ title: searchText })}
>
<AutocompleteInput optionText="title" />
</ReferenceInput>

<InputGuesser source="rating" />
<InputGuesser source="body" />
Expand All @@ -345,3 +354,42 @@ export default () => (
```

The autocomplete field should now work properly!

Let's say `useEmbedded` parameter is set to `true` and you have nested a nested objects array you want to use in an ReferenceArrayInput. ReferenceArrayInput expects an array of ids. In this case you have to tell it, where it finds those ids. This is done via "format"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Let's say `useEmbedded` parameter is set to `true` and you have nested a nested objects array you want to use in an ReferenceArrayInput. ReferenceArrayInput expects an array of ids. In this case you have to tell it, where it finds those ids. This is done via "format"
Let's say `useEmbedded` parameter is set to `true` and you have nested an array of objects you want to use in a `ReferenceArrayInput`. `ReferenceArrayInput` expects an array of ids. In this case, you have to tell it where to find those ids. This is done by using the `format` prop.


```javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to add this example above than to write again the example.

import React from "react";
import {
HydraAdmin,
ResourceGuesser,
CreateGuesser,
EditGuesser,
InputGuesser
} from "@api-platform/admin";
import { ReferenceInput, AutocompleteInput } from "react-admin";

...
const BooksEdit = props => (
<EditGuesser {...props}>
...

<ReferenceArrayInput source="books" reference="books_reference" label="Books" sort={{field: 'title', order: 'ASC'}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indent properly?

format={v => (v ? v.map(i => (i["@id"] 11? i["@id"] : i)) : [])}>
Copy link
Member

@alanpoulain alanpoulain Jul 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
format={v => (v ? v.map(i => (i["@id"] 11? i["@id"] : i)) : [])}>
format={v => (v ? v.map(i => (i["@id"] || i)) : [])}>

<SelectArrayInput source="title" optionText="title" optionValue="@id"/>
</ReferenceArrayInput>
...
</EditGuesser>
);

...
export default () => (
<HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}>
<ResourceGuesser
name="books_reference"
edit={BooksEdit}
...
/>
</HydraAdmin>
);

```