Loading component…
Loading component…
Several values in one field as chips
To reuse this local React demo, copy all files from the Code tab into the same folder, then import TagInput from ./tag-input.
import { useState } from "react";
import { TagInput } from "@/components/ui/tag-input";
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
export function InviteField() {
const [emails, setEmails] = useState<string[]>([]);
return (
<TagInput
label="Invite people"
value={emails}
onChange={setEmails}
placeholder="name@company.com"
avatar
splitOnSpace
max={10}
normalize={(t) => t.trim().toLowerCase()}
validate={(t) => (EMAIL.test(t) ? true : `“${t}” doesn't look like an email address.`)}
name="emails"
/>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| value | string[] | The chips – makes it controlled | |
| defaultValue | string[] | [] | Starting chips when uncontrolled |
| onChange | (tags: string[]) => void | Called when a chip is added or removed | |
| validate | (tag: string) => true \| string | Checks a value before it becomes a chip; a message keeps it in the field and shows under it | |
| normalize | (tag: string) => string | trim | Cleans a value first, e.g. lowercases an email |
| splitOnSpace | boolean | false | Also split on spaces – right for emails and single words, wrong for labels with spaces |
| max | number | At most this many chips; shows "3/10" under the field | |
| avatar | boolean | false | A small circle with the first letter on every chip, for people |
| label | ReactNode | Shown above the field and tied to its input | |
| hint | ReactNode | A quiet line under the field – a message takes its place while there is one | |
| error | ReactNode | Marks the field invalid from outside and shows the message | |
| optional | boolean | false | Adds "Optional" next to the label |
| placeholder | string | "Add…" | Shown while there are no chips; "Add more…" after the first |
| name | string | Sends the chips with a form, one hidden input per chip | |
| disabled | boolean | false | Dims the field and ignores input |
Chips wrap onto new lines; after about five lines the field scrolls inside
Long values end in … with the full text on hover
Touch screens get taller chips, a bigger × and 16px text; dark mode built in via .dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.