| Name | Type | Description | Default |
|---|---|---|---|
| ariaLabel | string | Specifies a string to be used as the name for the radio group when no `label` is provided. | 'Radio group' |
| defaultValue | string | Initial value of the radio group, only when it is uncontrolled. | - |
| disabled | boolean | If true, the component will be disabled. | false |
| 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. | - |
| helperText | string | Helper text to be placed above the radio group. | - |
| 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. | - |
| 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. | - |
| onChange | (value: string) => void | This function will be called when the user chooses an option. The new value will be passed to this function. | - |
| 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' |
Required options | { disabled?: boolean; label: string; value: string; }[] | An array of objects representing the selectable options. Each object Option has the following properties:
| - |
| readOnly | boolean | If true, the component will not be mutable, meaning the user can not edit the control. | false |
| ref | React.Ref<HTMLDivElement> | Reference to the component. | - |
| stacking | 'row' | 'column' | Sets the orientation of the options within the radio group. | 'column' |
| tabIndex | number | Value of the tabindex attribute. | 0 |
| value | string | Value of the radio group. If undefined, the component will be uncontrolled and the value will be managed internally by the component. | - |
() => { const [value, setValue] = useState(""); const onChange = (value) => { setValue(value); }; const onBlur = ({ value }) => { setValue(value); }; const options = [ { label: "Female", value: "female" }, { label: "Male", value: "male" }, { label: "Non-binary", value: "non-binary" }, { label: "Other", value: "other" }, ]; return ( <DxcInset space="var(--spacing-padding-xl)"> <DxcRadioGroup label="Gender" options={options} value={value} onChange={onChange} onBlur={onBlur} /> </DxcInset> ); }
() => { const radioGroupRef = useRef(); const handleSubmit = () => { const radioGroup = radioGroupRef.current.getElementsByTagName("input")[0]; console.log(radioGroup.value); }; const options = [ { label: "Orange", value: "orange" }, { label: "Apple", value: "apple" }, { label: "Pear", value: "pear" }, ]; return ( <DxcInset space="var(--spacing-padding-xl)"> <DxcFlex direction="column" gap="var(--spacing-gap-xl)"> <DxcRadioGroup label="Fruit" defaultValue="apple" options={options} ref={radioGroupRef} /> <DxcButton label="Submit" type="submit" onClick={handleSubmit} /> </DxcFlex> </DxcInset> ); }
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.
() => { const [error, setError] = useState(""); const onBlur = ({ error }) => { setError(error); }; const options = [ { label: "Pasta", value: "pasta" }, { label: "Fish", value: "fish" }, { label: "Meat", value: "Meat" }, ]; return ( <DxcInset space="var(--spacing-padding-xl)"> <DxcRadioGroup label="Food" options={options} onBlur={onBlur} error={error == null ? "" : error} /> </DxcInset> ); }