Skip to content

CdsCounter

New in v4.0

The counter shows how many characters are already typed and how many are allowed: 42 / 100. When the value exceeds the maximum, the counter turns red — the user sees the problem before submitting the form.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
6 / 6 props, events, slots and exposed members with a test
Test cases
12 unit · 0 browser
Code coverage
100% lines · 100% branches measured in v4.0.0-alpha.13
A11y (axe)
no violations in 4 stories measured in v4.0.0-alpha.11

The ready-made fields already have the counter built in — turn on the counter prop of CdsTextInput or CdsTextArea. A standalone CdsCounter is needed when you build your own control.

Variations ​

Value and maximum ​

The value prop sets the current number, max sets the limit. Without max the counter shows a single number, with max it shows a fraction. As soon as value is greater than max, the text turns red.

Appearance ​

The counter is visible only with active — by default it is hidden. Showing and hiding are animated, and the animation is set by the transition prop.

Slot ​

The default slot replaces the text of the counter and receives value, max and the ready counter string.

Scenarios ​

Character limit of a field ​

The details slot of an input is the place for a counter next to the messages. Count the length of the value yourself and pass it into value: when the text outgrows the limit, the counter turns red.

API ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Active state
disabledboolean | null | undefinednullDisabled state
maxstring | number | undefined—Maximum value
transitionstring | boolean | (TransitionProps & { component?: Component | undefined; }) | { component: Component; } | null | undefined{ component: slide-transition-y }Transition animation
valuestring | number | undefined0Value of the counter

Slots

NameTypeDescription
defaultCdsCounterSlotYour own look for the counter. The slot props carry the ready counter string, the current and the maximum number of characters