# Input

The kit's text field: a floating or top label, hint and validation messages, prefix and suffix icons, three sizes and a multiline mode. It speaks Signal Forms natively and Reactive Forms through `VigValueAccessor`.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-input`, `[vigInputPrefix]`, `[vigInputSuffix]`
- Source: [libs/ui/src/lib/molecules/input-field/input-field.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/input-field/input-field.ts)

```typescript title="Import"
import { InputField, InputPrefix, InputSuffix } from '@vigdx/ui/input-field';
```

## Basic

The `label` rests inside the field and floats up on focus or when filled. Two-way bind `[(value)]` for plain signal state.

```typescript title="input-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [InputField],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="Full name" placeholder="Ada Lovelace" [(value)]="name" />
      <vig-input label="Email" type="email" value="ada@example.com" />
    </div>
    <p class="text-sm text-muted-foreground">Hello, {{ name() || 'stranger' }}.</p>
  `,
})
export class InputBasicDemo {
  protected readonly name = signal('');
}
```

## Label on top

`labelPosition="top"` sets the label above a shorter field, for forms laid out with labels over their controls. `multiline` renders a textarea; add `autoResize` to let it grow with its text (`field-sizing: content`, never shorter than `rows`).

```typescript title="input-label-top.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-label-top',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [InputField],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="First name" labelPosition="top" placeholder="Mia" />
      <vig-input label="Last name" labelPosition="top" placeholder="Wong" />
      <vig-input
        class="sm:col-span-2"
        label="Notes"
        labelPosition="top"
        [multiline]="true"
        [rows]="3"
        placeholder="Anything we should know?"
      />
    </div>
  `,
})
export class InputLabelTopDemo {}
```

## Hint and error

`hint` adds help text. Errors show once the field is `touched` and `invalid` (see Error timing below for the `interacted` mode); `errorMessages` overrides the built-in text per error kind (the i18n hook).

```typescript title="input-hint-error.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-hint-error',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [InputField],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="Username" hint="Visible only to your team." />
      <vig-input
        label="Email"
        type="email"
        value="ada@"
        [required]="true"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'email' }]"
      />
      <vig-input
        label="E-posta"
        type="email"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'email' }]"
        [errorMessages]="{ email: 'Geçerli bir e-posta adresi girin.' }"
      />
    </div>
  `,
})
export class InputHintErrorDemo {}
```

## Prefix and suffix icons

Mark any 1em icon with `vigInputPrefix` or `vigInputSuffix`; they are decorative and mirror in RTL.

```typescript title="input-icons.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Icon } from '@vigdx/ui/icon';
import { InputField, InputPrefix, InputSuffix } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-icons',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Icon, InputField, InputPrefix, InputSuffix],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="Search" type="search" placeholder="Orders, customers…">
        <vig-icon vigInputPrefix name="search" />
      </vig-input>
      <vig-input label="Due date" placeholder="2026-08-14">
        <vig-icon vigInputSuffix name="calendar" />
      </vig-input>
    </div>
  `,
})
export class InputIconsDemo {}
```

## Disabled, read-only and sizes

`disabled` removes the field from interaction, `readonly` keeps it focusable and selectable. `size` is `sm`, `md` (default) or `lg`.

```typescript title="input-states.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [InputField],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="Workspace" value="Acme Inc." [disabled]="true" />
      <vig-input label="Member since" value="2024" [readonly]="true" />
    </div>
    <div class="grid gap-4 sm:grid-cols-3">
      <vig-input label="Small" size="sm" />
      <vig-input label="Medium" size="md" />
      <vig-input label="Large" size="lg" />
    </div>
  `,
})
export class InputStatesDemo {}
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`. Built-in validators show the same messages; submit the empty form to see them and `focusFirstInvalid` move focus to the first error.

```typescript title="input-reactive-forms.ts"
import { JsonPipe } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  Injector,
  afterNextRender,
  inject,
  viewChild,
} from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Button } from '@vigdx/ui/button';
import { VigValueAccessor, focusFirstInvalid } from '@vigdx/ui/core';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [JsonPipe, ReactiveFormsModule, Button, InputField, VigValueAccessor],
  template: `
    <form #formRef class="flex flex-col gap-4" [formGroup]="form" (ngSubmit)="submit()">
      <div class="grid gap-4 sm:grid-cols-2">
        <vig-input label="Name" formControlName="name" autocomplete="name" />
        <vig-input label="Email" type="email" formControlName="email" autocomplete="email" />
      </div>
      <div class="flex gap-2">
        <vig-button type="submit" label="Save" />
        <vig-button label="Reset" variant="secondary" (click)="form.reset()" />
      </div>
      <pre class="rounded-input bg-subtle p-3 font-mono text-xs">{{ form.value | json }}</pre>
    </form>
  `,
})
export class InputReactiveFormsDemo {
  private readonly injector = inject(Injector);
  private readonly formRef = viewChild.required<ElementRef<HTMLFormElement>>('formRef');

  protected readonly form = new FormGroup({
    name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    email: new FormControl('', {
      nonNullable: true,
      validators: [Validators.required, Validators.email],
    }),
  });

  protected submit(): void {
    if (this.form.invalid) {
      this.form.markAllAsTouched();
      // Angular forms are novalidate: move focus to the first error ourselves,
      // once the fields have rendered their error state.
      afterNextRender(() => focusFirstInvalid(this.formRef()), { injector: this.injector });
    }
  }
}
```

## Signal Forms

`vig-input` implements `FormValueControl<string>`, so `[formField]` binds value, validation and interaction state with no accessor.

```typescript title="input-signal-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, email, form, required } from '@angular/forms/signals';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-input-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, InputField],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-input label="Name" [formField]="profile.name" />
      <vig-input label="Email" type="email" [formField]="profile.email" />
    </div>
    <p class="text-sm text-muted-foreground">
      Valid: {{ profile().valid() ? 'yes' : 'no' }} · {{ model().email || 'no email yet' }}
    </p>
  `,
})
export class InputSignalFormsDemo {
  // Initialize text fields with '' — never null or undefined.
  protected readonly model = signal({ name: '', email: '' });
  protected readonly profile = form(this.model, (path) => {
    required(path.name);
    required(path.email);
    email(path.email);
  });
}
```

## Error messages

A message is resolved in this order: the `message` of the validation error (Signal Forms schema) → `errorMessages` for its kind → the built-in default of the active locale → `errorMessages['default']` → a generic fallback. Set app-wide texts once with `provideVigdxUi`:

```typescript
provideVigdxUi({
  locale: 'en',
  labels: { errors: { required: 'Please fill in this field.' } },
});
```

## Error timing

By default an error shows once the field is touched. With `errorDisplay: 'interacted'` it waits until the field was left _and_ edited, or the surrounding `<form>` was submitted, so tabbing through an empty required field stays quiet. `VigValueAccessor` mirrors `dirty` and the form's `submitted` flag. Signal Forms has no submitted state (`submit()` only marks the fields touched), so the fields follow the `<form>` element's own `submit` event instead, which both `[formRoot]` and a `(submit)` handler calling `submit()` fire; `field().reset()` clears it again. A `submit()` called without a `<form>` reveals only the errors of fields the user edited.

To clear the form after a submit, call `resetForm()` on the `FormGroupDirective` (or `NgForm`) rather than only `form.reset()`: a plain reset leaves `submitted` set, so required errors would show on the fresh form, as in Angular Material.

```typescript
provideVigdxUi({ errorDisplay: 'interacted' }); // default: 'touched'
```

## After a failed submit

Angular forms are `novalidate`, so the browser does not move focus to the first invalid field. `focusFirstInvalid(root)` does: it focuses the first control under `root` with `aria-invalid="true"` (the checked radio of an invalid group), scrolls it into view and returns it, or `null` when no invalid field can take focus. A field in an inactive tab or a closed accordion item is revealed first, as find-in-page does; fields under plain `hidden` or `inert` are skipped. The screen reader then reads that field's error. Call it after the next render so the fields show their touched state first:

```typescript
private readonly injector = inject(Injector);
private readonly formRef = viewChild.required<ElementRef<HTMLFormElement>>('formRef');

save(): void {
  if (this.form.invalid) {
    this.form.markAllAsTouched();
    afterNextRender(() => focusFirstInvalid(this.formRef()), { injector: this.injector });
    return;
  }
  // …
}
```

With Signal Forms, every kit control also implements `focus()`, so `form.field().focusBoundControl()` moves focus to its input (or the trigger of a select), and `reset()`, so `form().reset()` drops half-typed text and closes open panels along with the touched and dirty state.

## Signal Forms models

Controls that can be emptied write `null`: `vig-number-input` and `vig-slider` (`number | null`), `vig-select`, `vig-autocomplete`, `vig-radio-group` and `vig-select-button` (`T | null`) and `vig-date-picker` (`Date | null`). Type those model fields as nullable and start them at `null`, even though Angular's guide prefers non-null models: clearing the control writes `null` into the field either way. Text fields use `''`, `vig-multi-select` and `vig-file-upload` use `[]`.

Remove hidden fields with `@if (!form.field().hidden())`; the controls' `hidden` input is only a fallback, and Signal Forms warns about rendered hidden fields in development.

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="input"][data-part="field"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="input"]`

| Part | Element | data-state |
| --- | --- | --- |
| `field` | Positioned wrapper of the label, slots and control. |   |
| `label` | The visible (or floating) label. |   |
| `prefix` | Wrapper of the `[vigInputPrefix]` slot. |   |
| `input` | The native `<input>`, or `<textarea>` with `multiline`. |   |
| `suffix` | Wrapper of the `[vigInputSuffix]` slot. |   |
| `hint` | The `vig-hint` (a `display: contents` host; style its `text` part). |   |
| `disabled-reason` | The `vig-hint` with the disabled reason (`display: contents`). |   |
| `error` | The `vig-field-error` (a `display: contents` host; style its `message` part). |   |

## Accessibility

- The visible `label` is a real `<label for>`; with `hideLabel` it stays as the accessible name and only the visual row goes.
- Hint, disabled reason and error are linked through `aria-describedby`; an invalid, touched field sets `aria-invalid="true"`. Error texts are not live regions: when an error first appears after the user leaves the field, it is announced once ("Email: Enter a valid email address.") through a single polite live region, half a second later so it does not talk over the next field. The error starts with an error icon (named by `fieldErrorIconLabel`, “Error”), so it differs from the hint by more than its color.
- After a failed submit, move focus to the first error with `focusFirstInvalid`.
- The floating label also reacts to browser autofill, which fills the field without an input event.
- `required` sets `aria-required` and adds a visual asterisk.
- Prefix and suffix icons are decorative. If an icon carries meaning, say it in the label or hint.
- Focus draws a 2px outline in the primary color over the border (the error color when invalid), at least 3:1 against the page and kept in forced-colors mode, inside the `--shadow-focus` halo (`--shadow-focus-error` when invalid).
- `email`, `url` and `search` fields turn off auto-capitalization, autocorrect and spell-check, which would mangle addresses and queries; `inputMode`, `enterKeyHint`, `autocapitalize`, `autocorrect` and `spellcheck` set them for other fields.

## API

### InputField

Selector: `vig-input` · Kind: component

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `autocapitalize` | `'none' \| 'sentences' \| 'words' \| 'characters' \| undefined` | `undefined` | Text correction (`autocapitalize`, `autocorrect`, `spellcheck`). Left unset, it is off for `email`, `url` and `search` fields, which hold identifiers and queries, and the browser default otherwise. |
| `autocomplete` | `string \| undefined` | `undefined` | — |
| `autocorrect` | `'on' \| 'off' \| undefined` | `undefined` | Autocorrect (`autocorrect`); off by default for `email`, `url` and `search`. |
| `autoResize` | `boolean` | `false` | Multiline only: the textarea grows with its content (`field-sizing: content`), never shorter than `rows`. Browsers without `field-sizing` keep the fixed, user-resizable height. |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `enterKeyHint` | `'enter' \| 'done' \| 'go' \| 'next' \| 'previous' \| 'search' \| 'send' \| undefined` | `undefined` | Label of the virtual keyboard's Enter key, e.g. `search` or `next` (`enterkeyhint`). |
| `errorMessages` | `Record<string, string>` | — | — |
| `errors` | `readonly ValidationError.WithOptionalFieldTree[]` | `[]` | — |
| `hidden` | `boolean` | `false` | — |
| `hideLabel` | `boolean` | `false` | Visually hides the label — it still names the control for assistive tech — and drops the row reserved for it, giving a shorter control for dense toolbars such as table filters. Pair it with a placeholder. |
| `hint` | `string \| undefined` | `undefined` | — |
| `inputMode` | `'none' \| 'text' \| 'decimal' \| 'numeric' \| 'tel' \| 'search' \| 'email' \| 'url' \| undefined` | `undefined` | Virtual keyboard hint, e.g. `numeric` or `decimal` (`inputmode`). |
| `invalid` | `boolean` | `false` | — |
| `label` (required) | `string` | — | — |
| `labelPosition` | `LabelPosition` | `'float'` | `float` (default) rests the label inside the field; `top` sets it above a shorter field, for forms laid out with labels over their controls. |
| `maxLength` | `number \| undefined` | `undefined` | — |
| `minLength` | `number \| undefined` | `undefined` | — |
| `multiline` | `boolean` | `false` | Renders a `<textarea>` instead of a single-line input. |
| `name` | `string` | `''` | — |
| `pending` | `boolean` | `false` | — |
| `placeholder` | `string \| undefined` | `undefined` | — |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `rows` | `number` | `3` | — |
| `size` | `InputSize` | `'md'` | — |
| `spellcheck` | `boolean \| undefined` | `undefined` | Spell-check (`spellcheck`); off by default for `email`, `url` and `search`. |
| `touched` | `boolean` | `false` | — |
| `type` | `InputType` | `'text'` | — |
| `value` (model) | `string` | `''` | — |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `string` | — |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` | Moves focus to the control's interactive element (the input, or the trigger of a popup control). Signal Forms calls it through `field().focusBoundControl()`; the host element itself cannot take focus. |
| `reset(): void` | Returns the control to its pristine look: drops in-progress text, closes an open popup, clears errors the component raised itself and the submitted state of `errorDisplay: 'interacted'`. Signal Forms calls it from `field().reset()`; `VigValueAccessor` from `control.reset()`. It never changes the value: the bound form owns that. |

### VigValueAccessor

Kind: directive

Bridges the vigdx form fields to Reactive Forms (`formControl`, `formControlName`) and template-driven forms (`ngModel`). Import it next to the component; Signal Forms (`[formField]`) need no accessor.

#### Methods

| Signature | Description |
| --- | --- |
| `registerOnChange(fn: (value: unknown) => void): void` | — |
| `registerOnTouched(fn: () => void): void` | — |
| `setDisabledState(disabled: boolean): void` | — |
| `writeValue(value: unknown): void` | — |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigInputPrefix]` | `InputPrefix` | Marks projected content as a leading icon inside the input (e.g. a search glyph). Any 1em-sized icon source works. Single-line inputs only: a `multiline` field keeps it hidden. |
| `[vigInputSuffix]` | `InputSuffix` | Marks projected content as a trailing icon inside the input; single-line only, like the prefix. |
