Skip to content

CdsField

New in v4.0

The frame of an input: the background, the border, the focus and error states, the inner icons, the clear button and the loading indicator. You put the control itself into the default slot — the text fields, selects and autocompletes of the design system are built on CdsField.

Storybook GitLab
Test coverage: 96% — good coverage
API in specs
33 / 33 props, events, slots and exposed members with a test
Test cases
41 unit · 0 browser
Known bugs
4 pinned by an it.fails test
Code coverage
96.4% lines · 97.1% branches measured in v4.0.0-alpha.13
A11y (axe)
6 recommendations in 7 stories measured in v4.0.0-alpha.11

For regular input take the ready-made fields — CdsTextInput, CdsSelect, CdsTextArea. CdsField is needed when your control is not in the library. The label, the description and the messages around the frame are added by CdsInput.

Anatomy ​

The default slot receives a props object — bind it to your control so the field gets an id, a class and the link to the messages. Around the control CdsField lays out the inner icons and the clear button.

Variations ​

Icons ​

The prepend-inner-icon and append-inner-icon props put an icon inside the frame — before and after the control. The same areas are available as the prepend-inner and append-inner slots when you need a button rather than an icon.

Clearing ​

The clearable prop adds a cross; it shows up when the field is marked dirty and appears on hover or focus. The persistent-clearable prop keeps the cross visible at all times. The value is reset not by the component but by you — in the @click:clear handler.

States ​

The disabled, readonly and error props change the color of the frame and of the text. Set the state of the control inside separately: CdsField draws the frame but does not control your input.

Loading ​

The loading prop shows a bar under the field. The circle value replaces it with a circular indicator inside the frame — handier when the field waits for a server response while typing.

Sizes ​

The size prop changes the height of the frame and its inner paddings. The default size is md.

Scenarios ​

A field with tags ​

The control in the default slot is any markup you like, so a set of tags and an input for a new entry fit inside the frame. The focus and blur handlers from the slot turn the highlighting of the frame on, dirty shows the cross, and @click:clear empties the list.

API ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Active state of the field
activeColorstring | undefined—Color of the component in its active state
appendInnerIconIconValue | undefined—Icon in the inner append slot, after the content of the field
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
detailsboolean | undefined—Reserves the space for hints and error messages under the field
dirtyboolean | undefined—Marks that the value of the field was changed by the user
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—Error 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.
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Field identifier — the value of the id HTML attribute for the inner control
loadingstring | boolean | undefined—Loading indicator for the component
persistentClearableboolean | undefined—Always show the clear button, not only on hover or focus
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
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme

Events

NameTypeDescription
click:clear[e: MouseEvent]Click on the clear button (clearable prop)
update:focused[focused: boolean]Fires when the element gains or loses focus

Slots

NameTypeDescription
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).
defaultCdsFieldSlotContent inside the field
loaderLoaderSlotPropsSlot for a custom loader (shown when the loading prop is true).
prepend-innerCdsInputDefaultSlotSlot added at the start of the input

Exposed

NameTypeDescription
controlRefHTMLElement | undefinedRef to the control of the input
fieldIconColorstring | undefinedIcon color