Skip to content

Commit f29a61a

Browse files
authored
Merge pull request #9071 from marmelab/doc-clearer-explanation-for-option-text-in-automplete-input
[Doc] Clearer explanation for some props in AutompleteInput
2 parents d99a624 + 753b1fe commit f29a61a

3 files changed

Lines changed: 45 additions & 13 deletions

File tree

docs/AutocompleteInput.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ const choices = possibleValues.map(value => ({ id: value, name: ucfirst(value) }
145145

146146
To allow users to add new options, pass a React element as the `create` prop. `<AutocompleteInput>` will then render a menu item at the bottom of the list, which will render the passed element when clicked.
147147

148+
<video controls autoplay playsinline muted loop>
149+
<source src="./img/autocomplete-input-with-create.webm" type="video/webm"/>
150+
<source src="./img/autocomplete-input-with-create.mp4" type="video/mp4"/>
151+
Your browser does not support the video tag.
152+
</video>
153+
148154
{% raw %}
149155
```jsx
150156
import { CreateCategory } from './CreateCategory';
@@ -381,28 +387,54 @@ const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
381387

382388
`optionText` also accepts a React Element, that will be rendered inside a [`<RecordContext>`](./useRecordContext.md) using the related choice as the `record` prop. You can use Field components there. However, using an element as `optionText` implies that you also set two more props, `inputText` and `matchSuggestion`. See [Using A Custom Element For Options](#using-a-custom-element-for-options) for more details.
383389

384-
`optionText` is also useful when the choices are records [fetched from another resource](#fetching-choices), and `<AutocompleteInput>` is a child of a [`<ReferenceInput>`](./ReferenceInput.md).
390+
`optionText` can also be useful when the choices are records [fetched from another resource](#fetching-choices), and `<AutocompleteInput>` is a child of a [`<ReferenceInput>`](./ReferenceInput.md). In that case, react-admin uses the [`recordRepresentation`](./Resource.md#recordrepresentation) of the related resource to display the record label. In the example below, `<AutocompleteInput>` renders author options via their `last_name` attribute, because it's the record representation defined in the `<Resource name="authors">`:
385391

386392
```jsx
387-
import { AutocompleteInput, ReferenceInput } from 'react-admin';
393+
// in src/PostCreate.jsx
394+
import { AutocompleteInput, Create, ReferenceInput, SimpleForm } from 'react-admin';
388395

389-
<ReferenceInput label="Author" source="author_id" reference="authors">
390-
<AutocompleteInput />
391-
</ReferenceInput>
392-
```
396+
export const PostCreate = () => (
397+
<Create>
398+
<SimpleForm>
399+
<ReferenceInput label="Author" source="author_id" reference="authors">
400+
<AutocompleteInput />
401+
</ReferenceInput>
402+
</SimpleForm>
403+
</Create>
404+
);
405+
406+
// in src/App.js
407+
import { Admin, Resource, ListGuesser } from 'react-admin';
408+
import { dataProvider } from './dataProvider';
409+
import { PostCreate } from './PostCreate';
393410

394-
In that case, react-admin uses the [`recordRepresentation`](./Resource.md#recordrepresentation) of the related resource to display the record label. In the example above, `<AutocompleteInput>` uses the resource representation of the `authors` resource, which is the `name` property.
411+
export const App = () => (
412+
<Admin dataProvider={dataProvider}>
413+
<Resource name="posts" list={ListGuesser} create={PostCreate} />
414+
<Resource name="authors" recordRepresentation="last_name" />
415+
</Admin>
416+
)
417+
```
395418

396-
But if you set the `optionText` prop, react-admin uses it instead of relying on `recordRepresentation`.
419+
If you set the `optionText` prop, react-admin uses it instead of relying on `recordRepresentation`:
397420

398421
```jsx
399-
import { AutocompleteInput, ReferenceInput } from 'react-admin';
422+
// in src/PostCreate.jsx
423+
import { AutocompleteInput, Create, ReferenceInput, SimpleForm } from 'react-admin';
400424

401-
<ReferenceInput label="Author" source="author_id" reference="authors">
402-
<AutocompleteInput optionText="last_name" />
403-
</ReferenceInput>
425+
export const PostCreate = () => (
426+
<Create>
427+
<SimpleForm>
428+
<ReferenceInput label="Author" source="author_id" reference="authors">
429+
<AutocompleteInput optionText={author => `${author.first_name} ${author.last_name}`} />
430+
</ReferenceInput>
431+
</SimpleForm>
432+
</Create>
433+
);
404434
```
405435

436+
Now `<AutocompleteInput>` will render author options using their full name.
437+
406438
## `optionValue`
407439

408440
You can customize the property to use for the option value (instead of the default `id`) thanks to the `optionValue` prop:
@@ -799,7 +831,7 @@ const CreateCategory = () => {
799831

800832
**Tip:** As showcased in this example, react-admin provides a convenient hook for accessing the filter the user has already input in the `<AutocompleteInput>`: `useCreateSuggestionContext`.
801833

802-
The `Create %{item}` option will only be displayed once the user has already set a filter (by typing in some input). If you expect your users to create new items often, you can make this more user-friendly by adding a placeholder text like this:
834+
The `Create %{item}%` option will only be displayed once the user has already set a filter (by typing in some input). If you expect your users to create new items often, you can make this more user-friendly by adding a placeholder text like this:
803835

804836
{% raw %}
805837
```diff
164 KB
Binary file not shown.
70.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)