sliptree / bootstrap-tokenfield

A jQuery tag/token input plugin for Twitter's Bootstrap, by the guys from Sliptree
http://sliptree.github.io/bootstrap-tokenfield/
Other
861 stars 238 forks source link

Autocomplete keypress enter create duplicate tokens #308

Open dmedyk opened 7 years ago

dmedyk commented 7 years ago

Hello, when i selected item via autocmplete menu with 'enter' key press, i have 2 tokens created. i fixed this with:

line 490 in keydown function

// Create token
if (this.$input.is(document.activeElement) && this.$input.val().length || this.$input.data('edit')) {
     return this.createTokensFromInput(e, this.$input.data('edit'));
}

to

// Create token
if (this.$input.is(document.activeElement) && this.$input.val().length && this.$input.data('edit')) {
    return this.createTokensFromInput(e, this.$input.data('edit'));
}

but i don't know if i break the edit feature. Can you tell me if it's correct ?

Thank you

leocaseiro commented 7 years ago

I confirm this issue.

Sorry for tagging you @ragulka, but doesn't it happen to you as well?

emdena commented 6 years ago

This issue still not solve yet? Is there any proper fix? Fix provided above works but it produce bug with tab key not accepting input.

SemirLab2 commented 6 years ago

Yep, this fix break a keyboard tag input

this.$input.val().length || this.$input.data('edit')

input work fine, but select from list and press enter - call a double token creation.

this.$input.val().length && this.$input.data('edit')

select from list work fine, but handy input is break.

SemirLab2 commented 6 years ago
$('#tokenfield').on('tokenfield:createtoken', function (event) {
    var existingTokens = $(this).tokenfield('getTokens');
    $.each(existingTokens, function(index, token) {
        if (token.value === event.attrs.value)
            event.preventDefault();
    });
});

Work fine

Palzer commented 5 years ago

@SemirLab2 that doesn't work if they already have the tag and want to add it again for some reason.