To add the RadioGroup
component to your app:
1 2import { RadioGroup } from '@forge/react';
A radio group presents a list of options where only one choice can be selected.
Name | Type | Required | Available in macro config | Description |
---|---|---|---|---|
options | Array<Option> | Array<{ isDisabled: boolean; label: string; name: string; value: string; }> | Yes | Yes | An array of objects, each object is mapped onto a Radio element within the group. Name must be unique to the group. |
value | string | null | No | No | Once set, controls the selected value on the RadioGroup . |
defaultValue | string | null | No | Yes | Sets the initial selected value on the RadioGroup . |
isDisabled | boolean | No | No | Sets the disabled state of all Radio elements in the group. Overrides the isDisabled setting of all child Radio items. |
isRequired | boolean | No | Yes | Sets the required state of all Radio elements in the group. Should only be set when using within a Form component. |
isInvalid | boolean | No | No | Sets the invalid state of all Radio elements in the group. |
onInvalid | () => void | No | No | Function that gets fired after each invalid event. |
onChange | (e: ChangeEvent) => void | No | No | Function that gets fired after each change event. |
name | string | Required for macro configuration, not required for other extension points | Yes | Sets the name prop on each of the Radio elements in the group. |
The default form of a radio group.
1 2const options = [ { name: 'color', value: 'red', label: 'Red' }, { name: 'color', value: 'blue', label: 'Blue' }, { name: 'color', value: 'yellow', label: 'Yellow' }, { name: 'color', value: 'green', label: 'Green' }, { name: 'color', value: 'black', label: 'Black' }, ]; const RadioGroupDefaultExample = () => { return ( <RadioGroup options={options} value="red" /> ); };
isDisabled
can be used to disable the entire radio group.
1 2const options = [ { name: 'color', value: 'red', label: 'Red' }, { name: 'color', value: 'blue', label: 'Blue' }, { name: 'color', value: 'yellow', label: 'Yellow' }, { name: 'color', value: 'green', label: 'Green' }, { name: 'color', value: 'black', label: 'Black' }, ]; const RadioGroupDisabledExample = () => { return ( <RadioGroup options={options} isDisabled={true}/> ); };
For required fields, always add RequiredAsterisk
component next to the label. Use the ErrorMessage
or ValidationMessage
components for displaying a validation message.
1 2import { useForm, RadioGroup, Label, Form, FormSection, RequiredAsterisk } from '@forge/react'; const options = [ { name: 'color', value: 'red', label: 'Red' }, { name: 'color', value: 'blue', label: 'Blue' }, { name: 'color', value: 'green', label: 'Green' }, ]; const RadioGroupRequiredExample = () => { const { register, getFieldId, formState, handleSubmit } = useForm(); return ( <Form onSubmit={handleSubmit((values) => console.log(values))}> <FormSection> <Label labelFor={getFieldId("color")}> Pick a color <RequiredAsterisk /> </Label> <RadioGroup options={options} {...register("color", { required: true })} /> {formState.errors.color && ( <ErrorMessage>One of these options needs to be selected</ErrorMessage> )} </FormSection> <Button type="submit">Submit</Button> </Form> ); };
When using the RadioGroup
component, we recommend keeping the following accessibility considerations in mind:
Rate this page: