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
Set the `choices` attribute to determine the options (with `id`, `name` tuples):
12
+
This input allows editing values that are arrays of scalar values, e.g. `[123, 456]`.
13
+
14
+
**Tip**: React-admin includes other components allowing the edition of such values:
15
+
16
+
-[`<SelectArrayInput>`](./SelectArrayInput.md) renders a dropdown list of choices
17
+
-[`<AutocompleteArrayInput>`](./AutocompleteArrayInput.md) renders an autocomplete input of choices
18
+
19
+
And if you are looking for a way to edit a list of embedded objects (e.g. `[{ id: 123, title: 'Hello' }, { id: 456, title: 'World' }]`), check the [`<ArrayInput>`](./ArrayInput.md) component.
20
+
21
+
## Usage
22
+
23
+
In addition to the `source`, `<CheckboxGroupInput>` requires one prop: the `choices` listing the possible values.
13
24
14
25
```jsx
15
26
import { CheckboxGroupInput } from'react-admin';
16
27
17
-
<CheckboxGroupInput source="category" choices={[
18
-
{ id:'programming', name:'Programming' },
19
-
{ id:'lifestyle', name:'Lifestyle' },
20
-
{ id:'photography', name:'Photography' },
28
+
<CheckboxGroupInput source="roles" choices={[
29
+
{ id:'admin', name:'Admin' },
30
+
{ id:'u001', name:'Editor' },
31
+
{ id:'u002', name:'Moderator' },
32
+
{ id:'u003', name:'Reviewer' },
21
33
]} />
22
34
```
23
35
24
-
## Properties
36
+
By default, the possible choices are built from the `choices` prop, using:
|`choices`| Required |`Object[]`| - | List of choices |
29
-
|`optionText`| Optional |`string`|`Function`|`name`| Field name of record to display in the suggestion item or function which accepts the correct record as argument (`record => {string}`) |
30
-
|`optionValue`| Optional |`string`|`id`| Field name of record containing the value to use as input value |
31
-
|`row`| Optional |`boolean`|`true`| Display group of elements in a compact row. |
32
-
|`labelPlacement`| Optional |`"bottom" `|`"end"`|`"start"`|`"top" `|`"end"`| The position of the checkbox label. |
40
+
The form value for the source must be an array of the selected values, e.g.
33
41
34
-
Refer to [MUI Checkbox documentation](https://mui.com/api/checkbox/) for more details.
|`choices`| Required |`Object[]`| - | List of choices |
55
+
|`labelPlacement`| Optional |`"bottom" `|`"end"`|`"start"`|`"top" `|`"end"`| The position of the checkbox label. |
56
+
|`options`| Optional |`Object`| - | Props to pass to the MUI `<CheckboxGroup>` component. |
57
+
|`optionText`| Optional |`string`|`Function`|`name`| Field name of record to display in the suggestion item or function which accepts the correct record as argument (`record => {string}`) |
58
+
|`optionValue`| Optional |`string`|`id`| Field name of record containing the value to use as input value |
59
+
|`row`| Optional |`boolean`|`true`| Display group of elements in a compact row. |
60
+
|`translateChoice`| Optional |`boolean`|`true`| Whether the choices should be translated |
35
61
36
62
`<CheckboxGroupInput>` also accepts the [common input props](./Inputs.md#common-input-props).
37
63
38
-
## Usage
64
+
## `choices`
65
+
66
+
The list of choices must be an array of objects - one object for each possible choice. In each object, `id` is the value, and the `name` is the label displayed to the user.
67
+
68
+
```jsx
69
+
<CheckboxGroupInput source="roles" choices={[
70
+
{ id:'admin', name:'Admin' },
71
+
{ id:'u001', name:'Editor' },
72
+
{ id:'u002', name:'Moderator' },
73
+
{ id:'u003', name:'Reviewer' },
74
+
]} />
75
+
```
76
+
77
+
You can also use an array of objects with different properties for the label and value, given you specify the `optionText` and `optionValue` props:
78
+
79
+
```jsx
80
+
<CheckboxGroupInput source="roles" choices={[
81
+
{ _id:'admin', label:'Admin' },
82
+
{ _id:'u001', label:'Editor' },
83
+
{ _id:'u002', label:'Moderator' },
84
+
{ _id:'u003', label:'Reviewer' },
85
+
]} optionValue="_id" optionText="label"/>
86
+
```
87
+
88
+
The choices are translated by default, so you can use translation identifiers as choices:
89
+
90
+
```jsx
91
+
constchoices= [
92
+
{ id:'admin', label:'myroot.roles.admin' },
93
+
{ id:'u001', label:'myroot.roles.u001' },
94
+
{ id:'u002', label:'myroot.roles.u002' },
95
+
{ id:'u003', label:'myroot.roles.u003' },
96
+
];
97
+
```
98
+
99
+
You can opt-out of this translation by setting [the `translateChoice` prop](#translatechoice) to `false`.
100
+
101
+
If you need to *fetch* the options from another resource, you're actually editing a one-to-many or a many-to-many relationship. In this case, wrap the `<CheckboxGroupInput>` in a [`<ReferenceArrayInput>`](./ReferenceArrayInput.md) or a [`<ReferenceManyToManyInput>`](./ReferenceManyToManyInput.md) component. You don't need to specify the `choices` prop - the parent component injects it based on the possible values of the related resource.
By default, this inputs renders a checkbox and a label for each choice, with the label on the right of the checkbox. You can change this behavior with the `labelPlacement` prop:
`optionText` is especially useful when the choices are records coming from a `<ReferenceArrayInput>` or a `<ReferenceManyToManyInput>`. By default, react-admin uses the [`recordRepresentation`](./Resource.md#recordrepresentation) function to display the record label. But if you set the `optionText` prop, react-admin will use it instead.
`optionText` also accepts a React Element, that will be cloned and receive the related choice as the `record` prop. You can use Field components there.
180
+
`optionText` also accepts a React Element, that will be rendered inside a [`<RecordContext>`](./useRecordContext.md) using the related choice as the `record` prop. You can use Field components there.
However, in some cases (e.g. inside a `<ReferenceInput>`), you may not want the choice to be translated. In that case, set the `translateChoice` prop to `false`.
Lastly, use the `options` attribute if you want to override any of MUI's `<Checkbox>` attributes:
212
+
By default, the checkboxes are displayed in a row. You can change that and let react-admin render one choice per row by setting the `row` prop to `false`:
@@ -107,3 +226,22 @@ The `<CheckboxGroupInput>` component accepts the usual `className` prop. You can
107
226
|`& .RaCheckboxGroupInput-label`| Applied to the underlying MUI's `FormLabel` component |
108
227
109
228
To override the style of all instances of `<CheckboxGroupInput>` using the [MUI style overrides](https://mui.com/customization/globals/#css), use the `RaCheckboxGroupInput` key.
229
+
230
+
## `translateChoice`
231
+
232
+
The choices are translated by default, so you can use translation identifiers as choices:
233
+
234
+
```jsx
235
+
constchoices= [
236
+
{ id:'admin', label:'myroot.roles.admin' },
237
+
{ id:'u001', label:'myroot.roles.u001' },
238
+
{ id:'u002', label:'myroot.roles.u002' },
239
+
{ id:'u003', label:'myroot.roles.u003' },
240
+
];
241
+
```
242
+
243
+
However, in some cases (e.g. inside a `<ReferenceArrayInput>`), you may not want the choice to be translated. In that case, set the `translateChoice` prop to `false`.
0 commit comments