VikramN / Re-Tag

Reactjs Tag Component
MIT License
21 stars 2 forks source link

Disallow duplicate tags? #3

Closed bebraw closed 9 years ago

bebraw commented 9 years ago

Ie. if I hit "foobar" twice, I end up with two "foobar" tags. That's not very convenient. :)

VikramN commented 9 years ago

Yes, useful. I'll provide an option to ignore dups. Thanks

VikramN commented 9 years ago

@bebraw I've decided the best way to handle this is at model level. You can provide option in the model ctor to disallow duplicates, and you can provide your custom function as well. Check out the demo and let me know. Thanks

bebraw commented 9 years ago

@VikramN Looks like a nice API. You can simplify this if you want, though. Demo:

// Current API
var tags1 = new TagCollection({
    duplicates : {
        // Dups are allowed by default
        allow : false,

        // If not provided, we use ===
        // tags will be array of { tag: , key: }
        contains : function(item, tags) {
            var lTag = item.toLowerCase();
            for(var i =0; i < tags.length; i++) {
                if(tags[i] && tags[i].tag.toLowerCase() === lTag){
                    return true;
                }
            }
            return false;
        }
    }
});

// Improved API
var tags1 = new TagCollection({
    // by default we allow every tag. you can check against duplicates etc. here
    allowTag: function(item, tags) {
        var lTag = item.toLowerCase();
        for(var i =0; i < tags.length; i++) {
            if(tags[i] && tags[i].tag.toLowerCase() === lTag){
                return true;
            }
        }
        return false;
    }
});

This will eliminate the flag and keeps things simpler. You can provide a couple of helpers with the library for common tasks (ie. detecting duplicates). Then you would just do allowTag: ReTag.disallowDuplicates and so on.

VikramN commented 9 years ago

Hey. Thanks for the suggestion. I've added generic onAdd and onDelete options. Let me know what you think.

bebraw commented 9 years ago

@VikramN Looking good. :+1: