React Tag Input

6.10.6 · active · verified Sun Apr 19

React Tag Input is a comprehensive and user-friendly tagging component designed for React applications, currently stable at version 6.10.6. It offers robust features such as autocomplete based on a suggestion list, full keyboard and mouse navigation, drag-and-drop tag reordering powered by `react-dnd`, and in-place tag editing. The library is actively maintained with frequent patch and minor releases, and version 7 is currently in preparation. A key differentiator is its focus on accessibility and a user experience inspired by email 'To' fields, providing a flexible solution for managing tag inputs in various UI contexts.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `react-tag-input` with initial tags and suggestions, handling tag additions, deletions, updates, drags, and clicks, along with a 'clear all' functionality.

import React from 'react';
import { createRoot } from 'react-dom/client';
import { WithContext as ReactTags, SEPARATORS } from 'react-tag-input';
import type { Tag } from 'react-tag-input'; // Assuming type Tag is exported directly

const COUNTRIES = [
  'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda',
  'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain',
  'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan',
];

const suggestions = COUNTRIES.map((country) => ({
  id: country,
  text: country,
  className: '',
}));

const KeyCodes = {
  comma: 188,
  enter: [10, 13],
};

const App = () => {
  const [tags, setTags] = React.useState<Array<Tag>>([
    { id: 'Thailand', text: 'Thailand', className: '' },
    { id: 'India', text: 'India', className: '' },
    { id: 'Vietnam', text: 'Vietnam', className: '' },
    { id: 'Turkey', text: 'Turkey', className: '' },
  ]);

  const handleDelete = (index: number) => {
    setTags(tags.filter((_, i) => i !== index));
  };

  const onTagUpdate = (index: number, newTag: Tag) => {
    const updatedTags = [...tags];
    updatedTags.splice(index, 1, newTag);
    setTags(updatedTags);
  };

  const handleAddition = (tag: Tag) => {
    setTags((prevTags) => [...prevTags, tag]);
  };

  const handleDrag = (tag: Tag, currPos: number, newPos: number) => {
    const newTags = tags.slice();
    newTags.splice(currPos, 1);
    newTags.splice(newPos, 0, tag);
    setTags(newTags);
  };

  const handleTagClick = (index: number) => {
    console.log('The tag at index ' + index + ' was clicked');
  };

  const onClearAll = () => {
    setTags([]);
  };

  return (
    <div className="app">
      <h1>React Tags Example</h1>
      <p>Start typing to see suggestions. Use Enter or Comma to add tags. Click to edit, drag to reorder.</p>
      <ReactTags
        tags={tags}
        suggestions={suggestions}
        handleDelete={handleDelete}
        handleAddition={handleAddition}
        handleDrag={handleDrag}
        handleTagClick={handleTagClick}
        onTagUpdate={onTagUpdate}
        onClearAll={onClearAll}
        delimiters={KeyCodes.enter.concat(KeyCodes.comma)}
        placeholder="Add new tags..."
        inputFieldPosition="bottom"
        autocomplete
        allowUnique
      />
      <div style={{ marginTop: '20px' }}>
        <h3>Current Tags:</h3>
        <ul>
          {tags.map(tag => (
            <li key={tag.id}>{tag.text}</li>
          ))}
        </ul>
      </div>
    </div>
  );
};

const container = document.getElementById('root');
if (container) {
  const root = createRoot(container);
  root.render(<App />);
} else {
  console.error('Root element not found');
}

view raw JSON →