asafefe.dev

Tag Input

New

A field that turns text into chips that pop in and out. Enter or comma adds, Backspace removes, and suggestions can be picked with the arrow keys.

motionlucide-reactutils@soultel/use-reduced-motion-safe
  • billing
  • enterprise
Enter or comma to add. Backspace twice removes the last one.2/6

Installation

$ pnpm dlx shadcn@latest add @soultel/tag-input

No namespace set up? npx shadcn@latest add https://asafefe.dev/r/tag-input.json works too.

Manual installation
  1. 1. Install the dependencies.
    terminal1 lines
    npm install motion lucide-react
  2. 2. Copy the source from the Code tab into components/tag-input.tsx.

Usage

example.tsx12 lines
import { TagInput } from "@/components/tag-input"

export function ProjectTags() {
  return (
    <TagInput
      label="Tags"
      maxTags={5}
      defaultValue={["billing"]}
      suggestions={["onboarding", "bug", "feature request", "enterprise"]}
    />
  )
}

Props

PropTypeDefaultDescription
value / defaultValuestring[]-Controlled or initial tags.
onValueChange(tags: string[]) => void-Called when tags change.
suggestionsstring[]-Offered while typing, filtered and without tags already added.
maxTagsnumber-Upper limit. Shows a counter and shakes when exceeded.
validate(tag, tags) => boolean | string-Return false or a message to reject a tag.
preserveCasebooleanfalseKeep tags as typed instead of lowercasing.
label / hint / placeholderstring-Visible label, help text and placeholder.
namestring-Adds a hidden input with comma joined tags.