You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ArrayInput.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -133,3 +133,54 @@ const OrderEdit = () => (
133
133
</Edit>
134
134
);
135
135
```
136
+
137
+
## Changing An Item's Value Programmatically
138
+
139
+
You can leverage `react-hook-form`'s [`setValue`](https://react-hook-form.com/docs/useform/setvalue) method to change an item's value programmatically.
140
+
141
+
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.
142
+
143
+
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.
144
+
145
+
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`.
146
+
147
+
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:
**Tip:** If you only need the item's index, you can leverage the [`useSimpleFormIteratorItem` hook](./SimpleFormIterator.md#getting-the-element-index) instead.
Copy file name to clipboardExpand all lines: docs/Inputs.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -647,7 +647,7 @@ const OrderEdit = () => (
647
647
);
648
648
```
649
649
650
-
**Tip**: When used inside an `ArrayInput`, `<FormDataConsumer>` provides one additional property to its child function called `scopedFormData`. It's an object containing the current values of the *currently rendered item*. This allows you to create dependencies between inputs inside a `<SimpleFormIterator>`, as in the following example:
650
+
**Tip**: When used inside an `<ArrayInput>`, `<FormDataConsumer>` provides one additional property to its child function called `scopedFormData`. It's an object containing the current values of the *currently rendered item*. This allows you to create dependencies between inputs inside a `<SimpleFormIterator>`, as in the following example:
651
651
652
652
```tsx
653
653
import { FormDataConsumer } from'react-admin';
@@ -682,6 +682,8 @@ const PostEdit = () => (
682
682
683
683
**Tip:** TypeScript users will notice that `scopedFormData` is typed as an optional parameter. This is because the `<FormDataConsumer>` component can be used outside of an `<ArrayInput>` and in that case, this parameter will be `undefined`. If you are inside an `<ArrayInput>`, you can safely assume that this parameter will be defined.
684
684
685
+
**Tip:** If you need to access the *effective* source of an input inside an `<ArrayInput>`, for example to change the value programmatically using `setValue`, you will need to leverage the [`useSourceContext` hook](./ArrayInput#changing-an-items-value-programmatically).
686
+
685
687
## Hiding Inputs Based On Other Inputs
686
688
687
689
You may want to display or hide inputs based on the value of another input - for instance, show an `email` input only if the `hasEmail` boolean input has been ticked to `true`.
Copy file name to clipboardExpand all lines: docs/ReferenceManyInput.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -300,3 +300,63 @@ const ProductEdit = () => (
300
300
-`<ReferenceManyInput>` cannot be used with `undoable` mutations in a `<Create>` view.
301
301
-`<ReferenceManyInput>` cannot have a `<ReferenceOneInput>` or a `<ReferenceManyToManyInput>` as one of its children.
302
302
-`<ReferenceManyInput>` does not support server side validation.
303
+
304
+
## Changing An Item's Value Programmatically
305
+
306
+
You can leverage `react-hook-form`'s [`setValue`](https://react-hook-form.com/docs/useform/setvalue) method to change an item's value programmatically.
307
+
308
+
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.
309
+
310
+
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.
311
+
312
+
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`.
313
+
314
+
Here is an example where we leverage `getSource` and `setValue` to prefill the email input when the 'Prefill email' button is clicked:
**Tip:** If you only need the item's index, you can leverage the [`useSimpleFormIteratorItem` hook](./SimpleFormIterator.md#getting-the-element-index) instead.
Copy file name to clipboardExpand all lines: docs/ReferenceOneInput.md
+57-45Lines changed: 57 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -234,58 +234,70 @@ Name of the field carrying the relationship on the referenced resource. For inst
234
234
</ReferenceOneInput>
235
235
```
236
236
237
-
## Customizing The Child Inputs
237
+
## Limitations
238
+
239
+
-`<ReferenceOneInput>` cannot be used inside an `<ArrayInput>` or a `<ReferenceManyInput>`.
240
+
-`<ReferenceOneInput>` cannot have a `<ReferenceManyInput>` or a `<ReferenceManyToManyInput>` as one of its children.
241
+
-`<ReferenceOneInput>` does not support server side validation.
242
+
243
+
## Changing An Item's Value Programmatically
244
+
245
+
You can leverage `react-hook-form`'s [`setValue`](https://react-hook-form.com/docs/useform/setvalue) method to change the reference record's value programmatically.
238
246
239
-
`<ReferenceOneInput>` works by cloning its children and overriding their `source` prop, to add a temporary field name prefix. This means that, if you need to nest your inputs inside another component, you will need to propagate the `source` prop to them.
247
+
However you need to know the `name` under which the inputs were registered in the form, and these names are dynamically generated by `<ReferenceOneInput>`.
240
248
241
-
In this example, the `<TextInput>` component is wrapped inside a `<MyCustomInput>` component. That adds an icon and additional styling.
249
+
To get the name of a specific input, you can leverage the `SourceContext` created by react-admin, which can be accessed using the `useSourceContext` hook.
250
+
251
+
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`.
252
+
253
+
Here is an example where we leverage `getSource` and `setValue` to update some of the book details when the 'Update book details' button is clicked:
**Tip:** This hook also returns the total number of elements (`total`).
472
+
473
+
**Tip:** If you need the index to change the value of an input programmatically, you should use the [`useSourceContext` hook](./ArrayInput.md#changing-an-items-value-programmatically) instead.
You can leverage `react-hook-form`'s [`setValue`](https://react-hook-form.com/docs/useform/setvalue) method to change an input's value programmatically.
205
+
206
+
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 locale.
207
+
208
+
To get the name of the input for a given locale, you can leverage the `SourceContext` created by react-admin, which can be accessed using the `useSourceContext` hook.
209
+
210
+
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`.
211
+
212
+
Here is an example where we leverage `getSource` and `setValue` to pre-fill the 'description' input using the value of the 'title' input when the corresponding button is clicked:
0 commit comments