Coder Social home page Coder Social logo

react-tagsinput's Introduction

react-tagsinput

npm version Build Status Coverage Status Dependency Status Download Count

Simple React component for inputing tags.

Demo

Demo

Install

npm install react-tagsinput --save

or

bower install react-tagsinput --save

Example

Controlled usage:

var TagsInput = require('react-tagsinput');

var App = React.createClass({
  mixins: [React.addons.LinkedStateMixin],

  getInitialState: function () {
    return { tags: [] };
  },

  saveTags: function () {
    console.log('tags: ', this.refs.tags.getTags().join(', '));
  },

  render: function () {
    return (
      <div>
        <TagsInput ref='tags' valueLink={this.linkState('tags')} />
        <button onClick={this.saveTags}>Save</button>
      </div>
    );
  }
});

Uncontrolled usage:

var TagsInput = require('react-tagsinput');

var App = React.createClass({
  saveTags: function () {
    console.log('tags: ', this.refs.tags.getTags().join(', '));
  },

  render: function () {
    return (
      <div>
        <TagsInput ref='tags' />
        <button onClick={this.saveTags}>Save</button>
      </div>
    );
  }
});

More examples in example/.

API

Props

value

An array of tags. This prop or valueLink is required.

valueLink

A ReactLink object.

defaultValue

Initialize the component with a value. This is only used when value is null (i.e when the component is uncontrolled.)

placeholder

Placeholder text for the add a tag input, default is "Add a tag".

classNamespace

Namespace for CSS classes, default is react i.e CSS classes are react-tagsinput.

addOnBlur

Boolean whether a tag should be added when the input field blurs, default is true.

validate or validateAsync

A function which returns true if a tag is valid, default function returns true for non-empty strings and unique tags. The validation is asynchronous if the validate function takes two arguments tag and a callback cb or if the prop validateAsync is set. validateAsync is always asynchronous and takes two arguments tag and cb.

transform

A function which transforms a tag before it is added, the default function trims the tag of whitespaces.

addKeys

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

removeKeys

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

onChange

Callback when the tag input changes, the argument is an array of the current tags and the tag which was added or removed.

onChangeInput

Callback when the input changes, the argument is the value of the input.

onBlur

Callback when input field blurs.

onTagAdd

Callback when a tag is added, argument is the added tag.

onTagRemove

Callback when a tag is removed, argument is the removed tag.

Methods

focus()

Focus on the tag input.

getTags()

Returns an array of the current tags.

addTag(tag)

Adds a tag.

removeTag(tag)

Removes a tag.

Styles

Look at react-tagsinput.css for an idea on how to style this component.

Addendum: Input completion

An example of how to add input completion to the TagsInput component can be found in examples/completion.html.

Completion Demo

Addendum: Components instead of strings as tags

An example of how to use React components instead of strings as tags can be found in examples/component.html.

Contributors

  • Ola Holmström (@olahol)
  • Dmitri Voronianski (@voronianski)
  • Artem Vovsya (@avovsya)
  • scott c (@scoarescoare)
  • junk (@jedverity)
  • Buz Carter (@buzcarter)

MIT Licensed

react-tagsinput's People

Contributors

avovsya avatar voronianski avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.