# Number input

The **number input** allows users to enter and modify a numeric value with an optional unit.

## Usage

The component includes `+` / `-` controls to increment or decrement the value in predefined steps. It acts as a high-level alternative to the native number input control.

![Number input](<../images/number-input.png>)

### When to use

- To enter a numeric value in a form or inline in a component.
- When a user needs precise control over a numeric value.

### Best practices

- Define the width of the number input field according to the maximum value and the grid system.
- Offer a smart default value whenever possible.
- When precision is not important, use a [slider](<https://element.siemens.io/components/forms-inputs/slider/>) instead.

## Design

### Elements

![Number input - elements](<../images/number-input-elements.png>)

> 1\. Label (optional), 2. Container, 3. Content, 4. Unit (optional), 5. Controls (optional)

#### Label (optional)

Effective form labeling helps users understand what information to enter.

![Number input - label](<../images/number-input-label.png>)

- Provide a short and concise label that is shorter than the input field itself.
- A mandatory field is marked with an **`*`-asterisk.**
- Labels can be placed above or left to the input.

#### Controls (optional)

The number input can contain controls to increment or decrement the value.

It is possible to set the step size. E.g. in a range of `0..10'000`, a step size of `10` is more appropriate.

![Number input - controls](<../images/number-input-controls.png>)

### Interaction states

![Number input - states](<../images/number-input-states.png>)

### Validation

Real-time validation ensures that the data being entered is within a specific range or follows certain rules, preventing errors and ensuring the integrity of the data.

![Number input - validation](<../images/number-input-validation.png>)

## Code

A drop-in replacement for the HTML `<input type="number">`. It has more accessible increment decrement buttons and can show a unit.

### Usage

```
import { SiNumberInputComponent } from '@siemens/element-ng/number-input';

@Component({
  imports: [SiNumberInputComponent, ...]
})
```

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { JsonPipe } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiNumberInputComponent } from '@siemens/element-ng/number-input';

@Component({
  selector: 'app-sample',
  imports: [FormsModule, SiFormItemComponent, SiNumberInputComponent, JsonPipe],
  templateUrl: './si-number-input.html',
  host: { class: 'p-5' }
})
export class SampleComponent {
  value = 42;
  minValue = 0;
  maxValue = 100;
  stepSize = 1;
  required = true;
  readonly = false;
  disabled = false;
  unit = '°C';
  showButtons = true;
  alignEnd = true;
}
```

Template:

```html
<div class="row">
  <div class="col-sm-4">
    <label for="exampleInput" class="form-label" [class.required]="required && !readonly">
      Number input
    </label>
    <si-number-input
      #control="ngModel"
      inputId="exampleInput"
      class="form-control"
      [min]="minValue"
      [max]="maxValue"
      [step]="stepSize"
      [required]="required"
      [readonly]="readonly"
      [disabled]="disabled"
      [unit]="unit"
      [showButtons]="showButtons"
      [class.text-end]="alignEnd"
      [(ngModel)]="value"
    />
  </div>
</div>

<div class="card mt-6 e2e-ignore">
  <div class="card-header">Control panel</div>
  <div class="card-body">
    <si-form-item class="mb-6 col-sm-4" label="Current value">
      <input type="number" class="form-control" [(ngModel)]="value" />
    </si-form-item>
    <div class="mb-6"> Validation: {{ control.errors | json }} </div>
    <div class="row">
      <div class="col-sm-2">
        <si-form-item label="Readonly">
          <input type="checkbox" class="form-check-input" [(ngModel)]="readonly" />
        </si-form-item>
      </div>
      <div class="col-sm-2">
        <si-form-item label="Required">
          <input type="checkbox" class="form-check-input" [(ngModel)]="required" />
        </si-form-item>
      </div>
      <div class="col-sm-2">
        <si-form-item label="Disabled">
          <input type="checkbox" class="form-check-input" [(ngModel)]="disabled" />
        </si-form-item>
      </div>
      <div class="col-sm-2">
        <si-form-item label="-/+ Buttons">
          <input type="checkbox" class="form-check-input" [(ngModel)]="showButtons" />
        </si-form-item>
      </div>
      <div class="col-sm-2">
        <si-form-item label="Align end (right in LTR)">
          <input type="checkbox" class="form-check-input" [(ngModel)]="alignEnd" />
        </si-form-item>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <si-form-item label="Min. value">
          <input type="number" class="form-control" [(ngModel)]="minValue" />
        </si-form-item>
      </div>
      <div class="col-sm-4">
        <si-form-item label="Max. value">
          <input type="number" class="form-control" [(ngModel)]="maxValue" />
        </si-form-item>
      </div>
      <div class="col-sm-4">
        <si-form-item label="Step size">
          <input type="number" class="form-control" [(ngModel)]="stepSize" />
        </si-form-item>
      </div>
      <div class="col-sm-4">
        <si-form-item label="Unit">
          <input type="text" class="form-control" [(ngModel)]="unit" />
        </si-form-item>
      </div>
    </div>
  </div>
</div>
```

### [SiNumberInputComponent](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L63-L291>)

**Kind:** component

**Selector:** `si-number-input`

```
import { SiNumberInputComponent } from '@siemens/element-ng/number-input';
```

```
component SiNumberInputComponent implements OnChanges, ControlValueAccessor, Validator, SiFormItemControl
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [aria-label](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L115-L115>) | `TranslatableString \| undefined` | `undefined` | The aria-label passed to the input |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L134-L134>) | `boolean` | `false` | Whether the input is disabled. |
| [errormessageId](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L162-L162>) | `string` | `` `${this.id()}-errormessage` `` | This ID will be bound to the `aria-describedby` attribute of the number-input. Use this to reference the element containing the error message(s) for the number-input. It will be picked by the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) if the number-input is used inside a form item.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [inputId](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L124-L124>) | `string` | `` `__si-number-input-${SiNumberInputComponent.idCounter++}` `` | ID that is set on the input, e.g. for `<label for="...">` |
| [max](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L89-L93>) | `number` | `undefined` | The max. value for HTML input |
| [min](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L79-L83>) | `number` | `undefined` | The min. value for HTML input |
| [placeholder](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L145-L145>) | `TranslatableString \| undefined` |  | The placeholder for input field. |
| [readonly](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L140-L140>) | `boolean` | `false` | Whether the input can be focused but not edited. |
| [showButtons](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L109-L109>) | `boolean` | `true` | Show increment/decrement buttons? |
| [step](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L99-L99>) | `number \| "any"` | `1` | The step size for HTML input |
| [unit](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L103-L103>) | `string \| undefined` |  | Optional unit label |
| [value](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L101-L101>) | `number \| null \| undefined` |  | The value |

#### Output Properties

| Name | Type | Description |
| --- | --- | --- |
| [valueChange](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L148-L148>) | `number \| undefined` | Emits the current value when the user changes it. |

#### Attributes and Methods

##### id

[Source for id](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L126-L126>)

**Modifiers:** readonly

**Type:** `Signal<string>`

**Default:** `computed(() => this.inputId())`

The actual id of the control that should be used in the `for` attribute of a label.

Implements [SiFormItemControl](<index.html#t:SiFormItemControl>)

##### inputElement

[Source for inputElement](<https://github.com/siemens/element/blob/main/projects/element-ng/number-input/si-number-input.component.ts#L150-L150>)

**Modifiers:** readonly

**Type:** `Signal<ElementRef<HTMLInputElement>>`

**Default:** `viewChild.required<ElementRef<HTMLInputElement>>('inputElement')`

### Types Documentation

### [TranslatableString](<https://github.com/siemens/element/blob/main/projects/element-translate-ng/translate-types/global.types.ts#L18-L18>)

**Kind:** type alias

```
import type { TranslatableString } from '@siemens/element-translate-ng/translate-types';
```

Represents a translatable string. This can either be a translation key, e.g. `ACTIONS.EDIT` that will be automatically translated when displayed on the UI or a pre-translated string, e.g. `Edit`. Equivalent to a normal string in usage and functionality.

```
type TranslatableString = string & Translatable
```

### [SiFormItemControl](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L14-L38>)

**Kind:** interface

```
import type { SiFormItemControl } from '@siemens/element-ng/form';
```

Common interface for form item controls. Controls that should be connected to [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) must implement this interface and must be provided using the [SI\_FORM\_ITEM\_CONTROL](<../../../api/siemens/element-ng/types/SI_FORM_ITEM_CONTROL/index.html#t:SI_FORM_ITEM_CONTROL>) token.

```
interface SiFormItemControl
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [errormessageId](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L34-L34>) (readonly) | `string \| Signal<string>` | Generated ID that the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) will assign to the element containing the error messages. Implementations of [SiFormItemControl](<index.html#t:SiFormItemControl>) must generate this ID and assign it to the `aria-describedby` attribute of their focusable elements. |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L16-L16>) (optional, readonly) | `string \| Signal<string \| undefined>` | The actual id of the control that should be used in the `for` attribute of a label. |
| [isFormCheck](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L37-L37>) (optional, readonly) | `boolean` | If the control is meant to be used a checkbox. This affects the label positioning. |
| [labelledby](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L27-L27>) (optional, readonly) | `string \| Signal<string \| undefined> \| null` | ID that should be assigned to the label element inside the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>).<br><br>If the target control is not of type input, select or button, it cannot be referenced by a label. Instead, aria-labelledby must be used to create a reference to a label. In such a case, an implementation of [SiFormItemControl](<index.html#t:SiFormItemControl>) must provide the id of the label in [labelledby](<index.html#t:SiFormItemControl.labelledby>). [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) will apply the id to the label so that it can be used inside the control to reference it in the aria-labelledby attribute. |
