Skip to content

Commit 585432c

Browse files
authored
Merge pull request #10872 from marmelab/raoss-next-10871
feat: Support label configuration in <SelectArrayInput>
2 parents 8b43a9a + 96a175d commit 585432c

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

docs/SelectArrayInput.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,18 @@ The form value for the source must be an array of the selected values, e.g.
6161

6262
## Props
6363

64-
| Prop | Required | Type | Default | Description |
65-
|-------------------|----------|----------------------------|--------------------|----------------------------------------------------------------------------------------------------------------------------------------|
66-
| `choices` | Optional | `Object[]` | - | List of items to show as options. Required unless inside a ReferenceArray Input. |
67-
| `create` | Optional | `Element` | - | A React Element to render when users want to create a new choice |
68-
| `createLabel` | Optional | `string` | `ra.action. create` | The label for the menu item allowing users to create a new choice. Used when the filter is empty |
69-
| `disableValue` | Optional | `string` | 'disabled' | The custom field name used in `choices` to disable some choices |
70-
| `onCreate` | Optional | `Function` | - | A function called with the current filter value when users choose to create a new choice. |
71-
| `options` | Optional | `Object` | - | Props to pass to the underlying `<SelectInput>` element |
72-
| `optionText` | Optional | `string` &#124; `Function` | `name` | Field name of record to display in the suggestion item or function which accepts the current record as argument (`record => {string}`) |
73-
| `optionValue` | Optional | `string` | `id` | Field name of record containing the value to use as input value |
74-
| `translateChoice` | Optional | `boolean` | `true` | Whether the choices should be translated |
64+
| Prop | Required | Type | Default | Description |
65+
|-------------------|----------|----------------------------|--------------- -----|----------------------------------------------------------------------------------------------------------------------------------------|
66+
| `choices` | Optional | `Object[]` | - | List of items to show as options. Required unless inside a ReferenceArray Input. |
67+
| `create` | Optional | `Element` | - | A React Element to render when users want to create a new choice |
68+
| `createLabel` | Optional | `string` | `ra.action. create` | The label for the menu item allowing users to create a new choice. Used when the filter is empty |
69+
| `disableValue` | Optional | `string` | 'disabled' | The custom field name used in `choices` to disable some choices |
70+
| `InputLabelProps` | Optional | `Object` | - | Props to pass to the underlying `<InputLabel>` element |
71+
| `onCreate` | Optional | `Function` | - | A function called with the current filter value when users choose to create a new choice. |
72+
| `options` | Optional | `Object` | - | Props to pass to the underlying `<SelectInput>` element |
73+
| `optionText` | Optional | `string` &#124; `Function` | `name` | Field name of record to display in the suggestion item or function which accepts the current record as argument (`record => {string}`) |
74+
| `optionValue` | Optional | `string` | `id` | Field name of record containing the value to use as input value |
75+
| `translateChoice` | Optional | `boolean` | `true` | Whether the choices should be translated |
7576

7677
`<SelectArrayInput>` also accepts the [common input props](./Inputs.md#common-input-props).
7778

@@ -265,6 +266,16 @@ const choices = [
265266
<SelectArrayInput source="roles" choices={choices} disableValue="not_available" />
266267
```
267268

269+
## `InputLabelProps`
270+
271+
Use the `options` attribute if you want to override Material UI's `<InputLabel>` attributes:
272+
273+
{% raw %}
274+
```jsx
275+
<SelectArrayInput source="category_ids" choices={choices} InputLabelProps={{ shrink: true }} />
276+
```
277+
{% endraw %}
278+
268279
## `onCreate`
269280

270281
Use the `onCreate` prop to allow users to create new options on-the-fly. Its value must be a function. This lets you render a `prompt` to ask users about the new value. You can return either the new choice directly or a Promise resolving to the new choice.

packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ export const DisabledChoice = () => (
181181
</Wrapper>
182182
);
183183

184+
export const InputLabelProps = () => (
185+
<Wrapper>
186+
<SelectArrayInput
187+
source="roles"
188+
choices={[
189+
{ id: 'admin', name: 'Admin' },
190+
{ id: 'u001', name: 'Editor' },
191+
{ id: 'u002', name: 'Moderator' },
192+
{ id: 'u003', name: 'Reviewer' },
193+
]}
194+
InputLabelProps={{
195+
shrink: true,
196+
required: true,
197+
}}
198+
/>
199+
</Wrapper>
200+
);
201+
184202
export const ReadOnly = () => (
185203
<AdminContext i18nProvider={i18nProvider}>
186204
<Create

packages/ra-ui-materialui/src/input/SelectArrayInput.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type FormControlProps,
1717
Chip,
1818
OutlinedInput,
19+
InputLabelProps,
1920
} from '@mui/material';
2021
import {
2122
type ChoicesProps,
@@ -106,6 +107,7 @@ export const SelectArrayInput = (inProps: SelectArrayInputProps) => {
106107
format,
107108
helperText,
108109
label,
110+
InputLabelProps,
109111
isFetching: isFetchingProp,
110112
isLoading: isLoadingProp,
111113
isPending: isPendingProp,
@@ -308,6 +310,7 @@ export const SelectArrayInput = (inProps: SelectArrayInputProps) => {
308310
ref={inputLabel}
309311
id={`${id}-outlined-label`}
310312
htmlFor={id}
313+
{...InputLabelProps}
311314
>
312315
<FieldTitle
313316
label={label}
@@ -380,6 +383,7 @@ export type SelectArrayInputProps = ChoicesProps &
380383
Omit<CommonInputProps, 'source'> &
381384
Omit<FormControlProps, 'defaultValue' | 'onBlur' | 'onChange'> & {
382385
options?: SelectProps;
386+
InputLabelProps?: Omit<InputLabelProps, 'htmlFor' | 'id' | 'ref'>;
383387
source?: string;
384388
onChange?: (event: ChangeEvent<HTMLInputElement> | RaRecord) => void;
385389
};

0 commit comments

Comments
 (0)