Skip to content

CdsTextInput

A text field is the basic way to get data from the user: a name, an email, an amount, a search query. Around the native input the component gathers everything a form needs: a label, a description, hints, error messages, icons, clearing and a character counter.

Storybook GitLab Figma
Test coverage: 97% — good coverage
API in specs
87 / 87 props, events, slots and exposed members with a test
Test cases
86 unit · 3 browser
Known bugs
6 pinned by an it.fails test
Code coverage
97.2% lines · 87.9% branches measured in v4.0.0-alpha.13
A11y (axe)
12 recommendations in 19 stories measured in v4.0.0-alpha.11

For multiline text use CdsTextArea, to pick from a ready list of options use CdsSelect.

Anatomy ​

The first diagram shows the frame of the component: the label, the description, the field and the bottom block with messages and the counter. The second one shows how the field itself is built: the prepend and append zones outside the border, prepend-inner and append-inner inside it, and the clear button. Every zone is available both as a *-icon prop and as a slot of the same name.

Variations ​

Label and description ​

The label prop names the field, description adds a supporting text under the label. The slots of the same name accept arbitrary markup.

Messages and hints ​

There is a single text line under the field, and props split it by state: messages is visible at rest, hint — on focus, error-messages — on an error and on top of the rest. The messages and details slots override the content.

Hiding the details ​

The hide-details prop removes the bottom block entirely — fields can then sit right next to each other. The auto value hides the block only while there is nothing to show: as soon as an error or a hint appears, the block is rendered and pushes the content below.

Icons ​

The prepend-icon and append-icon props put an icon outside the field, prepend-inner-icon and append-inner-icon — inside it.

Prefix and suffix ​

The prefix and suffix props add an immutable text at the edges of the entered value — a currency, a country code, a domain.

Clearing ​

The clearable prop shows the clear icon on hover or focus, persistent-clearable keeps it visible at all times.

States ​

disabled blocks the input and lowers the contrast, readonly forbids changes but keeps focus and copying, loading shows a loading indicator — as a bar or as a circle (loading="circle").

Validation ​

The rules prop accepts an array of validator functions: each one returns either true or a string with an error message. validate-on sets the moment they run — blur, input or submit (only on a validate() call).

Validators are plain functions: declare them once and reuse them across the fields of a form. Here the title is checked for being required and for its length (together with maxlength and the counter), and the email — for its format.

Errors from outside ​

The error-messages prop shows ready messages — the ones coming from the server, for example — independently of rules. error turns on the visual error state without any text, max-errors limits the number of messages.

Counter ​

The counter prop shows a character counter: true — without a limit, a number — with a limit. counter-value accepts your own counting formula, persistent-counter keeps the counter visible outside of focus.

Colors ​

The color prop sets the main color, active-color — the color on focus, icon-color — the color of the icons, bg-color — the background of the field.

Slots ​

When props are not enough, the content of any block is replaced by a slot. prepend / append put elements outside the field, prepend-inner / append-inner — inside it; unlike the *-icon props, slots accept any components — buttons, links, badges.

Slot nameDescription
labelLabel content (replaces label)
descriptionDescription content (replaces description)
prependElement before the input
appendElement after the input
prepend-innerElement at the start of the field, inside the border
append-innerElement at the end of the field, inside the border
clearYour own clear icon (with clearable)
loaderYour own loading indicator (with loading)
counterCounter content
detailsThe whole bottom block — messages and counter
messagesContent of the messages block

Guidelines ​

Scenarios ​

Searching through a list ​

A search field is made of the prepend-inner-icon="search" icon and clearable — the user resets the query in one click and sees the full list again. A label is not needed here: the role of the field is obvious from the icon and the placeholder.

Password with a preview ​

The append-inner slot accepts interactive elements — a button that toggles type between password and text, for example. The hint in hint states the password requirements before the user makes a mistake.

An error from the server ​

The server response goes into error-messages as is — validation through rules is not needed for that. Reset the error when the value changes, so that the field does not complain about an input that is already fixed.

Events ​

Every icon zone emits its own click event: click:prepend, click:append, click:prepend-inner, click:append-inner and click:clear (the last one fires after the value has already been reset). Here the icon on the left cycles through the options, the lock inside the field toggles, and the submit button clears the message. When you need a complex element instead of an icon — a button, a badge — use the slots of the same name, like in the password example above.

API ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Active state of the field
activeColorstring | undefined—Color of the component in its active state
appendIconIconValue | undefined—Icon in the append slot, after the content
appendInnerIconIconValue | undefined—Icon in the inner append slot, after the content of the field
autocompletestring | undefined—Helps to influence browser suggestions. The suppress value changes the name attribute of the fields, and off relies on the browser to stop suggesting values on its own. Any other value is passed to the built-in autocomplete of the corresponding element.
autofocusboolean | undefined—Enables autofocus
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
centerAffixboolean | undefined—Vertically center the appendInner, prependInner, clearIcon and label elements.
clearableboolean | undefined—Shows the clear button
clearIconIconValue | undefined'remove'Icon of the clear button
colorstring | undefined—Component color
counterstring | number | boolean | undefined—Character counter. Accepts a boolean, a number or a string
counterValuenumber | ((value: any) => number) | undefined—Custom counter value. Either a number or a function computing it from the current value
descriptionstring | undefined—Description of the component, shown under the label
dirtyboolean | undefined—Marks that the value of the field was changed by the user
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—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
loadingstring | boolean | undefined—Loading indicator for 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
modelModifiersRecord<string, boolean> | undefined—v-model modifiers passed by Vue to process the field value
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
persistentClearableboolean | undefined—Always show the clear button, not only on hover or focus
persistentCounterboolean | undefined—Always show the counter, not only while the field is in use
persistentHintboolean | undefined—Always show the hint, not only on focus
persistentPlaceholderboolean | undefined—Always show the placeholder instead of hiding it while the field is in use
placeholderstring | undefined—Placeholder text shown in an empty field
prefixstring | undefined—Text prefix shown before the field value
prependIconIconValue | undefined—Icon in the prepend slot, before the content
prependInnerIconIconValue | undefined—Icon in the inner prepend slot, before the content of the field
readonlyboolean | null | undefinednullRead-only state
reverseboolean | undefined—Reverses the order of the elements inside the field
rolestring | undefined—ARIA role for the native input element
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
suffixstring | undefined—Text suffix shown after the field value
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
typestring | undefined'text'Type of the native input
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
click:append[e: MouseEvent]Click on the outer append icon (append-icon prop)
click:appendInner[e: MouseEvent]Click on the inner append icon (append-inner-icon prop)
click:clear[e: MouseEvent]Click on the clear icon (clearable); fires after the value has been reset
click:control[e: MouseEvent]Fires on a click in the main area of the control
click:prepend[e: MouseEvent]Click on the outer prepend icon (prepend-icon prop)
click:prependInner[e: MouseEvent]Click on the inner prepend icon (prepend-inner-icon prop)
mousedown:control[e: MouseEvent]Fires on a mouse button press in the main area of the control
update:focused[focused: boolean]Fires when the element gains or loses focus
update:modelValue[value: string]Fires when the model of the component changes

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
append-innerCdsInputDefaultSlotSlot added at the end of the input
clearCdsInputDefaultSlot & { props: Record<string, any>; }Slot for a custom clear icon (shown when the clearable prop is true).
counterCdsCounterSlotSlot for a custom counter
default{ id: Readonly<Ref<string, string>>; }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
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component
loaderLoaderSlotPropsSlot for a custom loader (shown when the loading prop is true).
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
prepend-innerCdsInputDefaultSlotSlot added at the start of the input

Exposed

NameTypeDescription
controlRefHTMLElement | undefinedRef to the control of the input
fieldIconColorstring | undefinedIcon color
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