<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-input-field/usage/ in the magma monorepo.
-->

# mds-input-field - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-input-field>` component, ordered from most common to most specialized. The examples assume a working knowledge of the variant / tone ladders documented in [`agents/variants.md`](../../../../agents/variants.md) and the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Basic Labeled Input

The canonical form. Wrap [`mds-input`](../mds-input/AGENTS.md) in `<mds-input-field>` and set `label` for the visible field caption. The `<mds-input>` must be the first slotted element: the field wires validation to it, and with an empty slot it only logs a console warning.

```html
<mds-input-field label="Nome e cognome">
  <mds-input name="fullName" placeholder="Es: Mario Rossi"></mds-input>
</mds-input-field>
```

#### Input with a Static Helper Message

Set `message` to show a persistent hint below the control. The message disappears once the slotted input emits its first `mdsInputValidation` event, so use this only for pre-validation guidance.

```html
<mds-input-field label="Codice fiscale" message="16 caratteri alfanumerici">
  <mds-input name="cf" type="cf" placeholder="Es: MRCRSS83B21D704L"></mds-input>
</mds-input-field>
```

#### A Verdict with Its Icon

When the page judges the value of the field (a choice that holds or not, a check the value passes), set `variant` to the verdict, say it in `message` and give `icon` the mark of the verdict: it stands before the label, in the colour of the variant. The words stay in the message; the icon only lets the eye find the state.

```html
<mds-input-field
  label="Spazio colore"
  variant="warning"
  icon="mi/baseline/warning"
  message="La tonalità ruota di 25 gradi nei gradini scuri;OKLAB tiene meglio questo colore"
>
  <mds-input-select>
    <option value="OKLCH" selected>OKLCH</option>
    <option value="OKLAB">OKLAB</option>
  </mds-input-select>
</mds-input-field>
```

#### Validation-Driven State

When `<mds-input>` carries validation rules (e.g. `required`, `type="cf"`, or `pattern`), `<mds-input-field>` listens for `mdsInputValidation` and automatically sets `variant` to `'error'` (showing joined error messages) or `'success'` (clearing the message). No event wiring is needed - just add the validation attributes to the slotted input.

```html
<mds-input-field label="Indirizzo email">
  <mds-input name="email" type="email" required placeholder="Es: mario@esempio.it"></mds-input>
</mds-input-field>
```

#### Explicit Variant for Initial Status

If the field must start in a known non-default state - for example a server-side validation result rendered on page load - set `variant` directly. The validation cycle overwrites it at the first `mdsInputValidation` of the slotted input, which an `<mds-input>` emits on blur only when it has validation rules.

```html
<!-- Server returned an error before page load -->
<mds-input-field
  label="Username"
  variant="error"
  message="Username gia in uso"
>
  <mds-input name="username" value="mario.rossi"></mds-input>
</mds-input-field>
```

#### Field with an Action Button

The default slot accepts multiple elements. The first child grows to fill the available width; subsequent children shrink to their natural size. Use this to pair an input with a companion [`mds-button`](../mds-button/AGENTS.md).

```html
<mds-input-field label="Cerca per nome">
  <mds-input name="query" placeholder="Es: Mario Rossi"></mds-input>
  <mds-button icon="mi/baseline/search" size="lg" aria-label="Cerca"></mds-button>
</mds-input-field>
```

#### Inside a Form

The slotted `<mds-input>` is itself form-associated and submits under its own `name`. Place the field inside a `<form>` with a submit button; the field requires no extra wiring.

```html
<form action="/registrazione" method="post">
  <mds-input-field label="Nome">
    <mds-input name="firstName" required placeholder="Es: Mario"></mds-input>
  </mds-input-field>

  <mds-input-field label="Cognome">
    <mds-input name="lastName" required placeholder="Es: Rossi"></mds-input>
  </mds-input-field>

  <mds-button type="submit" label="Registrati" variant="primary" tone="strong"></mds-button>
</form>
```

#### Short Answers in a Group of Fields

Set `short` on a field whose answer is short (a house number, a postcode, a tax code, a date), and put the fields of one subject in a `fieldset` with the `field-group` utility of the styles package. A short field takes half the column and two in a row share it, in reading order: number and postcode, tax code and date of birth. Every other field takes the whole column, and under a column of `28rem` the halves take it too. The group reads its direct children: a framework component that wraps the field carries `short` on its own host.

```html
<form class="max-w-form">
  <fieldset class="field-group">
    <legend>Residenza</legend>
    <mds-input-field label="Via">
      <mds-input name="street"></mds-input>
    </mds-input-field>
    <mds-input-field label="Numero civico" short>
      <mds-input name="number"></mds-input>
    </mds-input-field>
    <mds-input-field label="CAP" short>
      <mds-input name="postcode"></mds-input>
    </mds-input-field>
  </fieldset>
</form>
```

#### Styling Customization

Adjust the message area colors through the two documented `--mds-input-field-*` CSS custom properties. Use the semantic color roles via `rgb(var(--magma-<role>))` ([`agents/color.md`](../../../../agents/color.md)) so dark mode and high-contrast modes keep working.

```css
.field-highlight mds-input-field {
  --mds-input-field-message-color: rgb(var(--magma-accent-fg));
  --mds-input-field-message-background: rgb(var(--magma-accent-surface));
}
```
