Skip to content

TagInput

A box where each word typed becomes a chip. Enter or a comma adds it, Backspace takes out the last one, and a pasted list is split into chips.

import { TagInput } from '@sypnia/ui/react'

On this page
  1. Words into chips
  2. Tidied and limited
  3. States
  4. When to use it
  5. For everyone
  6. Props

Words into chips

Type an address and press Enter, or paste «a.com, b.com». Backspace removes the last one.

sypnia.comapp.sypnia.com

2 sites can call this project from a browser. Separate them with Enter or a comma.

Code
import { useState } from 'react';
import { Field, TagInput } from '@sypnia/ui/react';

export default function Basic() {
  const [origins, setOrigins] = useState(['sypnia.com', 'app.sypnia.com']);
  return (
    <Field label="Allowed origins" hint={`${origins.length} sites can call this project from a browser. Separate them with Enter or a comma.`}>
      <TagInput value={origins} onChange={setOrigins} placeholder="example.com" />
    </Field>
  );
}

Tidied and limited

Each label is made lowercase with dashes before it is added, and there can be five at most.

productionbilling

Up to five. «Needs Review» becomes needs-review.

Code
import { Field, slugify, TagInput } from '@sypnia/ui/react';

export default function Tidy() {
  return (
    <Field label="Labels" hint="Up to five. «Needs Review» becomes needs-review.">
      <TagInput defaultValue={['production', 'billing']} max={5} normalize={(tag) => slugify(tag).replace(/-+$/, '')} placeholder="Add a label" />
    </Field>
  );
}

States

Empty with an error that says how to fix it, and disabled.

Add at least one address, then press Enter.

sypnia.comlocalhost:4410

Only the owner can change them.

Code
import { Field, FormGrid, TagInput } from '@sypnia/ui/react';

export default function States() {
  return (
    <FormGrid>
      <Field label="Invite by mail" error="Add at least one address, then press Enter.">
        <TagInput placeholder="name@company.com" />
      </Field>
      <Field label="Allowed origins" hint="Only the owner can change them.">
        <TagInput disabled defaultValue={['sypnia.com', 'localhost:4410']} />
      </Field>
    </FormGrid>
  );
}

When to use it

Use it when

  • The person writes their own short values, several of them: allowed addresses, labels, mail addresses to invite.
  • People will paste lists copied from elsewhere: commas, semicolons and line breaks all split.

Do not use it when

  • The values come from a known list: MultiSelect, so nobody has to remember how they are written.
  • It is one long text: Textarea.
  • Order matters and items are edited often: a list with rows.

For everyone

  • It is a plain text box: Enter or a comma turns what was typed into a chip; leaving the box does too, so nothing typed is lost.
  • Backspace in an empty box removes the last chip. Each chip also has its own button, named «Remove» plus the tag.
  • After each change a quiet message says how many there are.
  • A tag written twice is added once.
  • Inside a Field it takes the label, the hint and the error by itself; alone, give it an aria-label.

Props

TagInputTypeDefault
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
className string
defaultValue string[]
disabled boolean
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
max

The most tags it takes.

number
normalize

Tidies each tag before it is added (lowercase, no spaces…). Return an empty text to refuse it.

((tag: string) => string)
onChange ((tags: string[]) => void)
placeholder string
value string[]
  1. Words into chips
  2. Tidied and limited
  3. States
  4. When to use it
  5. For everyone
  6. Props

    Search pages, sections and terms