Git Product home page Git Product logo

tagger's Introduction

  _____
 |_   _|___ ___ ___ ___ ___
   | | | .'| . | . | -_|  _|
   |_| |__,|_  |_  |___|_|
           |___|___|   version 0.6.2

npm

Tag Editor widget in JavaScript

Online Demo

Installation

npm install @jcubic/tagger

or

yarn add @jcubic/tagger

Usage

tagger(document.querySelector('[name="tags"]'), {allow_spaces: false});

Multiple inputs can be created by passing a NodeList or array of elements (eg. document.querySelectorAll()). If only one element is contained in the list then tagger will return the tagger instance, an array of tagger instances will be returned if the number of elements is greater than 1.

Usage with React

Tagger can easily be used with ReactJS.

import { useRef, useState, useEffect } from 'react'
import tagger from '@jcubic/tagger'

const App = () => {
    const [tags, setTags] = useState([]);
    const inputRef = useRef(null);

    useEffect(() => {
        const taggerOptions = {
            allow_spaces: true,
        };
        tagger(inputRef.current, taggerOptions);
        onChange();
    }, [inputRef]);

    const onChange = () => {
        setTags(tags_array(inputRef.current.value));
    };

    return (
        <div className="app">
            <input type="text" ref={inputRef} onChange={onChange} defaultValue="charles, louis, michel" />
            <br/>
            <ul>
                {tags.map((tag, index) => <li key={`${tag}-${index}`}>{tag}</li>)}
            </ul>
        </div>
    )
}

function tags_array(str) {
    return str.split(/\s*,\s*/).filter(Boolean);
}

export default App

See demo in action on CodePen.

API

methods:

  • add_tag(string): boolean
  • remove_tag(string): booelan
  • complete(string): void

Options:

  • wrap (default false) allow tags to wrap onto new lines instead of overflow scroll
  • allow_duplicates (default false)
  • allow_spaces (default true)
  • add_on_blur (default false)
  • completion {list: string[] | function(): Promise(string[])|string[], delay: miliseconds, min_length: number}
  • link function(name): string|false it should return what should be in href attribute or false
  • tag_limit number (default -1) limit number of tags, when set to -1 there are no limits
  • placeholder string (default unset) If set in options or on the initial input, this placeholder value will be shown in the tag entry input
  • filter function(name): string it should return the tag name after applying any filters (eg String.toUpperCase()), empty string to filter out tag and prevent creation.

NOTE: if you're familiar with TypeScript you can check the API by looking at TypeScript definition file:

tagger.d.ts

Press

License

Copyright (c) 2018-2024 Jakub T. Jankiewicz
Released under the MIT license

tagger's People

Contributors

bulldy80 avatar davidworkman9 avatar jcubic avatar lucasnetau avatar mheiman avatar nuclear06 avatar sheabunge avatar sven-ve avatar trex22 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  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

tagger's Issues

Add more documentation about completion/link/filter to README

I want to use a tagging component for adding keywords to an item. There can be thousands of keywords,
so I want to have:

  • the user start typing a matching keyword
  • perform a query, using the currently typed match, to a web server after a short delay
  • get a json response with suitable matches
  • allow the user to choose a match

Thanks.

Create directory for files

Having a well-structured folder for Storing CSS styles, and javascript in different file makes the project more concise and organized.

Make links customizable

When badge for tags are added, links point to /tag/<tagname>, but we can't choose link prefix like:

my/route/tag/tagname or something like that. Or let user deactivate the link by href="# or use a span instead ?

tagger(input, {
    allow_duplicates: false,
    allow_spaces: true,
   use_links: false,
});

How does filter return error string to user?

I have a filter and I return "" to prevent the current string from being used as a tag. How do I notify the
user of the issue? Do I create a <span role="status"></span> after the input and set the innerText in
the filter?

option to specify the delimiter

Currently the library only allows comma as delimiter. It would be great to have an option to set the delimiter (for instance semi-colon) when initializing.

Add events

Events are required for proper use in React to have proper binding.

  • Add change event
  • update React example to display all tags while they are changing

Values Not Adding to main Input Field

Hi @jcubic ,

When we type in the field and without pressing comma /enter/space directly taking mouse out of the field then its not appending the value to main input as well its not getting convert like tag.

I tried to add the event on blur form tagger-new input but while firing keypress event, its not allowing because main field converting to hidden/readonly .

image

How do you get the values out?

For example, I want to listen to the enter event and get the the strings out:

let tagger = tagger(document.querySelector('[name="tags"]'), {allow_spaces: false});

tagger.complete((results) {
  console.log(results); // []string
});

But that doesn't really work

Autocomplete only works after Ctrl+Space

Hi,

Thanks for this library.

Autocomplete only works after Ctrl+Space. Is there an option to make it work like the default behaviour of datalist (it doesn't need any keystroke to provide auto complete suggestions natively) ?

Add unit tests

Or more like integration tests for the whole component.

remove jquery from the codepen?

it is a little confusing to say 'no dependencies' and then have your example demo code script-load jquery when the demo doesn't actually need it.

otherwise, looks pretty solid.

On Removing Backbutton

On Removing the by back button, string is not getting removed from the main associated input field. Can you please help me out

Safari Loses Focus After 2 Key Presses

Firefox doesn't have this problem but Safari does. For some reason, I can only type two characters in the tag box before I lose focus and have to click inside the input box again.

option to force case

I was suggested to add this option to ignore case. The feature will be breaking change because it will force to ignore the case by default.

options will be maybe force_lowercase that is true by default.

Add destroy method

Add code that will bring back the original input and clean up everything.

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.