Atoms / Tag input

Tag input

Tags typed in: a separator or Enter ends one, a paste is split, Backspace removes the last.

import { TagInput } from "@forgeron/ui";
/01 → Emails
  • ada@example.com
  • not-an-email, not valid
import { TagInput } from "@forgeron/ui";
import { useState } from "react";

const email = (tag: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(tag);

export default function Emails() {
  const [to, setTo] = useState(["ada@example.com", "not-an-email"]);
  return (
    <TagInput
      label="Recipients"
      placeholder="Type or paste addresses"
      value={to}
      onChange={setTo}
      validate={email}
    />
  );
}
/02 → Labels
  • good first issue
import { TagInput } from "@forgeron/ui";

export default function Labels() {
  return (
    <TagInput
      label="Labels"
      placeholder="Add a label, then Enter"
      separators={/[,;\n]+/}
      defaultValue={["good first issue"]}
      max={5}
    />
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the field for assistive technology.
value / defaultValuestring[]—Controlled or not.
onChange(value: string[]) => void—On every change.
separatorsRegExp/[\s,;]+/What ends a tag, typed or pasted. Use /[,;\n]+/ for tags with spaces.
validate(tag) => boolean—A tag that fails is marked and announced, and kept.
maxnumber—At most this many tags.
allowDuplicatesbooleanfalseThe same tag twice.
placeholderstring—Shown while there is no tag.
disabledboolean—Not editable.
id / aria-describedby / aria-invalidstring / string / boolean—From `Field`'s function form: the label, hint and error stay tied.
classNamestring—Extra classes.