|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "<ArrayFieldBase>" |
| 4 | +--- |
| 5 | + |
| 6 | +Use `<ArrayFieldBase>` to display an embedded array of objects from the current record. |
| 7 | + |
| 8 | +`<ArrayFieldBase>` reads the array field value from the current [`RecordContext`](./useRecordContext.md), creates a [`ListContext`](./useListContext.md) from it, and renders its children. This component is headless, so its children need to use that list context to render the desired UI. |
| 9 | + |
| 10 | +**Tip**: Use [`<ReferenceArrayFieldBase>`](./ReferenceArrayFieldBase.md) when the array contains foreign keys to another resource. Use `<ArrayFieldBase>` when the array already contains the embedded objects to display. |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +`<ArrayFieldBase>` is ideal for collections of embedded objects, like `tags` and `backlinks` in the following `post` record: |
| 15 | + |
| 16 | +```js |
| 17 | +{ |
| 18 | + id: 123, |
| 19 | + title: 'Lorem Ipsum Sit Amet', |
| 20 | + tags: [{ name: 'dolor' }, { name: 'sit' }, { name: 'amet' }], |
| 21 | + backlinks: [ |
| 22 | + { |
| 23 | + uuid: '34fdf393-f449-4b04-a423-38ad02ae159e', |
| 24 | + date: '2012-08-10T00:00:00.000Z', |
| 25 | + url: 'https://example.com/foo/bar.html', |
| 26 | + }, |
| 27 | + { |
| 28 | + uuid: 'd907743a-253d-4ec1-8329-404d4c5e6cf1', |
| 29 | + date: '2012-08-14T00:00:00.000Z', |
| 30 | + url: 'https://blog.johndoe.com/2012/08/12/foobar.html', |
| 31 | + }, |
| 32 | + ], |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +You can use `<ArrayFieldBase>` in a show view and render the embedded records with any component reading the list context: |
| 37 | + |
| 38 | +```jsx |
| 39 | +import { ArrayFieldBase, RecordsIterator, ShowBase } from 'ra-core'; |
| 40 | + |
| 41 | +const PostShow = () => ( |
| 42 | + <ShowBase> |
| 43 | + <div> |
| 44 | + <ArrayFieldBase source="tags"> |
| 45 | + <ul> |
| 46 | + <RecordsIterator render={tag => <li>{tag.name}</li>} /> |
| 47 | + </ul> |
| 48 | + </ArrayFieldBase> |
| 49 | + <ArrayFieldBase source="backlinks"> |
| 50 | + <ul> |
| 51 | + <RecordsIterator |
| 52 | + render={backlink => ( |
| 53 | + <li> |
| 54 | + <a href={backlink.url}>{backlink.url}</a> |
| 55 | + </li> |
| 56 | + )} |
| 57 | + /> |
| 58 | + </ul> |
| 59 | + </ArrayFieldBase> |
| 60 | + </div> |
| 61 | + </ShowBase> |
| 62 | +); |
| 63 | +``` |
| 64 | + |
| 65 | +## Props |
| 66 | + |
| 67 | +| Prop | Required | Type | Default | Description | |
| 68 | +| ---------- | -------- | -------------------- | ------- | ------------------------------------------------------------------------------------ | |
| 69 | +| `children` | Required | `ReactNode` | - | The UI rendered inside the `ListContext`. | |
| 70 | +| `filter` | Optional | `object` | - | A permanent filter applied client-side to the embedded array. | |
| 71 | +| `exporter` | Optional | `function \| false` | - | The exporter function exposed through the list context for export actions. | |
| 72 | +| `perPage` | Optional | `number` | `1000` | The number of records to display per page. | |
| 73 | +| `sort` | Optional | `{ field, order }` | - | The sort applied client-side to the embedded array. | |
| 74 | + |
| 75 | +`<ArrayFieldBase>` also accepts the base field props `source`, `record`, and `resource`. |
| 76 | + |
| 77 | +Because it relies on [`useList`](./useList.md), `<ArrayFieldBase>` supports the same local filtering, sorting, pagination, and export behavior as other list-context-based components. |
| 78 | + |
| 79 | +## `children` |
| 80 | + |
| 81 | +`<ArrayFieldBase>` renders its `children` inside a [`ListContext`](./useListContext.md). Common choices are [`<RecordsIterator>`](./RecordsIterator.md), [`<WithListContext>`](./WithListContext.md), or any custom component using `useListContext()`. |
| 82 | + |
| 83 | +```jsx |
| 84 | +import { ArrayFieldBase, WithListContext } from 'ra-core'; |
| 85 | + |
| 86 | +const BacklinksField = () => ( |
| 87 | + <ArrayFieldBase source="backlinks"> |
| 88 | + <WithListContext |
| 89 | + render={({ data }) => ( |
| 90 | + <ul> |
| 91 | + {data?.map(backlink => ( |
| 92 | + <li key={backlink.uuid}>{backlink.url}</li> |
| 93 | + ))} |
| 94 | + </ul> |
| 95 | + )} |
| 96 | + /> |
| 97 | + </ArrayFieldBase> |
| 98 | +); |
| 99 | +``` |
| 100 | +
|
| 101 | +## `filter` |
| 102 | +
|
| 103 | +By default, `<ArrayFieldBase>` displays all records from the embedded array. Use the `filter` prop to keep only matching items. Filtering happens client-side, after reading the array value from the record. |
| 104 | +
|
| 105 | +```jsx |
| 106 | +<ArrayFieldBase |
| 107 | + source="backlinks" |
| 108 | + filter={{ date: '2012-08-10T00:00:00.000Z' }} |
| 109 | +> |
| 110 | + <WithListContext |
| 111 | + render={({ data }) => ( |
| 112 | + <ul> |
| 113 | + {data?.map(backlink => ( |
| 114 | + <li key={backlink.uuid}>{backlink.url}</li> |
| 115 | + ))} |
| 116 | + </ul> |
| 117 | + )} |
| 118 | + /> |
| 119 | +</ArrayFieldBase> |
| 120 | +``` |
| 121 | +
|
| 122 | +## `perPage` |
| 123 | +
|
| 124 | +Because `<ArrayFieldBase>` creates a [`ListContext`](./useListContext.md), you can paginate the embedded array with any pagination UI wired to that context. |
| 125 | +
|
| 126 | +## `sort` |
| 127 | +
|
| 128 | +By default, `<ArrayFieldBase>` displays items in the order they appear in the array. Use the `sort` prop to apply a client-side sort. |
| 129 | +
|
| 130 | +```jsx |
| 131 | +<ArrayFieldBase source="tags" sort={{ field: 'name', order: 'ASC' }}> |
| 132 | + <ul> |
| 133 | + <RecordsIterator render={tag => <li>{tag.name}</li>} /> |
| 134 | + </ul> |
| 135 | +</ArrayFieldBase> |
| 136 | +``` |
| 137 | +
|
| 138 | +## `exporter` |
| 139 | +
|
| 140 | +If one of the children exposes an export action through the list context, you can customize the export behavior with the `exporter` prop, or disable it entirely by passing `false`. |
0 commit comments