# Number input

A text field for numbers that parses and formats with the active locale. Use it for quantities, amounts and prices; the value is a `number`, or `null` when empty.

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

```typescript title="Import"
import { NumberInput } from '@vigdx/ui/number-input';
```

## Basic

Bind `[(value)]` to a `number | null` signal. The value commits on blur or Enter and is clamped to `min` and `max`; ArrowUp and ArrowDown change it by `step`. Text that is not a number is rejected and the previous value comes back.

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

@Component({
  selector: 'app-number-input-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NumberInput],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-number-input
        label="Seats"
        hint="Between 1 and 50."
        [min]="1"
        [max]="50"
        [(value)]="seats"
      />
      <vig-number-input
        label="Discount (%)"
        hint="Arrow keys step by 5."
        [min]="0"
        [max]="100"
        [step]="5"
        [(value)]="discount"
      />
    </div>
    <p class="text-sm text-muted-foreground">
      Seats: {{ seats() ?? 'empty' }} · Discount: {{ discount() ?? 'empty' }}
    </p>
  `,
})
export class NumberInputBasicDemo {
  protected readonly seats = signal<number | null>(12);
  protected readonly discount = signal<number | null>(null);
}
```

## Formatting

Once the field loses focus the value is shown with `Intl.NumberFormat` in the active locale. Turn on `useGrouping`, bound the decimals with `minFractionDigits` and `maxFractionDigits`, add literal `prefix` or `suffix` text, or pass an ISO `currency` code. While focused the field shows the plain number for editing.

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

@Component({
  selector: 'app-number-input-formatting',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NumberInput],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-number-input label="Population" [value]="84680273" [useGrouping]="true" />
      <vig-number-input
        label="Exchange rate"
        [value]="1.0842"
        [minFractionDigits]="2"
        [maxFractionDigits]="4"
      />
      <vig-number-input label="Price" currency="EUR" [value]="1234.5" />
      <vig-number-input label="Weight" prefix="~ " suffix=" kg" [value]="72.5" />
    </div>
  `,
})
export class NumberInputFormattingDemo {}
```

## Label on top, hint and error

`labelPosition="top"` puts the label above the field. `hint` adds help text; errors show once the field is `touched` and `invalid`, and `errorMessages` replaces the built-in text for each error kind.

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

@Component({
  selector: 'app-number-input-hint-error',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NumberInput],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-number-input
        label="Quantity"
        labelPosition="top"
        placeholder="0"
        hint="Whole units only."
      />
      <vig-number-input
        label="Budget"
        labelPosition="top"
        currency="USD"
        [value]="250"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'min' }]"
        [errorMessages]="{ min: 'The minimum budget is $500.' }"
      />
    </div>
  `,
})
export class NumberInputHintErrorDemo {}
```

## Disabled, read-only and sizes

`disabled` blocks all interaction; `readonly` keeps the formatted value focusable and selectable but ignores typing and arrow keys. `size` is `sm`, `md` (default) or `lg`.

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

@Component({
  selector: 'app-number-input-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NumberInput],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-number-input label="Credit limit" currency="EUR" [value]="5000" [disabled]="true" />
      <vig-number-input label="Invoice total" currency="EUR" [value]="1290" [readonly]="true" />
    </div>
    <div class="grid gap-4 sm:grid-cols-3">
      <vig-number-input label="Small" size="sm" [value]="42" />
      <vig-number-input label="Medium" size="md" [value]="42" />
      <vig-number-input label="Large" size="lg" [value]="42" />
    </div>
  `,
})
export class NumberInputStatesDemo {}
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`. Type the control as `number | null`; resetting it empties the field.

```typescript title="number-input-reactive-forms.ts"
import { JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Button } from '@vigdx/ui/button';
import { VigValueAccessor } from '@vigdx/ui/core';
import { NumberInput } from '@vigdx/ui/number-input';

@Component({
  selector: 'app-number-input-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [JsonPipe, ReactiveFormsModule, Button, NumberInput, VigValueAccessor],
  template: `
    <form class="flex flex-col gap-4" [formGroup]="form" (ngSubmit)="submit()">
      <div class="grid gap-4 sm:grid-cols-2">
        <vig-number-input label="Guests" formControlName="guests" [min]="1" [max]="12" />
        <vig-number-input label="Deposit" formControlName="deposit" currency="EUR" />
      </div>
      <div class="flex gap-2">
        <vig-button type="submit" label="Book" />
        <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 NumberInputReactiveFormsDemo {
  protected readonly form = new FormGroup({
    guests: new FormControl<number | null>(null, Validators.required),
    deposit: new FormControl<number | null>(null, Validators.required),
  });

  protected submit(): void {
    this.form.markAllAsTouched();
  }
}
```

## Signal Forms

`vig-number-input` implements `FormValueControl<number | null>`. Schema `min()` and `max()` rules bind to the field's `min` and `max`, so typed values are clamped to the allowed range.

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

interface Applicant {
  age: number | null;
  income: number | null;
}

@Component({
  selector: 'app-number-input-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, NumberInput],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-number-input label="Age" hint="18 to 120." [formField]="applicant.age" />
      <vig-number-input label="Monthly income" currency="USD" [formField]="applicant.income" />
    </div>
    <p class="text-sm text-muted-foreground">
      Valid: {{ applicant().valid() ? 'yes' : 'no' }} · Age: {{ model().age ?? 'empty' }}
    </p>
  `,
})
export class NumberInputSignalFormsDemo {
  // Numeric fields are empty as null.
  protected readonly model = signal<Applicant>({ age: null, income: null });
  protected readonly applicant = form(this.model, (path) => {
    required(path.age);
    min(path.age, 18);
    max(path.age, 120);
    required(path.income);
  });
}
```

## 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="number-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="number-input"]`

| Part | Element | data-state |
| --- | --- | --- |
| `field` | Positioned wrapper of the label and the control. |   |
| `label` | The visible (or floating) label. |   |
| `input` | The native `<input>`. |   |
| `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 control is a native `<input type="text" inputmode="decimal">` named by a real `<label for>`, so mobile keyboards show digits and the decimal separator. `hideLabel` keeps the label as the accessible name.
- ArrowUp and ArrowDown change the value by `step`; Enter commits the typed text. It is not a `spinbutton`, so state the allowed range in the label or `hint`.
- Hint, disabled reason and error are linked through `aria-describedby`; a touched, invalid field sets `aria-invalid="true"`.
- Currency symbols, prefix and suffix are part of the displayed text, so screen readers announce them with the value.

## API

### NumberInput

Selector: `vig-number-input` · Kind: component

Numeric field (PrimeNG p-inputnumber territory) with the floating label. Typing is free-form; the text commits on blur or Enter — parses with the active locale's group and decimal separators (or a plain `.`/`,` decimal), clamps to min/max, and reverts anything unparseable. The committed value is shown formatted for the locale (`Intl.NumberFormat`) with optional grouping, fraction digits, currency and literal prefix/suffix; while focused the field shows the plain editable number. Arrow keys step by `step`. Empty is null.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `currency` | `string \| undefined` | `undefined` | ISO 4217 code (e.g. `'EUR'`) formatting the value as currency in the active locale; implies grouping and the currency's fraction digits. |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `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` | — |
| `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. |
| `max` | `number \| undefined` | `undefined` | — |
| `maxFractionDigits` | `number \| undefined` | `undefined` | Maximum fraction digits shown; typed values are rounded to it on commit. Unset, values keep their full precision (up to 20 digits). |
| `min` | `number \| undefined` | `undefined` | — |
| `minFractionDigits` | `number \| undefined` | `undefined` | Minimum fraction digits shown (pads with zeros), e.g. `2` for `12.50`. |
| `name` | `string` | `''` | — |
| `pending` | `boolean` | `false` | — |
| `placeholder` | `string \| undefined` | `undefined` | — |
| `prefix` | `string` | `''` | Literal text shown before the formatted value, e.g. `'~ '`. |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `size` | `FieldSize` | `'md'` | — |
| `step` | `number` | `1` | — |
| `suffix` | `string` | `''` | Literal text shown after the formatted value, e.g. `' kg'`. |
| `touched` | `boolean` | `false` | — |
| `useGrouping` | `boolean \| undefined` | `undefined` | Thousands separators in the formatted value; on by default only with `currency`. |
| `value` (model) | `number \| null` | `null` | — |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `number \| null` | — |

#### 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` | Drops the text being typed; see `VigFormField.reset`. |

### 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` | — |
