Skip to content

Machine-translated – improvements welcome. Suggest an improvement

Inputs with a unit

ZeitraumEingabe and ProzentEingabe (#1035) are inputs for the field kinds period and percentage: a number and its unit on one line. They sit in a field (or in a frame whose label points at their id with htmlFor) and take over the frame's links – id, aria-describedby and aria-invalid go to the number, because that is where you type.

What it is for

Building block Shows Value
ZeitraumEingabe number (number pad) and a choice of days, weeks, months, years – singular while the number is 1 (“1 month”), otherwise plural { anzahl: string, einheit } as typed
ProzentEingabe number (decimal pad), followed by a fixed “%” (for reading only) text as typed; leseProzent reads “19”, “19%” and “19.0” the same

For a date there is Date, for a plain number an <input> in a field.

Matching patterns in the pattern catalogue: A4 · What someone enters stays as it is, A5 · Number and choice fields check immediately, C4 · Form rows, D1 · Singular and plural.

How to use it

With How it works
Mouse Click into the number and type; for a period choose the unit from the choice next to it. A click on the field's label puts the focus into the number.
Keyboard Tab leads into the number, for a period on into the unit choice (arrow keys choose). The “%” is not a stop.
Touch The number brings up the matching on-screen keyboard (digits for a period, decimal for a percentage). Number and choice are at least 44 px high (On the phone).

What it means

The unit always stands visibly next to the number – nobody has to guess whether “3” means days or months. The choice speaks the language of the number: “1 month”, “3 months”. The “%” only shows how the value is meant; only the number is typed. What the person types stays as it is until it is saved.

  • Reading and showing is not part of the building block but lives in shared/src/feldarten.ts: leseZeitraum (“3 months”, P3M, { anzahl, einheit }), zeitraumText (singular and plural from the language files, gemeinsam.feldarten.zeitraum.*), leseProzent, prozentText (“19 %” with a narrow no-break space U+202F where the language puts a space, “19%” in English).
  • Stored: a period as { anzahl, einheit }, a percentage as a number (19, not 0.19). Periods are compared in days (zeitraumTage: month 30.4375, year 365.25).

States

State How it comes about How it looks
period filled wert number and unit in the plural (“3 months”)
singular number is 1 unit in the singular (“1 month”)
error fehler on the field as with the field: border and message red
percentage wert number followed by “%”
optional, empty optional on the field “(optional)” after the label
disabled disabled number and choice faded like a disabled field

Section “Inputs with a unit” of the pattern page: period, singular, error, percentage, optional, disabled

Section “Inputs with a unit” dark

Accessibility

id, aria-describedby and aria-invalid from the field frame go to the number – that is where you type and where you hear name, error and hint. The unit choice has its own name for screen readers. Without a visible label (e.g. as the value of a condition) the input needs an aria-label.

Code

import { Feld, ProzentEingabe, ZeitraumEingabe, type ZeitraumRoh } from '../bausteine';

<Feld beschriftung={t('…kuendigungsfrist')}><ZeitraumEingabe wert={frist} setze={setFrist} /></Feld>
<Feld beschriftung={t('…steuersatz')} hinweis={t('…steuersatz-hinweis')}><ProzentEingabe wert={satz} setze={setSatz} /></Feld>

// Without a visible label (e.g. as the value of a condition): give an aria-label.
<ZeitraumEingabe aria-label={t('werkbank.sichten.wert')} wert={roh} setze={…} />

Files and tests

  • Building block: web/src/bausteine/einheit/Einheit.tsx, look: Einheit.module.css; reading and showing: shared/src/feldarten.ts
  • Pattern page: web/src/bausteine/einheit/muster.tsx – section Inputs with a unit: filled, singular, error, optional and disabled