• Stars
    star
    1,345
  • Rank 34,927 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Highly customizable React component for inputing tags.

react-tagsinput

NPM version Size Code coverage Download count js-standard-style

Highly customizable React component for inputing tags.

Demo

Example

import React from 'react'
import TagsInput from 'react-tagsinput'

import 'react-tagsinput/react-tagsinput.css'

class Example extends React.Component {
  constructor() {
    super()
    this.state = {tags: []}
  }

  handleChange = (tags) => {
    this.setState({tags})
  }

  render() {
    return <TagsInput value={this.state.tags} onChange={this.handleChange} />
  }
}

Table of Contents

Styling

Look at react-tagsinput.css for a basic style.

Component Interface

Props

value (required)

An array of tags.

onChange (required)

Callback when tags change, gets three arguments tags which is the new tag array, changed which is an array of the tags that have changed and changedIndexes which is an array of the indexes that have changed.

onChangeInput

Callback from the input box, gets one argument value which is the content of the input box. (onChangeInput is only called if the input box is controlled, for this to happen both inputValue and onChangeInput need to be set)

addKeys

An array of keys or key codes that add a tag, default is [9, 13] (Tab and Enter).

currentValue

A string to set a value on the input.

inputValue

Similar to currentValue but needed for controlling the input box. (inputValue is only useful if you use it together with onChangeInput)

onlyUnique

Allow only unique tags, default is false.

validate

Allow only tags that pass this validation function. Gets one argument tag which is the tag to validate. Default is () => true.

validationRegex

Allow only tags that pass this regex to be added. Default is /.*/.

onValidationReject

Callback when tags are rejected through validationRegex, passing array of tags as the argument.

disabled

Passes the disabled prop to renderInput and renderTag, by default this will "disable" the component.

maxTags

Allow limit number of tags, default is -1 for infinite.

addOnBlur

Add a tag if input blurs. Default false.

addOnPaste

Add a tags if HTML5 paste on input. Default false.

pasteSplit

Function that splits pasted text. Default is:

function defaultPasteSplit (data) {
  return data.split(' ').map(d => d.trim())
}
removeKeys

An array of key codes that remove a tag, default is [8] (Backspace).

className

Specify the wrapper className. Default is react-tagsinput.

focusedClassName

Specify the class to add to the wrapper when the component is focused. Default is react-tagsinput--focused.

tagProps

Props passed down to every tag component. Default is:

{
  className: 'react-tagsinput-tag',
  classNameRemove: 'react-tagsinput-remove'
}
inputProps

Props passed down to input. Default is:

{
  className: 'react-tagsinput-input',
  placeholder: 'Add a tag'
}
tagDisplayProp

The tags' property to be used when displaying/adding one. Default is: null which causes the tags to be an array of strings.

renderTag

Render function for every tag. Default is:

function defaultRenderTag (props) {
  let {tag, key, disabled, onRemove, classNameRemove, getTagDisplayValue, ...other} = props
  return (
    <span key={key} {...other}>
      {getTagDisplayValue(tag)}
      {!disabled &&
        <a className={classNameRemove} onClick={(e) => onRemove(key)} />
      }
    </span>
  )
}
renderInput

Render function for input. Default is:

function defaultRenderInput (props) {
  let {onChange, value, addTag, ...other} = props
  return (
    <input type='text' onChange={onChange} value={value} {...other} />
  )
}

Note: renderInput also receives addTag as a prop.

renderLayout

Renders the layout of the component. Takes tagElements and inputElement as args. Default is:

function defaultRenderLayout (tagElements, inputElement) {
  return (
    <span>
      {tagElements}
      {inputElement}
    </span>
  )
}
preventSubmit

A boolean to prevent the default submit event when adding an 'empty' tag. Default: true

Set to false if you want the default submit to fire when pressing enter again after adding a tag.

Methods

focus()

Focus on input element.

blur()

Blur input element.

accept()

Try to add whatever value is currently in input element.

addTag(tag)

Convenience method that adds a tag.

clearInput()

Clears the input value.

Contributors

Changelog

License


MIT Licensed

More Repositories

1

melody

🎶 Minimalist websocket framework for Go
Go
3,685
star
2

reactpack

📦 build your react apps with one command and one `npm i`.
JavaScript
982
star
3

melody-jsnes

📺 Multiplayer NES through the magic of WebSockets and Go.
HTML
767
star
4

react-ab

Simple declarative and universal A/B testing component for React.
JavaScript
432
star
5

react-social

Simple React components for social buttons (Facebook, Twitter and Pinterest) and social counts.
HTML
177
star
6

go-imageupload

🔳 Gracefully handle image uploading and thumbnail creation.
Go
139
star
7

iso-3166-2.js

Lookup information about ISO-3166-2 subdivisions
JavaScript
108
star
8

express-chrome-logger

Debug your express app using the Chrome console.
JavaScript
83
star
9

pocketplace

🎆 Draw pixels on a canvas with friends.
Go
79
star
10

scrollparent.js

A function to get the scrolling parent of a html element.
HTML
58
star
11

node-csp

Communicating sequential processes for node.
JavaScript
55
star
12

iso-3166-2.json

JSON file of ISO 3166-2 subdivisions.
JavaScript
54
star
13

greasyphone

📱 🎮 Play NES using smartphones as joypads
JavaScript
45
star
14

viewdiff

📒 view your diffs in a separate window.
JavaScript
41
star
15

bf2c

🐛 Not the smallest compiler ever
JavaScript
20
star
16

socialcount.js

Tiny javascript library for getting social share counts. Pinterest, Facebook, Twitter and Google Plus supported.
JavaScript
17
star
17

cpp-csp

Minimalistic header-only library for channels and CSP (Communicating sequential process) in C++11.
C++
15
star
18

ethereum-web1-guestbook

📓 A Web 1.0 guestbook on Web 3.0
HTML
15
star
19

mainthread

Run functions in Go's main thread.
Go
14
star
20

eselement

DOM-like wrapper around the Javascript AST
JavaScript
6
star
21

tsreflect

Flexible reflection based TypeScript type generator for Go types that can be marshalled with `encoding/json`.
Go
6
star
22

capreq

Capture HTTP Requests using gopacket and libpcap
Go
6
star
23

hnplain

output Hacker News in plain text
Python
6
star
24

react-bus

Event emitter designed for communication between react components.
JavaScript
5
star
25

bf2asmjs

A simple brainfuck compiler targeting asm.js
Python
5
star
26

spawn.js

Tiny javascript library for spinning up one off web workers.
JavaScript
3
star
27

node-crcaptcha

Node package for using the Civil Rights Captcha.
JavaScript
2
star
28

tiny-xss-scanner

Lighweight xss scanner
Python
2
star
29

olapass

Stateless password manager.
JavaScript
1
star
30

request-animation-loop

As setInterval is to setTimeout, requestAnimationLoop is to requestAnimationFrame.
JavaScript
1
star
31

cpp-project-template

A small template for C++11 projects using tup and clang.
C++
1
star
32

generator-react-umd

A Yeoman generator that generates a minimalistic scaffold for a ES6 React component wrapped with the UMD.
JavaScript
1
star
33

gin-csrf

CSRF protection middleware for Go web framework Gin
Go
1
star
34

deepmodules

Analyze depth of Go packages
Go
1
star
35

node-mysql-slowlog

Application level slow log for mysql connections. Good for debugging slow queries when you haven't turned on or have access to the database slow log.
JavaScript
1
star