# Tag Input

A tokenizer for adding, validating, and removing string values.

Use Tag Input to add, validate, and remove string values in a text field. It uses the same surfaces, typography, and interaction styling as [Input](/components/input/), while growing vertically when selected tags wrap. Selected tags use the `red-light` palette by default.

```tsx
import { Field, TagInput } from "@zentara-co/zenos-ui";

function TagInputExample() {
  return (
    <Field.Root className="w-full max-w-md">
      <Field.Label>Keywords</Field.Label>
      <TagInput defaultValue={["React", "TypeScript"]} placeholder="Add keyword" />
    </Field.Root>
  );
}
```

## Import

```tsx
import { TagInput } from "@zentara-co/zenos-ui";
```

## Variants

Choose `variant="default"` for the standard bordered surface, `filled` for a subtle fill, or `none` for a transparent surface. Tag Input uses Input's variant and size styles directly, so its radius, border, background, typography, spacing, and state colors remain identical. Focus anywhere inside the control highlights its outer surface, and the control can still grow when tags wrap.

```tsx
import { Field, Stack, TagInput } from "@zentara-co/zenos-ui";

function TagInputVariantsExample() {
  return (
    <Stack gap="5" className="w-full max-w-lg">
      <Field.Root>
        <Field.Label>Default</Field.Label>
        <TagInput defaultValue={["React"]} placeholder="Add technology" />
      </Field.Root>
      <Field.Root>
        <Field.Label>Filled</Field.Label>
        <TagInput variant="filled" defaultValue={["TypeScript"]} placeholder="Add technology" />
      </Field.Root>
      <Field.Root>
        <Field.Label>None</Field.Label>
        <TagInput variant="none" defaultValue={["Design"]} placeholder="Add technology" />
      </Field.Root>
    </Stack>
  );
}
```

## Field states

Tag Input follows Field's disabled, required, invalid, description, and error relationships. Disabled controls prevent editing and disable tag actions without fading the whole surface. `readOnly` prevents editing and hides the default remove and clear actions. Invalid state uses Input's danger border and fill; `none` keeps its unfocused surface transparent.

```tsx
import { Field, Stack, TagInput } from "@zentara-co/zenos-ui";

function TagInputStatesExample() {
  return (
    <Stack gap="5" className="w-full max-w-lg">
      <Field.Root invalid>
        <Field.Label>Invalid</Field.Label>
        <TagInput defaultValue={[]} placeholder="Add at least one owner" />
        <Field.Error>Add at least one owner.</Field.Error>
      </Field.Root>
      <Field.Root disabled>
        <Field.Label>Managed tags</Field.Label>
        <TagInput defaultValue={["Organization policy"]} disabled />
        <Field.Description className="text-text-subtle">
          These tags are managed by your organization.
        </Field.Description>
      </Field.Root>
      <Field.Root>
        <Field.Label>Read-only tags</Field.Label>
        <TagInput defaultValue={["Organization policy"]} readOnly />
      </Field.Root>
    </Stack>
  );
}
```

## Sizes and wrapping

Use `size="sm"`, `md`, or `lg` to match nearby Inputs. Their minimum heights are 36px, 40px, and 44px respectively; unlike a single-line Input, Tag Input grows as tags wrap onto more lines. The default size is `md`.

```tsx
import { Field, Stack, TagInput } from "@zentara-co/zenos-ui";

function TagInputSizesExample() {
  return (
    <Stack gap="4" className="w-full max-w-lg">
      {(["sm", "md", "lg"] as const).map((size) => (
        <Field.Root key={size}>
          <Field.Label>{size}</Field.Label>
          <TagInput size={size} defaultValue={["React"]} placeholder="Add technology" />
        </Field.Root>
      ))}
    </Stack>
  );
}
```

## Controlled tags and query

Tag values and the pending input query can each be controlled independently. Update both callbacks when passing both controlled props.

```tsx
import { Field, Stack, TagInput, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

function TagInputControlledExample() {
  const [value, setValue] = useState(["React"]);
  const [inputValue, setInputValue] = useState("");

  return (
    <Stack gap="3" className="w-full max-w-lg">
      <Field.Root>
        <Field.Label>Technologies</Field.Label>
        <TagInput
          value={value}
          onValueChange={setValue}
          inputValue={inputValue}
          onInputValueChange={setInputValue}
          placeholder="Add technology"
        />
        <Field.Description>Press Enter or comma to add a value.</Field.Description>
      </Field.Root>
      <Text size="s" color="subtle" aria-live="polite">
        Values: {value.length ? value.join(", ") : "None"}
      </Text>
    </Stack>
  );
}
```

## Validation and limits

Normalize before validating. Rejected candidates report `duplicate`, `empty`, `invalid`, or `max`, allowing the surrounding Field to explain what happened without coupling validation messages to TagInput.

```tsx
import { Field, TagInput } from "@zentara-co/zenos-ui";
import { useState } from "react";

function TagInputValidationExample() {
  const [message, setMessage] = useState("Up to three unique tags; minimum three characters.");

  return (
    <Field.Root className="w-full max-w-lg">
      <Field.Label>Topics</Field.Label>
      <TagInput
        defaultValue={["React"]}
        maxTags={3}
        validateValue={(value) => value.length >= 3}
        normalizeValue={(value) => value.trim()}
        onValueRejected={(value, reason) => {
          const labels = {
            duplicate: `“${value}” is already selected.`,
            empty: "Empty tags are not allowed.",
            invalid: `“${value}” must contain at least three characters.`,
            max: "Remove a tag before adding another.",
          };
          setMessage(labels[reason]);
        }}
        placeholder="Add topic"
        aria-describedby="topic-guidance"
      />
      <Field.Description id="topic-guidance" aria-live="polite">
        {message}
      </Field.Description>
    </Field.Root>
  );
}
```

## Delimiters, blur, and forms

Enter always commits the pending query. `delimiters` adds other commit characters and also splits pasted text. `addOnBlur` commits a final query when focus leaves. With `name`, each tag becomes a hidden form value under the same key.

```tsx
import { Button, Field, Stack, TagInput, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

function TagInputFormExample() {
  const [submitted, setSubmitted] = useState("Not submitted");

  return (
    <form
      className="w-full max-w-lg"
      onSubmit={(event) => {
        event.preventDefault();
        const values = new FormData(event.currentTarget).getAll("skills");
        setSubmitted(values.join(", ") || "No values");
      }}
    >
      <Stack gap="4">
        <Field.Root required>
          <Field.Label>Skills</Field.Label>
          <TagInput
            name="skills"
            defaultValue={["React", "TypeScript"]}
            delimiters={[",", ";"]}
            addOnBlur
            required
            placeholder="Add skill"
          />
        </Field.Root>
        <Button type="submit" variant="primary">
          Submit skills
        </Button>
        <Text size="s" color="subtle" aria-live="polite">
          Submitted: {submitted}
        </Text>
      </Stack>
    </form>
  );
}
```

## Custom styling and tags

Use `className` for the outer control and `inputClassName` for its native text input. You do not need a variant prop to apply your own styles. The forwarded ref points to the native input.

The built-in tags use `color="red-light"`. Use `renderTag` to choose another palette or customize selected tags. Its arguments are the value, index, and a removal callback. Connect that callback to your tag's remove action. Custom renderers also own disabled and read-only presentation: omit removal actions for read-only values and disable them when the field is disabled.

```tsx
import { Field, Tag, TagInput } from "@zentara-co/zenos-ui";

function TagInputCustomExample() {
  return (
    <Field.Root className="w-full max-w-lg">
      <Field.Label>Project labels</Field.Label>
      <TagInput
        className="rounded-xl border-border-brand"
        inputClassName="placeholder:text-text-subtle"
        defaultValue={["Research", "Design"]}
        placeholder="Add label"
        renderTag={(value, index, remove) => (
          <Tag
            key={`${value}-${index}`}
            appearance="rounded"
            color="purple-light"
            onRemove={remove}
          >
            {value}
          </Tag>
        )}
      />
    </Field.Root>
  );
}
```

## Variants

default, filled, none

## Sizes

sm, md, lg

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | readonly string[] | — | Controlled tags. |
| defaultValue | readonly string[] | — | Initial uncontrolled tags. |
| onValueChange | (tags: string[]) => void | — | Runs when tags change. |
| inputValue / defaultInputValue | string | — | Controlled or initial pending query text. |
| onInputValueChange | (query: string) => void | — | Runs when the pending query changes. |
| maxTags | number | — | Maximum tag count. |
| allowDuplicates | boolean | false | Allows repeated values. |
| validateValue | (value: string) => boolean | — | Validates a pending tag. |
| normalizeValue | (value: string) => string | — | Normalizes a candidate before validation and insertion. |
| onValueRejected | (value, reason) => void | — | Reports duplicate, empty, invalid, or maximum-count rejection. |
| delimiters | readonly string[] | [","] | Characters that commit and split pending values. |
| addOnBlur / clearable | boolean | false / true | Blur commit and clear-all behavior. |
| renderTag | (value, index, remove) => ReactNode | — | Custom rendering for each selected tag. |
| variant | default \| filled \| none | default | Input surface style, including matching hover, focus, disabled, and invalid states. |
| size | sm \| md \| lg | md | Minimum control height. Grows vertically when tags wrap. |
| className / inputClassName | string | — | Style the outer surface or the native text input separately. |
| clearLabel | string | "Clear all tags" | Accessible name for the clear-all button. |
| disabled / readOnly | boolean | false | Prevent edits. Disabled also disables tag actions; readOnly hides the default remove and clear actions. |
| native props | React.InputHTMLAttributes<HTMLInputElement> | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Associate the native text input with a visible Field label. Selected tags form a named list, and every default remove button is labeled with its value. Enter or a configured delimiter commits the query; Backspace on an empty input removes the last tag. The clear button has a configurable accessible name. Announce rejection feedback in nearby described text, and do not rely on border color alone for invalid state.

Source: https://zenos-ui.pages.dev/components/tag-input/