Forms

Token Input

Free-form multi-value field: Enter, comma or paste creates tokens; Backspace removes the last.

react-aria-componentslucide-react

Preview

The second field validates emails.

VP SalesHead of Revenue

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a token-input.tsx file and paste the following code into it.

    src/components/ui/token-input.tsx
    "use client";
    
    import { type KeyboardEvent, useId, useRef, useState } from "react";
    import { cx } from "@/utils/cx";
    import { Tag } from "./tag";
    
    export interface TokenInputProps {
      value?: string[];
      defaultValue?: string[];
      onChange?: (tokens: string[]) => void;
      label?: string;
      placeholder?: string;
      /** Return false to reject a token (e.g. invalid email). */
      validate?: (token: string) => boolean;
      max?: number;
      className?: string;
    }
    
    /** Free-form multi-value input: Enter, comma or paste creates tokens; Backspace removes the last one. */
    export function TokenInput({ value, defaultValue = [], onChange, label, placeholder = "Add…", validate, max, className }: TokenInputProps) {
      const [inner, setInner] = useState(defaultValue);
      const tokens = value ?? inner;
      const [draft, setDraft] = useState("");
      const [invalid, setInvalid] = useState(false);
      const input = useRef<HTMLInputElement>(null);
      const id = useId();
    
      const update = (next: string[]) => {
        setInner(next);
        onChange?.(next);
      };
      const add = (raw: string) => {
        const parts = raw
          .split(/[,\n]/)
          .map((s) => s.trim())
          .filter(Boolean);
        const ok = parts.filter((p) => !tokens.includes(p) && (!validate || validate(p)));
        setInvalid(parts.length > ok.length && parts.some((p) => validate && !validate(p)));
        if (!ok.length) return false;
        update([...tokens, ...ok].slice(0, max ?? Infinity));
        return true;
      };
      const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
        if ((e.key === "Enter" || e.key === ",") && draft.trim()) {
          e.preventDefault();
          if (add(draft)) setDraft("");
        } else if (e.key === "Backspace" && !draft && tokens.length) {
          update(tokens.slice(0, -1));
        }
      };
      const full = max !== undefined && tokens.length >= max;
    
      return (
        <div className={cx("flex flex-col gap-2", className)}>
          {label && (
            <label htmlFor={id} className="text-sm font-medium text-fg">
              {label}
            </label>
          )}
          <div
            onClick={() => input.current?.focus()}
            className={cx(
              "flex min-h-10 cursor-text flex-wrap items-center gap-1.5 rounded-md border bg-surface px-2 py-1.5 transition-[border,box-shadow]",
              invalid ? "border-error focus-within:shadow-focus-error" : "border-line-strong focus-within:border-accent focus-within:shadow-focus",
            )}
          >
            {tokens.map((t) => (
              <Tag key={t} variant="gray" onRemove={() => update(tokens.filter((x) => x !== t))}>
                {t}
              </Tag>
            ))}
            {!full && (
              <input
                ref={input}
                id={id}
                value={draft}
                placeholder={tokens.length ? "" : placeholder}
                aria-invalid={invalid || undefined}
                onChange={(e) => {
                  setDraft(e.target.value);
                  setInvalid(false);
                }}
                onKeyDown={onKeyDown}
                onPaste={(e) => {
                  const text = e.clipboardData.getData("text");
                  if (/[,\n]/.test(text)) {
                    e.preventDefault();
                    add(text);
                  }
                }}
                onBlur={() => draft.trim() && add(draft) && setDraft("")}
                className="h-7 min-w-24 flex-1 bg-transparent px-1 text-base outline-none placeholder:text-fg-muted"
              />
            )}
          </div>
          {invalid && <p className="text-sm text-error-text">That doesn’t look valid.</p>}
        </div>
      );
    }
  3. Create a tag.tsx file and paste the following code into it.

    src/components/ui/tag.tsx
    "use client";
    
    import { X } from "lucide-react";
    import type { ReactNode } from "react";
    import { Button as AriaButton } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    export interface TagProps {
      /** `stroke` white with border (default) · `gray` muted fill. */
      variant?: "stroke" | "gray";
      /** Leading visual: an icon, <Avatar size="xs">, <CompanyMark size="sm">, a flag… */
      leading?: ReactNode;
      /** Muted secondary text after the label, e.g. a count. */
      sublabel?: ReactNode;
      onRemove?: () => void;
      isDisabled?: boolean;
      className?: string;
      children: ReactNode;
    }
    
    /** Compact 24px label for people, companies, filters and categories. */
    export function Tag({ variant = "stroke", leading, sublabel, onRemove, isDisabled, className, children }: TagProps) {
      return (
        <span
          className={cx(
            "inline-flex h-6 shrink-0 items-center gap-1.5 rounded-[6px] text-sm font-medium [&>svg]:size-3.5",
            onRemove ? "pl-1.5 pr-1" : "px-1.5",
            !leading && "pl-2",
            variant === "stroke" ? "bg-surface text-fg ring-1 ring-inset ring-line" : "bg-muted text-fg",
            isDisabled && "text-fg-disabled ring-line-subtle [&_*]:opacity-60",
            className,
          )}
        >
          {leading}
          <span>{children}</span>
          {sublabel && <span className="font-normal text-fg-muted">{sublabel}</span>}
          {onRemove && (
            <AriaButton
              aria-label={`Remove ${typeof children === "string" ? children : "tag"}`}
              onPress={onRemove}
              isDisabled={isDisabled}
              className="inline-flex size-4 items-center justify-center rounded-[4px] text-fg-muted outline-none data-hovered:bg-active data-hovered:text-fg data-focus-visible:shadow-focus"
            >
              <X className="size-3" />
            </AriaButton>
          )}
        </span>
      );
    }
  4. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.