/react-tagsinput

A simple react component for inputing tags.

Primary LanguageJavaScriptMIT LicenseMIT

react-tagsinput

NPM version Build Status Coverage Status Dependency Status Size Download Count js-standard-style

Highly customizable React component for inputing tags.

Table of Contents

Demo

Demo

Install

npm install react-tagsinput --save
bower install react-tagsinput --save

Example

import TagsInput from 'react-tagsinput'

import 'react-tagsinput/react-tagsinput.css' // If using WebPack and style-loader.

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} />
  }
}

FAQ

How do I make the input dynamically grow in size?

Install react-input-autosize and change the renderInput prop to:

function autosizingRenderInput (props) {
  let {onChange, value, ...other} = props
  return (
    <AutosizeInput type='text' onChange={onChange} value={value} {...other} />
  )
}
How do I add auto suggestion?

Use react-autosuggest and change the renderInput prop to something like:

function autosuggestRenderInput (props) {
  return (
    <Autosuggest
      ref={props.ref}
      suggestions={suggestions}
      shouldRenderSuggestions={(value) => value && value.trim().length > 0}
      getSuggestionValue={(suggestion) => suggestion.name}
      renderSuggestion={(suggestion) => <span>{suggestion.name}</span>}
      inputProps={props}
      onSuggestionSelected={(e, {suggestion}) => {
        this.refs.tagsinput.addTag(suggestion.name)
      }}
    />
  )
}

A working example can be found in example/index.js.

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.

addKeys

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

currentValue

A string to set a value on the input.

onlyUnique

Allow only unique tags, default is false.

validationRegex

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

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. Defualt is: {className: 'react-tagsinput-tag', classNameRemove: 'react-tagsinput-remove'}.

inputProps

Props passed down to input. Default is: {className: 'react-tagsinput-input'}

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, onRemove, getTagDisplayValue, ...other} = props
  return (
    <span key={key} {...other}>
      {getTagDisplayValue(tag)}
      <a 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 tagComponents and inputComponent as args. Default is:

function defaultRenderLayout (tagComponents, inputComponent) {
  return (
    <span>
      {tagComponents}
      {inputComponent}
    </span>
  )
}

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.

Styling

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


MIT Licensed