Skip to content

Commit d2cd51e

Browse files
committed
Add disableValue prop
1 parent 0baee4d commit d2cd51e

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/RadioButtonGroupInput.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The form value for the source must be the selected value, e.g.
6262
| `optionValue` | Optional | `string` | `id` | Field name of record containing the value to use as input value |
6363
| `row` | Optional | `boolean` | `true` | Display options in a compact row. |
6464
| `translateChoice` | Optional | `boolean` | `true` | Whether the choices should be translated |
65+
| `disableValue` | Optional | `string` | `disabled` | The custom field name used in `choices` to disable some choices |
6566

6667
`<RadioButtonGroupInput>` also accepts the [common input props](./Inputs.md#common-input-props).
6768

@@ -293,6 +294,30 @@ However, in some cases, you may not want the choice to be translated. In that ca
293294

294295
Note that `translateChoice` is set to `false` when `<RadioButtonGroupInput>` is a child of `<ReferenceInput>`.
295296

297+
## `disableValue`
298+
299+
By default, `<RadioButtonGroupInput>` renders the choices with the field `disabled: true` as disabled.
300+
301+
```jsx
302+
const choices = [
303+
{ id: 'tech', name: 'Tech' },
304+
{ id: 'lifestyle', name: 'Lifestyle' },
305+
{ id: 'people', name: 'People', disabled: true },
306+
];
307+
<RadioButtonGroupInput source="category" choices={choices} />
308+
```
309+
310+
If you want to use another field to denote disabled options, set the `disableValue` prop.
311+
312+
```jsx
313+
const choices = [
314+
{ id: 'tech', name: 'Tech' },
315+
{ id: 'lifestyle', name: 'Lifestyle' },
316+
{ id: 'people', name: 'People', not_available: true },
317+
];
318+
<RadioButtonGroupInput source="category" choices={choices} disableValue="not_available" />
319+
```
320+
296321
## Fetching Choices
297322

298323
You can use [`useGetList`](./useGetList.md) to fetch choices. For example, to fetch a list of countries for a user profile:

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ import { LinearProgress } from '../layout';
8888
* <RadioButtonGroupInput source="gender" choices={choices} translateChoice={false}/>
8989
*
9090
* The object passed as `options` props is passed to the Material UI <RadioButtonGroup> component
91+
*
92+
* You can disable some choices by providing a `disableValue` field which name is `disabled` by default
93+
* @example
94+
* const choices = [
95+
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
96+
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
97+
* { id: 976, first_name: 'William', last_name: 'Rinkerd', disabled: true },
98+
* ];
99+
*
100+
* @example
101+
* const choices = [
102+
* { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
103+
* { id: 456, first_name: 'Jane', last_name: 'Austen' },
104+
* { id: 976, first_name: 'William', last_name: 'Rinkerd', not_available: true },
105+
* ];
106+
* <RadioButtonGroupInput source="gender" choices={choices} disableValue="not_available" />
107+
*
91108
*/
92109
export const RadioButtonGroupInput = (inProps: RadioButtonGroupInputProps) => {
93110
const props = useThemeProps({
@@ -115,6 +132,7 @@ export const RadioButtonGroupInput = (inProps: RadioButtonGroupInputProps) => {
115132
source: sourceProp,
116133
translateChoice,
117134
validate,
135+
disableValue = 'disabled',
118136
disabled,
119137
readOnly,
120138
...rest
@@ -222,6 +240,7 @@ export const RadioButtonGroupInput = (inProps: RadioButtonGroupInputProps) => {
222240
optionValue={optionValue}
223241
source={id}
224242
translateChoice={translateChoice ?? !isFromReference}
243+
disableValue={disableValue}
225244
/>
226245
))}
227246
</RadioGroup>
@@ -277,6 +296,7 @@ export type RadioButtonGroupInputProps = Omit<CommonInputProps, 'source'> &
277296
RadioGroupProps & {
278297
options?: RadioGroupProps;
279298
source?: string;
299+
disableValue?: string;
280300
};
281301

282302
const PREFIX = 'RaRadioButtonGroupInput';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const RadioButtonGroupInputItem = (
1919
optionValue,
2020
source,
2121
translateChoice,
22+
disableValue = 'disabled',
2223
...rest
2324
} = useThemeProps({
2425
props: props,
@@ -29,6 +30,7 @@ export const RadioButtonGroupInputItem = (
2930
optionText,
3031
optionValue,
3132
translateChoice,
33+
disableValue,
3234
});
3335
const label = getChoiceText(choice);
3436
const value = getChoiceValue(choice);
@@ -55,6 +57,7 @@ export interface RadioButtonGroupInputItemProps
5557
Pick<ChoicesProps, 'optionValue' | 'optionText' | 'translateChoice'> {
5658
choice: any;
5759
source: any;
60+
disableValue?: string;
5861
}
5962

6063
const PREFIX = 'RaRadioButtonGroupInputItem';

0 commit comments

Comments
 (0)