diff --git a/src/select.js b/src/select.js index 4cc483dd5..d08a7ce3b 100644 --- a/src/select.js +++ b/src/select.js @@ -393,7 +393,7 @@ if ( ctrl.taggingLabel === false ) { if ( ctrl.activeIndex < 0 ) { item = ctrl.tagging.fct !== undefined ? ctrl.tagging.fct(ctrl.search) : ctrl.search; - if ( angular.equals( ctrl.items[0], item ) ) { + if (!item || angular.equals( ctrl.items[0], item ) ) { return; } } else { @@ -412,6 +412,7 @@ // use tagging function if we have one if ( ctrl.tagging.fct !== undefined && typeof item === 'string' ) { item = ctrl.tagging.fct(ctrl.search); + if (!item) return; // if item type is 'string', apply the tagging label } else if ( typeof item === 'string' ) { // trim the trailing space @@ -675,7 +676,7 @@ if ( ctrl.tagging.fct ) { newItem = ctrl.tagging.fct( newItem ); } - ctrl.select( newItem, true); + if (newItem) ctrl.select(newItem, true); }); } } @@ -758,6 +759,7 @@ if ( stashArr.filter( function (origItem) { return angular.equals( origItem, ctrl.tagging.fct(ctrl.search) ); } ).length > 0 ) { return; } + newItem.isTag = true; // handle newItem string and stripping dupes in tagging string context } else { // find any tagging items already in the ctrl.items array and store them diff --git a/test/select.spec.js b/test/select.spec.js index 4a8482576..8be66afa8 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -317,6 +317,21 @@ describe('ui-select tests', function() { expect(isDropdownOpened(el3)).toEqual(true); }); + it('should allow decline tags when tagging function returns null', function() { + scope.taggingFunc = function (name) { + return null; + }; + + var el = createUiSelect({tagging: 'taggingFunc'}); + clickMatch(el); + + $(el).scope().$select.search = 'idontexist'; + $(el).scope().$select.activeIndex = 0; + $(el).scope().$select.select('idontexist'); + + expect($(el).scope().$select.selected).not.toBeDefined(); + }); + it('should allow tagging if the attribute says so', function() { var el = createUiSelect({tagging: true}); clickMatch(el);