Skip to content

CdsRadioGroup

A radio group is a choice of one option out of a short mutually exclusive set: a payment method, a plan, a report period. The group keeps the selected value, captions the set with a heading and a description, shows messages and errors; the options themselves are CdsRadio components with their own value.

Storybook GitLab Figma
Test coverage: 100% — good coverage
API in specs
59 / 59 props, events, slots and exposed members with a test
Test cases
60 unit · 0 browser · 2 planned
Known bugs
9 pinned by an it.fails test
Code coverage
100% lines · 100% branches measured in v4.0.0-alpha.13
A11y (axe)
4 recommendations in 11 stories measured in v4.0.0-alpha.11

For independent options that are turned on separately use CdsCheckbox, for an on/off setting that applies immediately — CdsToggle. If there are more than five options, fold the list into CdsSelect.

Anatomy ​

The heading and the description caption the whole set, below them are the radio buttons: each one has a control with a state icon, a label and an optional description. At the bottom is the common messages block of the group. A click on the label selects the button exactly like a click on the control.

Variations ​

Heading and description ​

The label prop names the group, description explains the choice. Every button has its own label and description: name the option briefly in the label and move the details — the price, the deadlines, the limits — into the description.

Direction ​

By default the buttons are stacked in a column. direction="horizontal" puts them in a row — handy for short sets of two or three options such as a period or a sort order.

States ​

disabled blocks the whole group and lowers the contrast, readonly forbids changing the choice but does not change the appearance. A single option is blocked by the disabled prop of the button itself.

Messages and errors ​

messages shows a supporting text under the group, error-messages — a ready error message (the one coming from the server, for example) together with the error state on all the buttons. Validation rules through rules are in the "Submitting a form" scenario. hide-details removes the bottom block entirely.

Colors ​

color sets the color of the selected button, base-color — of the unselected ones. The values apply to all the buttons of the group at once.

Slots ​

When the text props are not enough, the content of the blocks is replaced by slots — both on the group and on every button.

Slot nameDescription
labelContent of the group heading (replaces label)
descriptionContent of the group description (replaces description)
prependElement before the set of buttons
appendElement after the set of buttons
messagesContent of the messages block
detailsThe whole bottom block

CdsRadio has its own label and description slots — to highlight a part of the text or add an icon, for example.

Guidelines ​

Scenarios ​

Submitting a form ​

The choice is required: a rule in rules returns the error text while no value is selected. The button starts the check through the validate() of the CdsForm.

Choosing a plan ​

The descriptions of the buttons state the conditions, and the computed total under the group is updated on every choice — the user sees the consequences right away.

Sorting a list ​

A horizontal group with hide-details works as a sorting panel: the selected value rebuilds the list on the fly through a computed.

API ​

The props, events and slots of a single button are on the CdsRadio page.

Props

NameTypeDefaultDescription
activeColorstring | undefined—Color of the component in its active state
appendIconIconValue | undefined—Icon in the append slot, after the content
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
centerAffixboolean | undefinedtrueVertically center the appendInner, prependInner, clearIcon and label elements.
colorstring | undefined—Component color
defaultsTargetstring | undefined—Name of the child component the group prop values are passed to as defaults
descriptionstring | undefined—Description of the component, shown under the label
direction"horizontal" | "vertical" | undefined'vertical'Switches the component between horizontal and vertical layout.
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—Puts the component into the visual error state
errorMessagesstring | readonly string[] | null | undefined[]Puts the component into the error state and passes custom error messages. They are merged with any checks defined in the rules prop. This field does not trigger validation.
falseIconIconValue | undefined'radio-off'Icon of an unselected radio button
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
heightstring | number | undefined'auto'Height of the component
hideDetailsboolean | "auto" | undefined—Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages
hideMessagesboolean | undefined—Hide the messages under the input
hintstring | undefined—Hint text shown under the input
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Unique identifier of the component
inlineboolean | undefined—Renders the control as an inline element — the container does not stretch to the full width
labelstring | undefined—Label text of the component
maxErrorsstring | number | undefined1Maximum number of validation errors shown
maxWidthstring | number | undefined—Maximum component width
messagesstring | readonly string[] | undefined[]Messages shown under the input
minWidthstring | number | undefined—Minimum component width
modelValueany—Value of the component (v-model). In a group it is the value of the selected control, with multiple — an array of the selected values
namestring | undefined—The name attribute of the native input element. In a group one name joins all the nested controls
persistentHintboolean | undefined—Always show the hint, not only on focus
prependIconIconValue | undefined—Icon in the prepend slot, before the content
readonlyboolean | null | undefinednullRead-only state
rulesreadonly ValidationRule[] | undefined[]Accepts an array of items of different types: functions, booleans and strings. Functions take the input value as an argument and must return either true / false, or a string with an error message. The input goes into the error state if a function returns false (or any value in the array is false) or is a string.
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
trueIconIconValue | undefined'radio-on'Icon of a selected radio button
typestring | undefined'radio'Type of the native input element of the group buttons
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
valueComparatorValueComparator | undefinedtrueFunction comparing the model value with the value of the control. Deep comparison by default
widthstring | number | undefined—Component width

Events

NameTypeDescription
update:modelValue[value: any]Fires when the model of the component changes

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
defaultanyDefault slot — the radio buttons of the group (CdsRadio)
description{ description: string | undefined; props: Record<string, any>; }Default slot of the CdsDescription component
detailsCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component
messageCdsMessageSlotSlot for customizing the content of a message
messagesCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
prependCdsInputSlotAdds an element before the input

Exposed

NameTypeDescription
isDirtybooleanWhether the input is dirty
isDisabledbooleanWhether the input is disabled
isPurebooleanWhether the input is pure
isReadonlybooleanWhether the input is read-only
isValidboolean | nullWhether the input is valid
isValidatingbooleanWhether the input is being validated
reset() => Promise<void>Resets the input to its initial state
resetValidation() => Promise<void>Resets the validation of the input
validate(silent?: boolean) => Promise<string[]>Validates the input