Skip to content

Commit cbc2812

Browse files
committed
rewrite snippets with components for AutocompleteInput recordRepresentation example
1 parent 604cf07 commit cbc2812

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

docs/AutocompleteInput.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -390,31 +390,57 @@ const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
390390
`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).
391391

392392
```jsx
393-
// in ./index.js
394-
import {Admin, Resource } from 'react-admin';
393+
// in src/PostCreate.jsx
394+
import { AutocompleteInput, Create, ReferenceInput, SimpleForm } from 'react-admin';
395395

396-
<Admin {...adminProps}>
397-
<Resource name="authors" recordRepresentation={record => record.name} />
398-
</Admin>
396+
export const PostCreate = () => {
397+
return (
398+
<Create>
399+
<SimpleForm>
400+
<ReferenceInput
401+
label="Author"
402+
source="author_id"
403+
reference="authors"
404+
>
405+
<AutocompleteInput />
406+
</ReferenceInput>
407+
</SimpleForm>
408+
</Create>
409+
);
410+
};
399411

400-
// in ./PostCreate.jsx
401-
import { AutocompleteInput, ReferenceInput } from 'react-admin';
412+
// in src/App.js
413+
import {Admin, Resource, ListGuesser } from 'react-admin';
414+
import { dataProvider } from './dataProvider';
415+
import { PostCreate } from './PostCreate';
402416

403-
<ReferenceInput label="Author" source="author_id" reference="authors">
404-
<AutocompleteInput />
405-
</ReferenceInput>
417+
export const App = () => (
418+
<Admin dataProvider={dataProvider}>
419+
<Resource name="posts" list={ListGuesser} create={PostCreate} />
420+
<Resource name="authors" recordRepresentation={record => record.name} />
421+
</Admin>
422+
)
406423
```
407424

408425
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.
409426

410427
But if you set the `optionText` prop, react-admin uses it instead of relying on `recordRepresentation`.
411428

412429
```jsx
413-
import { AutocompleteInput, ReferenceInput } from 'react-admin';
430+
// in src/PostCreate.jsx
431+
import { AutocompleteInput, Create, ReferenceInput, SimpleForm } from 'react-admin';
414432

415-
<ReferenceInput label="Author" source="author_id" reference="authors">
416-
<AutocompleteInput optionText="last_name" />
417-
</ReferenceInput>
433+
export const PostCreate = () => {
434+
return (
435+
<Create>
436+
<SimpleForm>
437+
<ReferenceInput label="Author" source="author_id" reference="authors">
438+
<AutocompleteInput optionText="last_name" />
439+
</ReferenceInput>
440+
</SimpleForm>
441+
</Create>
442+
);
443+
};
418444
```
419445
In the example above, `<AutocompleteInput>` uses the `last_name` property from `authors` resource instead of their `recordRepresentation`.
420446

0 commit comments

Comments
 (0)