Skip to content

CdsSelectionControl

New in v4.0

The base of the selection controls: a native input with state icons, a label and a description. The component holds the value, can work with its own true/false values and with multiple selection, and picks up the settings of CdsSelectionControlGroup. The checkboxes, radio buttons and toggles of the design system are built on it.

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

For regular selection take the ready-made controls — CdsCheckbox, CdsRadio, CdsToggle: they already have the icons, the sizes, the state colors and the block of messages. CdsSelectionControl is needed when the control you want is not in the library.

Anatomy ​

On the left is the control: the state icon from true-icon / false-icon and an invisible native input on top of it. On the right are the label (CdsLabel) and the description (CdsDescription); a click on the label toggles the control.

The component has no sizes of its own: the area of the control is set by the --cds-selection-control-width and --cds-selection-control-height CSS variables, and the gap up to the label is the free space inside that area. In the examples below the distance is added with the gap-x-2 class.

Variations ​

Type and icons ​

The type prop sets the type of the native input — the behavior and the accessibility depend on it: a checkbox is turned on and off, a radio is only selected. The state icons come from true-icon and false-icon; without them the control has no visible part.

Label and description ​

The label prop names the option, description explains it. Name the option briefly in the label, and move the details — the price, the deadlines, the limitations — into the description.

Values ​

By default the model holds true or false. The true-value and false-value props replace them with your own values — handy when what goes to the server is not a boolean.

Multiple selection ​

If the model is an array, the controls work in multiple mode: every control adds its value to it and removes it when unchecked. The mode is turned on automatically — the multiple prop is needed only to set it by hand. Comparing the values is the job of value-comparator, a deep comparison by default.

States ​

disabled blocks the control and dims its color, readonly forbids changing the value while keeping the look. error puts the --error modifier on the root — the error color is drawn for it by the styles of the particular control, the base component has none.

Colors ​

color sets the color of the selected control, base-color the color of an unselected one. The values are the colors of the design system palette.

Layout ​

Inside a flex container the control stretches together with its neighbors. The inline prop leaves it the width of its content — that way the controls can stand in a row without extra gaps.

Slots ​

Slots replace the content coming from the props and the markup of the control itself.

SlotDescription
defaultContent inside the control area, before the input. Receives the classes and the background color styles
inputThe whole markup of the control. Receives model, the ready inputNode and the current icon
labelContent of the label. Replaces label
descriptionContent of the description. Replaces description

Scenarios ​

A selection tile ​

The input slot replaces the icon with your own markup, and the native input from inputNode stays on top of it and catches the clicks and the focus. The size of the clickable area is set by the --cds-selection-control-width and --cds-selection-control-height variables, and the state arrives in the slot through model.

The control does not show messages — that is the job of the CdsInput wrapper. Without it, render the error text next to the control and pass error to it: the modifier will come in handy for your styles.

API ​

Props

NameTypeDefaultDescription
activeColorstring | undefined—Color of the component in its active state
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
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'horizontal'Switches the component between horizontal and vertical layout.
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—Puts the component into the visual error state
falseIconIconValue | undefined—Icon of the unselected state
falseValueany—Value for the false state.
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
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
multipleboolean | null | undefinednullAllows selecting several values — the model becomes an array. Turns on automatically when an array is passed to modelValue
namestring | undefined—The name attribute of the native input element. In a group one name joins all the nested controls
readonlyboolean | null | undefinednullRead-only state
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
trueIconIconValue | undefined—Icon of the selected state
trueValueany—Value for the true state.
typestring | undefined—Type of the native input element — radio or checkbox, for example
valueany—Value used when the component is selected inside a group. When it is not set, a unique identifier is used.
valueComparatorValueComparator | undefinedtrueFunction comparing the model value with the value of the control. Deep comparison by default

Events

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

Slots

NameTypeDescription
default{ backgroundColorClasses: Ref<string[], string[]>; backgroundColorStyles: Ref<CSSProperties, CSSProperties>; }Default slot
description{ description: string | undefined; props: Record<string, any>; }Default slot of the CdsDescription component
inputSelectionControlSlotSlot for the default input element.
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component

Exposed

NameTypeDescription
inputHTMLInputElement | undefinedRef to the native input element — a checkbox or a radio button.
isFocusedbooleanWhether the control is focused.