|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "The TextArrayField Component" |
| 4 | +storybook_path: ra-ui-materialui-fields-textarrayfield--basic |
| 5 | +--- |
| 6 | + |
| 7 | +# `<TextArrayField>` |
| 8 | + |
| 9 | +`<TextArrayField>` renders an array of scalar values using Material-UI's Stack and Chips. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +`<TextArrayField>` is ideal for displaying lists of simple text values, such as genres or tags, in a visually appealing way. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +`<TextArrayField>` can be used in a Show view to display an array of values from a record. For example: |
| 18 | + |
| 19 | +```js |
| 20 | +const book = { |
| 21 | + id: 1, |
| 22 | + title: 'War and Peace', |
| 23 | + genres: [ |
| 24 | + 'Fiction', |
| 25 | + 'Historical Fiction', |
| 26 | + 'Classic Literature', |
| 27 | + 'Russian Literature', |
| 28 | + ], |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +You can render the `TextArrayField` like this: |
| 33 | + |
| 34 | +```jsx |
| 35 | +import { Show, SimpleShowLayout, TextArrayField } from 'react-admin'; |
| 36 | + |
| 37 | +const BookShow = () => ( |
| 38 | + <Show> |
| 39 | + <SimpleShowLayout> |
| 40 | + <TextField source="title" /> |
| 41 | + <TextArrayField source="genres" /> |
| 42 | + </SimpleShowLayout> |
| 43 | + </Show> |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +## Props |
| 48 | + |
| 49 | +The following props are available for `<TextArrayField>`: |
| 50 | + |
| 51 | +| Prop | Type | Required | Description | |
| 52 | +| ----------- | ------------ | -------- | ------------------------------------------------------------- | |
| 53 | +| `source` | `string` | Yes | The name of the record field containing the array to display. | |
| 54 | +| `color` | `string` | - | The color of the Chip components. | |
| 55 | +| `emptyText` | `ReactNode` | - | Text to display when the array is empty. | |
| 56 | +| `record` | `RecordType` | - | The record containing the data to display. | |
| 57 | +| `size` | `string` | - | The size of the Chip components. | |
| 58 | +| `variant` | `string` | - | The variant of the Chip components. | |
| 59 | + |
| 60 | +Additional props are passed to the underlying [Material-UI `Stack` component](https://mui.com/material-ui/react-stack/). |
| 61 | + |
| 62 | +## `color` |
| 63 | + |
| 64 | +The color of the Chip components. Accepts any value supported by MUI's Chip (`primary`, `secondary`, etc). |
| 65 | + |
| 66 | +```jsx |
| 67 | +<TextArrayField source="genres" color="secondary" /> |
| 68 | +``` |
| 69 | + |
| 70 | +## `direction` |
| 71 | + |
| 72 | +The direction of the Stack layout. Accepts `row` or `column`. The default is `row`. |
| 73 | + |
| 74 | +```jsx |
| 75 | +<TextArrayField source="genres" direction="column" /> |
| 76 | +``` |
| 77 | + |
| 78 | +## `emptyText` |
| 79 | + |
| 80 | +Text to display when the array is empty. |
| 81 | + |
| 82 | +```jsx |
| 83 | +<TextArrayField source="genres" emptyText="No genres available" /> |
| 84 | +``` |
| 85 | + |
| 86 | +## `record` |
| 87 | + |
| 88 | +The record containing the data to display. Usually provided by react-admin automatically. |
| 89 | + |
| 90 | +```jsx |
| 91 | +const book = { |
| 92 | + id: 1, |
| 93 | + title: 'War and Peace', |
| 94 | + genres: [ |
| 95 | + 'Fiction', |
| 96 | + 'Historical Fiction', |
| 97 | + 'Classic Literature', |
| 98 | + 'Russian Literature', |
| 99 | + ], |
| 100 | +}; |
| 101 | + |
| 102 | +<TextArrayField source="genres" record={book} /> |
| 103 | +``` |
| 104 | + |
| 105 | +## `size` |
| 106 | + |
| 107 | +The size of the Chip components. Accepts any value supported by MUI's Chip (`small`, `medium`). The default is `small`. |
| 108 | + |
| 109 | +```jsx |
| 110 | +<TextArrayField source="genres" size="medium" /> |
| 111 | +``` |
| 112 | + |
| 113 | +## `source` |
| 114 | + |
| 115 | +The name of the record field containing the array to display. |
| 116 | + |
| 117 | +```jsx |
| 118 | +<TextArrayField source="genres" /> |
| 119 | +``` |
| 120 | + |
| 121 | +## `sx` |
| 122 | + |
| 123 | +Custom styles for the Stack, using MUI's `sx` prop. |
| 124 | + |
| 125 | +{% raw %} |
| 126 | +```jsx |
| 127 | +<TextArrayField source="genres" sx={{ gap: 2 }} /> |
| 128 | +``` |
| 129 | +{% endraw %} |
| 130 | + |
| 131 | +## `variant` |
| 132 | + |
| 133 | +The variant of the Chip components. Accepts any value supported by MUI's Chip (`filled`, `outlined`). The default is `filled`. |
| 134 | + |
| 135 | +```jsx |
| 136 | +<TextArrayField source="genres" variant="outlined" /> |
| 137 | +``` |
| 138 | + |
0 commit comments