|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "<ArrayInputBase>" |
| 4 | +--- |
| 5 | + |
| 6 | +`<ArrayInputBase>` allows editing of embedded arrays, like the `items` field in the following `order` record: |
| 7 | + |
| 8 | +```json |
| 9 | +{ |
| 10 | + "id": 1, |
| 11 | + "date": "2022-08-30", |
| 12 | + "customer": "John Doe", |
| 13 | + "items": [ |
| 14 | + { |
| 15 | + "name": "Office Jeans", |
| 16 | + "price": 45.99, |
| 17 | + "quantity": 1, |
| 18 | + }, |
| 19 | + { |
| 20 | + "name": "Black Elegance Jeans", |
| 21 | + "price": 69.99, |
| 22 | + "quantity": 2, |
| 23 | + }, |
| 24 | + { |
| 25 | + "name": "Slim Fit Jeans", |
| 26 | + "price": 55.99, |
| 27 | + "quantity": 1, |
| 28 | + }, |
| 29 | + ], |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +`<ArrayInputBase>` expects a single child, which must be a *form iterator* component. A form iterator is a component rendering a field array (the object returned by react-hook-form's [`useFieldArray`](https://react-hook-form.com/docs/usefieldarray)). You can build such component using [the `<SimpleFormIteratorBase>`](./SimpleFormIteratorBase.md). |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { ArrayInputBase, EditBase, Form } from 'ra-core'; |
| 39 | +import { MyFormIterator } from './MyFormIterator'; |
| 40 | +import { DateInput } from './DateInput'; |
| 41 | +import { NumberInput } from './NumberInput'; |
| 42 | +import { TextInput } from './TextInput'; |
| 43 | + |
| 44 | +export const OrderEdit = () => ( |
| 45 | + <EditBase> |
| 46 | + <Form> |
| 47 | + <DateInput source="date" /> |
| 48 | + <div> |
| 49 | + <div>Tags:</div> |
| 50 | + <ArrayInputBase source="tags"> |
| 51 | + <MyFormIterator> |
| 52 | + <TextInput source="name" /> |
| 53 | + <NumberInput source="price" /> |
| 54 | + <NumberInput source="quantity" /> |
| 55 | + </MyFormIterator> |
| 56 | + </ArrayInputBase> |
| 57 | + </div> |
| 58 | + <button type="submit">Save</button> |
| 59 | + </Form> |
| 60 | + </EditBase> |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | +**Note**: Setting [`shouldUnregister`](https://react-hook-form.com/docs/useform#shouldUnregister) on a form should be avoided when using `<ArrayInputBase>` (which internally uses `useFieldArray`) as the unregister function gets called after input unmount/remount and reorder. This limitation is mentioned in the react-hook-form [documentation](https://react-hook-form.com/docs/usecontroller#props). If you are in such a situation, you can use the [`transform`](./EditBase.html#transform) prop to manually clean the submitted values. |
| 65 | + |
| 66 | +## Props |
| 67 | + |
| 68 | +| Prop | Required | Type | Default | Description | |
| 69 | +|-----------------| -------- |---------------------------| ------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 70 | +| `source` | Required | `string` | - | Name of the entity property to use for the input value | |
| 71 | +| `defaultValue` | Optional | `any` | - | Default value of the input. | |
| 72 | +| `readOnly` | Optional | `boolean` | `false` | If true, the input is in read-only mode. | |
| 73 | +| `disabled` | Optional | `boolean` | `false` | If true, the input is disabled. | |
| 74 | +| `validate` | Optional | `Function` | `array` | - | Validation rules for the current property. See the [Validation Documentation](./Validation.md#per-input-validation-built-in-field-validators) for details. | |
0 commit comments