# Pills input

**Pills input** allows users to enter multiple items, such as tags or email addresses, into a single input field.

## Usage

Pill Input is a control that lets users enter text, transforming each entry into a distinct "pill". A new entry is added when the Enter key is pressed or when separated by commas or semicolons. Existing pills can be deleted as needed.

![Pills input](<../images/pills-input.png>)

### When to use

- To add tags to categorize content, making it easier for users to classify and find items based on keywords.
- To enter email addresses or names, allowing users to easily add or remove recipients.
- For selecting multiple options from a wide range of choices.

### Best practices

- Guide users with examples in the placeholder, like "Enter tags, separated by commas."
- If needed, set a maximum number of pills to keep the interface clean.
- Offer autocomplete suggestions as required.
- Provide validation when necessary.

## Design

### Elements

![Pills input elements](<../images/pills-input-element.png>)

> 1\. Label (optional), 2. Pills, 3. Container, 4. Helper text (optional)

### Interaction states

![Pills input interaction states](<../images/pills-input-states.png>)

### Label (optional)

Effective labeling helps users understand what information to enter.

- Keep labels short and clear (should not be wider than the input field).
- Mandatory fields are marked with an `*`-asterisk.
- Labels can be placed on top of fields or to the left (in RTL languages). Prefer top placement of labels, since it provides a consistent left edge alignment, improving scannability.

![Pills input label](<../images/pills-input-label.png>)

### Validation

Real-time validation can be implemented to ensure that entered text meets specific criteria.

![Pills input validation](<../images/pills-input-validation.png>)

### Responsive behavior

When space is limited, pills are stacked to ensure adaptability across various screen sizes.

![Pills input responsive](<../images/pills-input-responsive.png>)

## Code

### Usage

```
import { SiPillsInputComponent } from '@siemens/element-ng/pills-input';

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

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SiFormItemComponent } from '@siemens/element-ng/form';
import {
  SiPillsInputComponent,
  SiPillsInputCsvDirective,
  SiPillsInputEmailDirective
} from '@siemens/element-ng/pills-input';

@Component({
  selector: 'app-sample',
  imports: [
    FormsModule,
    ReactiveFormsModule,
    SiPillsInputComponent,
    SiPillsInputCsvDirective,
    SiPillsInputEmailDirective,
    SiFormItemComponent
  ],
  templateUrl: './si-pills-input.html',
  host: { class: 'p-5' }
})
export class SampleComponent {
  readonly group = new FormGroup({
    tagList: new FormControl(['item-1', 'item-2']),
    csv: new FormControl(['item-1', 'item-2']),
    email: new FormControl(['test-user@example.org'])
  });

  readonly = false;
  disabled = false;

  disabledToggled(disabled: boolean): void {
    if (disabled) {
      this.group.disable();
    } else {
      this.group.enable();
    }
  }
}
```

Template:

```html
<form class="px-4" [formGroup]="group">
  <si-form-item label="Tags">
    <si-pills-input
      placeholder="Tag name,Other tag"
      formControlName="tagList"
      [readonly]="readonly"
    />
  </si-form-item>
  <si-form-item label="Comma-Separated Tags (or by semicolon)">
    <si-pills-input
      siPillsInputCsv
      placeholder="Tag name,Other tag"
      formControlName="csv"
      [readonly]="readonly"
    />
  </si-form-item>
  <si-form-item label="Email Recipients (separated by comma or semicolon)">
    <si-pills-input
      siPillsInputEmail
      placeholder="example@example.org,other@example.org"
      formControlName="email"
      [readonly]="readonly"
    />
  </si-form-item>
</form>
<div class="card mt-8 e2e-ignore">
  <div class="card-body">
    <si-form-item class="form-switch" label="Disabled">
      <input
        class="form-check-input"
        type="checkbox"
        [(ngModel)]="disabled"
        (ngModelChange)="disabledToggled($event)"
      />
    </si-form-item>
    <si-form-item class="form-switch" label="Readonly">
      <input class="form-check-input" type="checkbox" [(ngModel)]="readonly" />
    </si-form-item>
  </div>
</div>
```

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

**Kind:** component

**Selector:** `si-pills-input`

```
import { SiPillsInputComponent } from '@siemens/element-ng/pills-input';
```

```
component SiPillsInputComponent implements OnInit, ControlValueAccessor, SiFormItemControl
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L94-L94>) | `boolean` | `false` | Whether the pills-input is disabled. |
| [errormessageId](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L124-L124>) | `string` | `` `${this.id()}-errormessage` `` | This ID will be bound to the `aria-describedby` attribute of the pills-input. Use this to reference the element containing the error message(s) for the pills-input. It will be picked by the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) if the pills-input is used inside a form item.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L74-L74>) | `string` | `` `__si-pills-input-${SiPillsInputComponent.idCounter++}` `` | The identifier of the pills-input. Will be generated if not provided.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [inputElementAriaLabel](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L84-L86>) | `TranslatableString` | `` $localize`:@@SI_PILLS_INPUT.INPUT_ELEMENT_ARIA_LABEL:Create item` `` | The aria-label for the inner input where users enter new items. |
| [labelledby](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L112-L112>) | `string` | `` `${this.id()}-label` `` | 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#SiPillsInputComponent.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.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [placeholder](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L104-L104>) | `TranslatableString \| undefined` |  | The placeholder to be shown if no value is currently present. |
| [readonly](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input.component.ts#L101-L101>) | `boolean` | `false` | Whether the pills-input is readonly |

### [SiPillsInputCsvDirective](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input-csv.directive.ts#L19-L23>)

**Kind:** directive

**Selector:** `[siPillsInputCsv]`

```
import { SiPillsInputCsvDirective } from '@siemens/element-ng/pills-input';
```

```
directive SiPillsInputCsvDirective extends SiPillsInputPatternBase
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [pillsInputCsvItemRegex](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input-csv.directive.ts#L22-L22>) | `RegExp \| undefined` | `undefined` | Regex to validate an input string item.<br><br>Inherited from `SiPillsInputPatternBase` |

#### Attributes and Methods

##### separatorRegex

[Source for separatorRegex](<https://github.com/siemens/element/blob/main/projects/element-ng/pills-input/si-pills-input-pattern-base.ts#L20-L20>)

**Modifiers:** readonly, inherited

**Type:** `WritableSignal<RegExp>`

**Default:** `SEPARATOR_REGEX`

Regex to split the input value into segments.

Inherited from `SiPillsInputPatternBase`

### 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. |
