Skip to content

CdsCheckbox

A checkbox turns an independent option on and off: consent to terms, an entry in a filter list, a profile setting. Every checkbox in a group is selected on its own, which makes it right for multiple choice.

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

To pick one option out of several use CdsRadio, for settings that apply immediately — CdsToggle. If you need the control alone, without a label and messages — in a table cell, for example — take CdsCheckboxButton.

Anatomy ​

A checkbox is made of the control with the state icon, the label with its description and the messages block under them. A click on the label toggles the checkbox exactly like a click on the control.

Variations ​

Label and description ​

The label prop names the option, description adds an explanation under the label. The slots of the same name accept arbitrary markup — a link to the terms, for example (see the "Consent in a form" scenario).

States ​

disabled blocks the checkbox and lowers the contrast, readonly forbids changes but does not change the appearance.

Indeterminate state ​

The indeterminate prop shows an intermediate state — usually on a parent checkbox when only some of the nested entries are selected. This is a visual flag: it does not change the model value, and the icon is set by indeterminate-icon. A live example is in the "Select all" scenario.

Sizes ​

The size prop accepts values from xs to xl, md by default. The label and the description scale together with the control.

Colors ​

The color prop sets the color of a selected checkbox, base-color — the color of an unselected one.

Messages and errors ​

messages shows a supporting text under the checkbox, error-messages — a ready error message (the one coming from the server, for example) together with the error state. Validation rules through rules are in the "Consent in a form" scenario.

Your own values ​

By default the model keeps true or false. The true-value and false-value props replace them with your own values — handy when the API expects strings or codes.

A group of checkboxes ​

To build a group, pass the same array in v-model to all the checkboxes and give them unique values — the selected values are collected into the array. hide-details removes the bottom block and the rows sit right next to each other; the common heading and description of the group are set by CdsLabel and CdsDescription.

Guidelines ​

Scenarios ​

Select all ​

The parent checkbox controls the group: a computed model checks and unchecks all the entries at once, and indeterminate turns on while only some of them are selected.

A required consent is a rule in rules that returns the error text while the checkbox is unchecked. The label slot adds a link to the terms into the label, and the button starts the check through the validate() of the CdsForm.

Filtering a list ​

A group of checkboxes with an array in v-model filters the list on the fly: a computed keeps the documents of the selected types, and an empty selection means "show everything".

API ​

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'horizontal'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'checkbox-off'Icon of the unselected state
falseValueany—Value for the false state.
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
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
indeterminateboolean | undefined—Indeterminate state of the checkbox.
indeterminateIconIconValue | undefined'checkbox-indeterminate-fill'Icon shown in the indeterminate state.
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
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
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'checkbox-on-fill'Icon of the selected state
trueValueany—Value for the true state.
typestring | undefined—Type of the native input element — radio or checkbox, for example
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
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
widthstring | number | undefined—Component width

Events

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

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
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
detailsCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
inputSelectionControlSlotSlot for the default input element.
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