Code Monkey home page Code Monkey logo

react-categorized-tag-input's Introduction

react-categorized-tag-input Build Status

React.js component for making tag autocompletion inputs with categorized results with no dependencies and 10KB minified.

Note: v1.x versions only work with react 0.14.0 or higher. For compatibility with previous versions use the v0.x versions.

Install

npm install react-categorized-tag-input

Include

With webpack, browserify, etc (recommended way):

import TagInput from 'react-categorized-tag-input';

or (if you are not yet using ES2015, which you should)

var TagInput = require('react-categorized-tag-input');

With other tools: Just include the categorized-tag-input.js file in your HTML and your good to go. It is already minified.

If you want to use the default style you have to include the categorized-tag-input.css file. It is plain CSS, no LESS, no SASS, no dependencies.

As a personal suggestion, I recommend webpack. You would just need to require('node_modules/react-categorized-tag-input/categorized-tag-input.css');.

Usage

To use this component we will assume the imported variable name is TagInput. The props are very straightforward.

Name Type Description default
addNew boolean If true, allows the user to create new tags that are not set in the dataset true
categories Array of objects Dataset with categories and items Required
transformTag function A function that will receive the tag object (which has at least keys title and category) and must return a string. This string will be displayed to the user. Useful if you need to apply a transformation to the tags. (tag) => tag.title
value Array of tags. Tags are objects with (at least) keys title and category, where category is the id of a category in the array passed in for the categories prop Array with the initial tags []
onBlur function Callback for when the input loses focus noop
onChange function Callback for when the input changes. It does not get an event as parameter, it gets the array of tags after the change. noop
placeholder string A placeholder will be given in the input box. Add a tag
getTagStyle function A function from the tag text (string) to an object with any or all of the following keys: base, content and delete. The values are React style objects. This example renders 1-letter long tags in red: text => text.length === 1 ? {base: {color: "red"}} : {} () => ({})
getCreateNewText function A function that returns the text to display when the user types an unrecognized tag, given a title and text. (title, text) => Create new ${title} "${text}"
getTagStyle function A function from the tag (object with at least the keys title and category) to an object with any or all of the following keys: base, content and delete. The values are React style objects. This example renders 1-letter long tags in red: text => text.length === 1 ? {base: {color: "red"}} : {} () => ({})

The tag object

Tag objects look like this:

{
  title: 'String to used to identify the tag',
  category: 'id of the category for the tag'

}

The category object

The category object for the dataset looks like this:

{
  id: 'string or number identifying the category',
  type: 'word to describe the category. Will be used on the create new tag button. E.g: "Create new animal foo"',
  title: 'Title displayed on the category row',
  items: ['Array', 'With', 'Tags'],
  single: optional boolean. If is true the row will be treated as one-valued row. It does not have the option of adding new items to the category
}

Create the object

<TagInput categories={myCategories} addNew={true}
    transformTag={tagTransformer}
    onBlur={onBlur}
    onChange={onChange} />

Get the value

You can either use the onChange callback or use the value() method of the component. It will return the existing tags as an array of strings.

How to use the rendered component

When you click on the input you will be able to write on it. Right away, a panel with the categories with matches will be shown. You can navigate through categories and options using the arrow keys to change the selected tag. Backspace when there is nothing written erases the last tag. Enter and , add the currently selected tag to the input.

react-categorized-tag-input's People

Contributors

kthurimella avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-categorized-tag-input's Issues

error when placeholder attribute undefined

Hey, great work on this component -- thanks! Just wanted to let you know it's throwing this error for me if you don't provide a placeholder attribute:

Uncaught TypeError: Cannot read property 'length' of undefined when trying to read this.props.placeholder.length.

ERROR TypeError: undefined is not an object (evaluating 'i.bool')

Steps to reproduce:

  1. npm install react-caetgorized-tag-input
  2. import TagInput from 'react-categorized-tag-input';

That's it. I get the error: ERROR TypeError: undefined is not an object (evaluating 'i.bool')

grepping 'i.bool' in source code of react-categorized-tag-input finds only on 'categorized-tag-input.js' at 3 places.

I am curious, if no other people faced it !!
My package.json

"react": "16.13.1",
"react-categorized-tag-input": "^2.1.2",
"react-native": "0.63.2",

Enable tags to have distinct styles

I need to apply colors to specific tags (some red, some blue, for example).
Do you have a suggestion for the cleanest way to do this? I can do a PR.
Proposal: an optional getTagStyle prop that is a function from tagName to a style object, which is then applied to the .cti__tag div and its children.

Hide items from Autocomplete once selected

Hi,

When I add an "tag" item from the autocomplete, it does not get removed from the autocomplete list.

This means it allows duplicates of the same tag to be created.

Is this a bug? Or is there a way to improve this?

how to add new fields tag objects.

Tag object
{
title: 'String to used to identify the tag',
category: 'id of the category for the tag'
}

how to add new fields in tag object.for example i want to add new fields for "Link" in the tag object.how to solve this

Warning: React.createElement: type should not be null && Error: Element type is invalid:

First off thanks for this very well-written component for tag input. I'm struggling to get this working with my project, I've verified the version on npm works with a greenfields project (tested using https://github.com/unicorn-standard/starter-kit) but something in my project is leading to these two errors in the browser console and the app failing to run anytime I include a in my render functions:

Error1: Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of InputTest.
Error2: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of InputTest.

I'm using webpack, ES6, TS2, React 0.15/React-Router/React-DOM (I tried with 0.14 as well but same error). Any ideas on what I'm doing wrong? Google results indicate a myriad of possible causes none of which seem to have resolved the issue for me, thanks in advance for any help!

available tags

Hello! How a user can see a set of available tags designed for quick selection without typing?

Make Tab key advance to next form field

Thanks for sharing this great work!!

For accessibility, we want the key to take the user to the next form field when the user is not in the middle of typing something. Would you accept a PR for that?

Request for more examples

Hi,

Great work on this- looks amazing.

However, I haven't been able to integrate it into my own project.

I have written:

import TagInput from 'react-categorized-tag-input';

render: function(){
        return (
        <TagInput categories={myCategories} addNew={true}
    transformTag={tagTransformer}
    onBlur={onBlur}
    onChange={onChange} />
        <Jumbotron>

This gave me the error myCategories is not defined.

So I tried defining myCategories- but this led to the error ReferenceError: tagTransformer.

var myCategories = [
{
  title: 'String to used to identify the tag',
  category: 'id of the category for the tag'

}
];

Could you please provide some direct for usage in a project? Thanks

Use as controlled component

TagInput acts like an uncontrolled component. That is, it maintains its own state, and the onChange callback just lets the listener know "hey, I changed."

We'd like to use it as a controlled component, so we can have more control over its state. Our immediate need is to dedupe tags, but I can see other uses as well.

The React convention is to have value be dynamic (like input.value) and defaultValue act like the value HTML attribute. This is how input works. That probably isn't an option for TagInput because it would break backwards compatibility.

I can PR for this, but would like your feedback on the best API design.

Option 1

  • If value never changes, treat it like defaultValue. If it changes, treat it like React.Dom.input's value prop. This has the upside of more-closely following React conventions, but the downside of being potentially confusing.

Option 2

  • Add a defaultValue prop
  • Setting controlled={true} makes the component a controlled component

Update to react ^15.1.0?

I really like your component! Would it be possible to upgrade it to depend on react-15? As this is the last missing piece before I can upgrade my project to react-15. Thanks!

Cannot read property 'bool' of undefined

Source:

import React from "react";
import ReactDOM from "react-dom";
import TagInput from 'react-categorized-tag-input';

import "./styles.css";

function App() {
  return (
    <div className="App">
      <TagInput addNew={false}/>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
TypeError
Cannot read property 'bool' of undefined
Object.eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:1057
t
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:495
Object.eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:9613
t
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:495
Object.eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:3101
t
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:495
Object.eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:726
t
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:495
eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:582
eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:587
eval
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:181
evaluate
https://xjw6ywqyz.codesandbox.io/node_modules/react-categorized-tag-input/categorized-tag-input.js:1:365

https://codesandbox.io/s/xjw6ywqyz

Update to react ^14.0.1?

This lib is great - but if you use it with react ^14.0.1 you get the error

Warning: ReactDOMComponent: Do not access .getDOMNode() of a DOM node; instead, use the node directly.

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.