diff --git a/src/select.js b/src/select.js index f8906c235..42812d936 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,9 @@ // 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 @@ -668,7 +671,9 @@ if ( ctrl.tagging.fct ) { newItem = ctrl.tagging.fct( newItem ); } - ctrl.select( newItem, true); + if (newItem) { + ctrl.select(newItem, true); + } }); } } @@ -728,11 +733,15 @@ stashArr = stashArr.slice(1,stashArr.length); } newItem = ctrl.tagging.fct(ctrl.search); - newItem.isTag = true; + if (!newItem) { + return; + } + // verify the the tag doesn't match the value of an existing item 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 32271d606..725f562a6 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -311,6 +311,21 @@ describe('ui-select tests', function() { }); }); + 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(); + }); + // See when an item that evaluates to false (such as "false" or "no") is selected, the placeholder is shown https://github.com/angular-ui/ui-select/pull/32 it('should not display the placeholder when item evaluates to false', function() { scope.items = ['false'];