DXC Logo

Radio group

A radio group allows users to select one option from a set of related, mutually exclusive choices.

Props

NameTypeDescriptionDefault
ariaLabelstringSpecifies a string to be used as the name for the radio group when no `label` is provided.'Radio group'
defaultValuestringInitial value of the radio group, only when it is uncontrolled.-
disabledbooleanIf true, the component will be disabled.false
errorstringIf 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.-
helperTextstringHelper text to be placed above the radio group.-
labelstringText to be placed above the radio group.-
namestringName 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 }) => voidThis 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) => voidThis function will be called when the user chooses an option. The new value will be passed to this function.-
optionalbooleanIf 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
optionalItemLabelstringLabel 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:
  • label: Label of the option placed next to the radio input.
  • value: Value of the option. It should be unique and not an empty string, which is reserved to the optional item added by the optional prop.
  • disabled: disables the option.
-
readOnlybooleanIf true, the component will not be mutable, meaning the user can not edit the control.false
refReact.Ref<HTMLDivElement>Reference to the component.-
stacking'row' | 'column'Sets the orientation of the options within the radio group.'column'
tabIndexnumberValue of the tabindex attribute.0
valuestringValue of the radio group. If undefined, the component will be uncontrolled and the value will be managed internally by the component.-

Examples

Controlled

() => {
  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>
  );
}

Uncontrolled

() => {
  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>
  );
}

Error handling

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>
  );
}