# Tag input

> Removable chips with a field to type more. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/tag-input/

:::tip[In plain words]
A box where you type words and each one becomes a little label you can remove.
:::

A box of chips with a field at the end. Enter or a comma turns the text into a tag and `on_change` gets the new list, as it does when the x of a chip is clicked. `suggestions` offers matching tags under it.

`ui.tag_input` · Category: [Forms](https://albakore.github.io/pyzed/library/#forms) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#tag_input)

**Code:**

```python
pz.container(ui.tag_input(["design", "rust", "python"], on_change, placeholder="Add a tag…"),
             width=380)
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- For labels, keywords, recipients or any short list the user types.
- For a fixed set of options, use [Chip](https://albakore.github.io/pyzed/library/chip/) filters or a [Drop-down](https://albakore.github.io/pyzed/library/select/).

## Usage

```python
import pyzed as pz
from pyzed import ui

class Labels(pz.App):
    def __init__(self):
        self.tags = ["design"]
        self.typing = ""

    def view(self):
        return ui.tag_input(self.tags, lambda tags: setattr(self, "tags", tags),
                            text=self.typing, on_input=lambda text: setattr(self, "typing", text),
                            suggestions=["python", "rust", "docs"])
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in Pyzed Dark or Pyzed Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Content

```python
pz.container(ui.tag_input([], on_change, placeholder="Add a tag…"), width=320)
```

```python
pz.container(ui.tag_input(["design", "rust"], on_change), width=320)
```

```python
pz.container(ui.tag_input(["design"], on_change, text="py", suggestions=["python", "pyzed", "rust"]), width=340)
```

```python
pz.container(ui.tag_input(["approved", "ready"], on_change, tint="success"), width=320)
```

### States and events

```python
pz.container(ui.tag_input(["one", "two"], on_change, max_tags=2), width=320)
```

```python
pz.container(ui.tag_input(["design", "rust"], on_change, disabled=True), width=320)
```

## Events

Tag input has **2 events of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_change` | `(tags)` | Callable that receives the new list of tags. |
| `on_input` | `(text)` | Callable that receives the text being typed. |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Labels(pz.App):
    def __init__(self):
        self.tags = ["design", "rust"]
        self.typing = ""

    def set_tags(self, tags):            # on_change: the new list (a tag added or removed)
        self.tags = tags

    def type(self, text):                # on_input: the text in the field
        self.typing = text

    def view(self):
        return ui.tag_input(self.tags, self.set_tags, text=self.typing, on_input=self.type,
                            placeholder="Add a label…", suggestions=["python", "docs", "bug"])
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class SetTags:
    tags: list

@dataclass
class Type:
    text: str

class Labels(pz.App):
    tags = ["design"]
    typing = ""

    def update(self, message):
        match message:
            case SetTags(tags):
                self.tags = tags
            case Type(text):
                self.typing = text

    def view(self):
        return ui.tag_input(self.tags, SetTags, text=self.typing, on_input=Type)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    tags: tuple = ("design",)
    typing: str = ""

def view(state, set_state):
    return ui.tag_input(state.tags, lambda tags: set_state(tags=tuple(tags)), text=state.typing,
                        on_input=lambda text: set_state(typing=text))
```

## Properties and API

### `ui.tag_input`

```python
tag_input(
    tags,
    on_change=None,
    *,
    text='',
    on_input=None,
    placeholder='',
    suggestions=(),
    max_tags=None,
    unique=True,
    tint='accent',
    width='fill',
    disabled=False,
    id=None,
    theme=None,
)
```

A box of removable chips with a field to type more. Enter or a comma turns the text into a tag; `on_change` gets the new list, as it does when a chip's x is clicked.

| Property | Default | Description |
| --- | --- | --- |
| `tags` | *required* | Your list of tags. |
| `text` | `""` | What is being typed (your state too). |
| `placeholder` | `""` | Hint text shown when empty. |
| `suggestions` | `()` | Tags offered under the field while the text matches them. |
| `max_tags` | `None` | Hides the field once the list has this many. |
| `unique` | `True` | A tag already in the list is not added again. |
| `tint` | `"accent"` | Color of the chips. |
| `width` | `"fill"` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `disabled` | `False` | Disables it (ignores clicks). |
| `id` | `None` | Stable identifier used to focus or scroll it with a `Command`. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_change`, `on_input` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#tag_input)

## Related

- [Chip](https://albakore.github.io/pyzed/library/chip/): A compact tag: clickable as a filter, removable with an ✕.
- [Input](https://albakore.github.io/pyzed/library/input/): A controlled text field with icon, placeholder and password mode.
- [Select](https://albakore.github.io/pyzed/library/select/): A dropdown to choose one option.
