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/AutocompleteArrayInput.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -436,7 +436,7 @@ In that case, set the `translateChoice` prop to `false`.
436
436
437
437
**Tip**: To use the `disableCloseOnSelect` prop, you must also set `blurOnSelect={false}`, since this is enabled by default.
438
438
439
-
## Using In A ReferenceArrayInput
439
+
## Fetching Choices
440
440
441
441
If you want to populate the `choices` attribute with a list of related records, you should decorate `<AutocompleteArrayInput>` with [`<ReferenceArrayInput>`](./ReferenceArrayInput.md), and leave the `choices` empty:
If you want to populate the `choices` attribute with a list of related records, you should decorate `<CheckboxGroupInput>` with [`<ReferenceArrayInput>`](./ReferenceArrayInput.md), and leave the `choices` empty:
|`choices`| Optional |`Object[]`| - | List of items to show as options. Required unless inside a ReferenceInput. |
53
+
|`choices`| Optional |`Object[]`| - | List of items to show as options. Required unless inside a ReferenceInput. |
54
+
|`isLoading`| Optional |`boolean`|`false`| If `true`, the component will display a loading indicator. |
54
55
|`options`| Optional |`Object`| - | Props to pass to the underlying `<RadioButtonGroup>` element |
55
56
|`optionText`| Optional |`string`|`Function`|`name`| Field name of record to display in the suggestion item or function which accepts the current record as argument (`record => {string}`) |
56
57
|`optionValue`| Optional |`string`|`id`| Field name of record containing the value to use as input value |
@@ -108,7 +109,7 @@ If you need to *fetch* the options from another resource, you're actually editin
108
109
</ReferenceInput>
109
110
```
110
111
111
-
See [Using in a `<ReferenceInput>`](#using-in-a-referenceinput) below for more information.
112
+
See [Selecting a foreign key](#selecting-a-foreign-key) below for more information.
112
113
113
114
If you have an *array of values* for the options, turn it into an array of objects with the `id` and `name` properties:
When [fetching choices from a remote API](#fetching-choices), the `<RadioButtonGroupInput>` can't be used until the choices are fetched. To let the user know, you can pass the `isLoading` prop to `<RadioButtonGroupInput>`. This displays a loading indicator while the choices are being fetched.
See [Using in a `<ReferenceInput>`](#using-in-a-referenceinput) below for more details.
184
+
See [fetching choices](#fetching-choices) below for more details.
162
185
163
186
`optionText` also accepts a function, so you can shape the option text at will:
164
187
@@ -247,24 +270,110 @@ However, in some cases, you may not want the choice to be translated. In that ca
247
270
248
271
Note that `translateChoice` is set to `false` when `<RadioButtonGroupInput>` is a child of `<ReferenceInput>`.
249
272
250
-
## Using In A ReferenceInput
273
+
## Fetching Choices
251
274
252
-
If you want to populate the `choices` attribute with a list of related records, you should decorate `<RadioButtonGroupInput>` with [`<ReferenceInput>`](./ReferenceInput.md), and leave the `choices` empty:
275
+
You can use [`useGetList`](./useGetList.md) to fetch choices. For example, to fetch a list of countries for a user profile:
The `isLoading` prop is used to display a loading indicator while the data is being fetched.
296
+
297
+
However, most of the time, if you need to populate a `<RadioButtonGroupInput>` with choices fetched from another resource, it's because you are trying to set a foreign key. In that case, you should use [`<ReferenceInput>`](./ReferenceInput.md) to fetch the choices instead (see next section).
298
+
299
+
## Selecting a Foreign Key
300
+
301
+
If you use `<RadioButtonGroupInput>` to set a foreign key for a many-to-one or a one-to-one relationship, you'll have to [fetch choices](#fetching-choices), as explained in the previous section. You'll also have to fetch the record corresponding to the current value of the foreign key, as it may not be in the list of choices.
302
+
303
+
For example, if a `contact` has one `company` via the `company_id` foreign key, a contact form can let users select a company as follows:
// if the current company is not in the list of possible companies, add it
318
+
constchoicesWithCurrentCompany= choices
319
+
?choices.find(choice=>choice.id=== companyId)
320
+
? choices
321
+
: [...choices, currentCompany]
322
+
: [];
323
+
return (
324
+
<RadioButtonGroupInput
325
+
label="Company"
326
+
source="company_id"
327
+
choices={choicesWithCurrentCompany}
328
+
optionText="name"
329
+
disabled={isLoading}
330
+
/>
331
+
);
332
+
}
260
333
```
261
334
262
-
In that case, `<RadioButtonGroupInput>` uses the [`recordRepresentation`](./Resource.md#recordrepresentation) to render each choice from the list of possible records. You can override this behavior by setting the `optionText` prop:
335
+
As this is a common task, react-admin provides a shortcut to do the same in a declarative way: [`<ReferenceInput>`](./ReferenceInput.md):
- fetches a list of records with `dataProvider.getList()` and `dataProvider.getOne()`, using the `reference` prop for the resource,
354
+
- puts the result of the fetch in the `ChoiceContext` as the `choices` prop, as well as the `isLoading` state,
355
+
- and renders its child component
356
+
357
+
When rendered as a child of `<ReferenceInput>`, `<RadioButtonGroupInput>` reads that `ChoiceContext` to populate its own `choices` and `isLoading` props.
358
+
359
+
In fact, you can simplify the code even further:
360
+
361
+
-`<ReferenceInput>` puts all its props inside the `ChoiceContext`, including `source`, so `<RadioButtonGroupInput>` doesn't need to repeat it.
362
+
- You can also put the `label` prop on the `<ReferenceInput>` rather than `<RadioButtonGroupInput>` so that it looks just like [`<ReferenceField>`](./ReferenceField.md) (for easier memorization).
363
+
-`<RadioButtonGroupInput>` uses the [`recordRepresentation`](./Resource.md#recordrepresentation) to determine how to represent the related choices. In the example above, the `companies` resource uses `name` as its `recordRepresentation`, so `<RadioButtonGroupInput>` will default to `optionText="name"`.
364
+
365
+
The code for the `<CompanyInput>` component can be reduced to:
This is the recommended approach for using `<RadioButtonGroupInput>` to select a foreign key. This not only signifies that the input is a `<RadioButtonGroupInput>` but also highlights its function in fetching choices from another resource, ultimately enhancing the code's readability.
378
+
379
+
**Tip**: `<ReferenceInput>` is much more powerful than the initial snippet. It optimizes and caches API calls, enables refetching of both API calls with a single command, and stores supplementary data in the `<ChoicesContext>`. `<ReferenceInput>` can provide choices to `<RadioButtonGroupInput>`, but also to [`<AutocompleteInput>`](./AutocompleteInput.md) and [`<SelectInput>`](./SelectInput.md). For further information, refer to [the `<ReferenceInput>` documentation](./ReferenceInput.md).
If you want to populate the `choices` attribute with a list of related records, you should decorate `<SelectArrayInput>` with [`<ReferenceArrayInput>`](./ReferenceArrayInput.md), and leave the `choices` empty:
0 commit comments