|
| 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>Items:</div> |
| 50 | + <ArrayInputBase source="items"> |
| 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.md#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 | +| `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. | |
| 73 | + |
| 74 | +## Global validation |
| 75 | + |
| 76 | +If you are using an `<ArrayInputBase>` inside a form with global validation, you need to shape the errors object returned by the `validate` function like an array too. |
| 77 | + |
| 78 | +For instance, to display the following errors: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +You need to return an errors object shaped like this: |
| 83 | + |
| 84 | +```js |
| 85 | + { |
| 86 | + authors: [ |
| 87 | + {}, |
| 88 | + { |
| 89 | + name: 'A name is required', |
| 90 | + role: 'ra.validation.required' // translation keys are supported too |
| 91 | + }, |
| 92 | + ], |
| 93 | + } |
| 94 | +``` |
| 95 | + |
| 96 | +**Tip:** You can find a sample `validate` function that handles arrays in the [Form Validation documentation](./Validation.md#global-validation). |
| 97 | + |
| 98 | +## Disabling The Input |
| 99 | + |
| 100 | +`<ArrayInputBase>` does not support the `disabled` and `readOnly` props. |
| 101 | + |
| 102 | +If you need to disable the input, make sure the children are either `disabled` and `readOnly`: |
| 103 | + |
| 104 | +```jsx |
| 105 | +import { ArrayInputBase, EditBase, Form } from 'ra-core'; |
| 106 | +import { MyFormIterator } from './MyFormIterator'; |
| 107 | +import { DateInput } from './DateInput'; |
| 108 | +import { NumberInput } from './NumberInput'; |
| 109 | +import { TextInput } from './TextInput'; |
| 110 | + |
| 111 | +const OrderEdit = () => ( |
| 112 | + <EditBase> |
| 113 | + <Form> |
| 114 | + <TextInput source="customer" /> |
| 115 | + <DateInput source="date" /> |
| 116 | + <div> |
| 117 | + <div>Items:</div> |
| 118 | + <ArrayInputBase source="items"> |
| 119 | + <MyFormIterator inline disabled> |
| 120 | + <TextInput source="name" readOnly/> |
| 121 | + <NumberInput source="price" readOnly /> |
| 122 | + <NumberInput source="quantity" readOnly /> |
| 123 | + </MyFormIterator> |
| 124 | + </ArrayInputBase> |
| 125 | + </div> |
| 126 | + <button type="submit">Save</button> |
| 127 | + </Form> |
| 128 | + </EditBase> |
| 129 | +); |
| 130 | +``` |
| 131 | + |
| 132 | +## Changing An Item's Value Programmatically |
| 133 | + |
| 134 | +You can leverage `react-hook-form`'s [`setValue`](https://react-hook-form.com/docs/useform/setvalue) method to change an item's value programmatically. |
| 135 | + |
| 136 | +However you need to know the `name` under which the input was registered in the form, and this name is dynamically generated depending on the index of the item in the array. |
| 137 | + |
| 138 | +To get the name of the input for a given index, you can leverage the `SourceContext` created by react-admin, which can be accessed using the `useSourceContext` hook. |
| 139 | + |
| 140 | +This context provides a `getSource` function that returns the effective `source` for an input in the current context, which you can use as input name for `setValue`. |
| 141 | + |
| 142 | +Here is an example where we leverage `getSource` and `setValue` to change the role of an user to 'admin' when the 'Make Admin' button is clicked: |
| 143 | + |
| 144 | +```tsx |
| 145 | +import { ArrayInputBase, useSourceContext } from 'ra-core'; |
| 146 | +import { useFormContext } from 'react-hook-form'; |
| 147 | +import { MyFormIterator } from './MyFormIterator'; |
| 148 | + |
| 149 | +const MakeAdminButton = () => { |
| 150 | + const sourceContext = useSourceContext(); |
| 151 | + const { setValue } = useFormContext(); |
| 152 | + |
| 153 | + const onClick = () => { |
| 154 | + // sourceContext.getSource('role') will for instance return |
| 155 | + // 'users.0.role' |
| 156 | + setValue(sourceContext.getSource('role'), 'admin'); |
| 157 | + }; |
| 158 | + |
| 159 | + return ( |
| 160 | + <button onClick={onClick}> |
| 161 | + Make admin |
| 162 | + </button> |
| 163 | + ); |
| 164 | +}; |
| 165 | + |
| 166 | +const UserArray = () => ( |
| 167 | + <ArrayInputBase source="users"> |
| 168 | + <MyFormIterator inline> |
| 169 | + <TextInput source="name" helperText={false} /> |
| 170 | + <TextInput source="role" helperText={false} /> |
| 171 | + <MakeAdminButton /> |
| 172 | + </MyFormIterator> |
| 173 | + </ArrayInputBase> |
| 174 | +); |
| 175 | +``` |
| 176 | + |
| 177 | +**Tip:** If you only need the item's index, you can leverage the `useSimpleFormIteratorItem` hook instead. |
0 commit comments