Radio Group
A radio group let the user make a mutually exclusive selection from a group of options.
Name | Type | Description | Default |
---|---|---|---|
defaultValue | string | Initial value of the radio group, only when it is uncontrolled. | - |
value | string | Value of the radio group. If undefined, the component will be uncontrolled and the value will be managed internally by the component. | - |
label | string | Text to be placed above the radio group. | - |
name | string | Name attribute of the input element. This attribute will allow users to find the component's value during the submit event. | - |
helperText | string | Helper text to be placed above the radio group. | - |
Required | { value: string; label: string; disabled?: boolean; }[] | An array of objects representing the selectable options. Each object Option has the following properties:
| - |
disabled | boolean | If true, the component will be disabled. | false |
optional | boolean | If true, the radio group will be optional, showing the text '(Optional)' next to the label and adding a default last option with an empty string as value. Otherwise, the field will be considered required and an error will be passed as a parameter to the onBlur function if an option wasn't selected. | false |
optionalItemLabel | string | Label of the optional radio input. | 'N/A' |
readOnly | boolean | If true, the component will not be mutable, meaning the user can not edit the control. | false |
stacking | 'row' | 'column' | Sets the orientation of the options within the radio group. | 'column' |
onChange | (value: string) => void | This function will be called when the user chooses an option. The new value will be passed to this function. | - |
onBlur | (val: { value?: string; error?: string }) => void | This function will be called when the radio group loses the focus. An object including the value and the error will be passed to this function. If there is no error, error will not be defined. | - |
error | string | If it is a defined value and also a truthy string, the component will change its appearance, showing the error below the radio group. If the defined value is an empty string, it will reserve a space below the component for a future error, but it would not change its look. In case of being undefined or null, both the appearance and the space for the error message would not be modified. | - |
tabIndex | number | Value of the tabindex attribute. | 0 |
ref | React.Ref<HTMLDivElement> | Reference to the component. | - |
For handling errors, we suggest initializing the error
prop with an empty string. This will reserve space for a possible future error message and prevent unintended layout changes. Also, the onBlur
event will send undefined
when there is no error, so you may also need to control this too to avoid the same problem.