|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "The ArrayFieldBase Component" |
| 4 | +--- |
| 5 | + |
| 6 | +# `<ArrayFieldBase>` |
| 7 | + |
| 8 | +`<ArrayFieldBase>` renders an embedded array of objects. |
| 9 | +`<ArrayFieldBase>` is a headless component, handling only the list logic. This allows you to use any UI library for the render. For a Material UI version, see [`<ArrayField>`](./ArrayField.md). |
| 10 | + |
| 11 | +`<ArrayFieldBase>` creates a [`ListContext`](./useListContext.md) with the field value, and renders its children components. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +`<ArrayFieldBase>` is ideal for collections of objects, e.g. `tags` and `backlinks` in the following `post` object: |
| 16 | + |
| 17 | +```js |
| 18 | +{ |
| 19 | + id: 123, |
| 20 | + title: 'Lorem Ipsum Sit Amet', |
| 21 | + tags: [{ name: 'dolor' }, { name: 'sit' }, { name: 'amet' }], |
| 22 | + backlinks: [ |
| 23 | + { |
| 24 | + uuid: '34fdf393-f449-4b04-a423-38ad02ae159e', |
| 25 | + date: '2012-08-10T00:00:00.000Z', |
| 26 | + url: 'https://example.com/foo/bar.html', |
| 27 | + }, |
| 28 | + { |
| 29 | + uuid: 'd907743a-253d-4ec1-8329-404d4c5e6cf1', |
| 30 | + date: '2012-08-14T00:00:00.000Z', |
| 31 | + url: 'https://blog.johndoe.com/2012/08/12/foobar.html', |
| 32 | + } |
| 33 | + ] |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +You can leverage `<ArrayFieldBase>` in a Show view and render the list using any component reading the list context: |
| 38 | + |
| 39 | +{% raw %} |
| 40 | +```jsx |
| 41 | +import { |
| 42 | + ArrayFieldBase, |
| 43 | + RecordsIterator, |
| 44 | + Show, |
| 45 | + SimpleShowLayout, |
| 46 | + TextField, |
| 47 | +} from 'react-admin'; |
| 48 | + |
| 49 | +const PostShow = () => ( |
| 50 | + <Show> |
| 51 | + <SimpleShowLayout> |
| 52 | + <TextField source="title" /> |
| 53 | + <ArrayFieldBase source="tags"> |
| 54 | + <ul> |
| 55 | + <RecordsIterator render={tag => <li>{tag.name}</li>} /> |
| 56 | + </ul> |
| 57 | + </ArrayFieldBase> |
| 58 | + <ArrayFieldBase source="backlinks"> |
| 59 | + <ul> |
| 60 | + <RecordsIterator |
| 61 | + render={backlink => ( |
| 62 | + <li> |
| 63 | + <a href={backlink.url}>{backlink.url}</a> |
| 64 | + </li> |
| 65 | + )} |
| 66 | + /> |
| 67 | + </ul> |
| 68 | + </ArrayFieldBase> |
| 69 | + </SimpleShowLayout> |
| 70 | + </Show> |
| 71 | +); |
| 72 | +``` |
| 73 | +{% endraw %} |
| 74 | + |
| 75 | +## Props |
| 76 | + |
| 77 | +| Prop | Required | Type | Default | Description | |
| 78 | +|------------|----------|-------------------|---------|------------------------------------------| |
| 79 | +| `children` | Required | `ReactNode` | | The component to render the list. | |
| 80 | +| `filter` | Optional | `object` | | The filter to apply to the list. | |
| 81 | +| `exporter` | Optional | `function` | `default Exporter` | The function called by export buttons in the list context. | |
| 82 | +| `perPage` | Optional | `number` | 1000 | The number of items to display per page. | |
| 83 | +| `sort` | Optional | `{ field, order}` | | The sort to apply to the list. | |
| 84 | + |
| 85 | +`<ArrayFieldBase>` accepts the base field props `source`, `record`, and `resource`. |
| 86 | + |
| 87 | +`<ArrayFieldBase>` relies on [`useList`](./useList.md) to filter, paginate, and sort the data, so it accepts the same props. |
| 88 | + |
| 89 | +## `children` |
| 90 | + |
| 91 | +`<ArrayFieldBase>` renders its `children` wrapped in a [`<ListContextProvider>`](./useListContext.md). Commonly used children are [`<RecordsIterator>`](./RecordsIterator.md), [`<WithListContext>`](./WithListContext.md), or any custom component using `useListContext()`. |
| 92 | + |
| 93 | +{% raw %} |
| 94 | +```jsx |
| 95 | +import { ArrayFieldBase, WithListContext } from 'react-admin'; |
| 96 | + |
| 97 | +const BacklinksField = () => ( |
| 98 | + <ArrayFieldBase source="backlinks"> |
| 99 | + <WithListContext |
| 100 | + render={({ data }) => ( |
| 101 | + <ul> |
| 102 | + {data?.map(backlink => ( |
| 103 | + <li key={backlink.uuid}>{backlink.url}</li> |
| 104 | + ))} |
| 105 | + </ul> |
| 106 | + )} |
| 107 | + /> |
| 108 | + </ArrayFieldBase> |
| 109 | +); |
| 110 | +``` |
| 111 | +{% endraw %} |
| 112 | +
|
| 113 | +## `filter` |
| 114 | +
|
| 115 | +By default, `<ArrayFieldBase>` displays all the records in the embedded array. Use the `filter` prop to restrict them. |
| 116 | +
|
| 117 | +{% raw %} |
| 118 | +```jsx |
| 119 | +<ArrayFieldBase |
| 120 | + source="backlinks" |
| 121 | + filter={{ date: '2012-08-10T00:00:00.000Z' }} |
| 122 | +> |
| 123 | + <WithListContext |
| 124 | + render={({ data }) => ( |
| 125 | + <ul> |
| 126 | + {data?.map(backlink => ( |
| 127 | + <li key={backlink.uuid}>{backlink.url}</li> |
| 128 | + ))} |
| 129 | + </ul> |
| 130 | + )} |
| 131 | + /> |
| 132 | +</ArrayFieldBase> |
| 133 | +``` |
| 134 | +{% endraw %} |
| 135 | +
|
| 136 | +## `perPage` |
| 137 | +
|
| 138 | +Because `<ArrayFieldBase>` creates a [`ListContext`](./useListContext.md), you can paginate the embedded array with any pagination UI wired to that context. |
| 139 | +
|
| 140 | +## `sort` |
| 141 | +
|
| 142 | +By default, `<ArrayFieldBase>` displays the items in the order they are stored in the field. You can use the `sort` prop to change the sort order. |
| 143 | +
|
| 144 | +{% raw %} |
| 145 | +```jsx |
| 146 | +<ArrayFieldBase source="tags" sort={{ field: 'name', order: 'ASC' }}> |
| 147 | + <ul> |
| 148 | + <RecordsIterator render={tag => <li>{tag.name}</li>} /> |
| 149 | + </ul> |
| 150 | +</ArrayFieldBase> |
| 151 | +``` |
| 152 | +{% endraw %} |
0 commit comments