Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Allow discard tag when tagging function returns null #663

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -668,7 +671,9 @@
if ( ctrl.tagging.fct ) {
newItem = ctrl.tagging.fct( newItem );
}
ctrl.select( newItem, true);
if (newItem) {
ctrl.select(newItem, true);
}
});
}
}
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down