Skip to content

CdsInput

The wrapper of an input: the label, the description, the outer icons, the block of messages and the validation by rules. You put the control itself into the default slot — usually a CdsField with your input inside.

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

For regular input take the ready-made fields — CdsTextInput, CdsSelect, CdsTextArea: they are already assembled from CdsInput and CdsField. CdsInput is needed when your control is not in the library.

Anatomy ​

CdsInput lays out on a grid the label (CdsLabel), the description (CdsDescription), the outer icons, your control and the block of messages (CdsMessages). The default slot gives out id, messagesId and the validation states — all of them are refs, so reach for id.value in the template.

Variations ​

Label and description ​

The label prop names the field, description adds an explanation under the label. The slots of the same names accept arbitrary markup.

Messages and hints ​

There is a single line of text under the field, and the props split it by states: messages is visible at rest, hint while focused, and error-messages on top of the rest. CdsInput does not track the focus state itself: pass it with the focused prop.

Hiding the details ​

The hide-details prop removes the bottom block entirely — the fields can be placed tight against each other. The auto value hides the block only while there is nothing to show.

Icons ​

The prepend-icon and append-icon props put an icon outside the control. Clicks on them are caught by @click:prepend and @click:append.

Direction ​

The direction prop changes the grid: horizontal puts the icons on the sides of the control, vertical puts them above and below it.

Scenarios ​

Validating your own field ​

The rules prop accepts validation functions, validation-value accepts the value to pass to them. The result arrives in the slot through isValid: bind it to the error prop of CdsField and the frame turns red together with the message.

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
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 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.
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
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 in v-model. If the component supports the multiple prop, an empty array is used by default.
namestring | undefined—Name of the input
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
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
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
defaultCdsInputSlotDefault 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
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