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

Commit 246b9e8

Browse files
committed
Merge pull request #678 from angular-ui/fix-tagging-decline
fix(tagging): allow to decline tag creation
2 parents 86689b9 + 30470f0 commit 246b9e8

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/select.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@
393393
if ( ctrl.taggingLabel === false ) {
394394
if ( ctrl.activeIndex < 0 ) {
395395
item = ctrl.tagging.fct !== undefined ? ctrl.tagging.fct(ctrl.search) : ctrl.search;
396-
if ( angular.equals( ctrl.items[0], item ) ) {
396+
if (!item || angular.equals( ctrl.items[0], item ) ) {
397397
return;
398398
}
399399
} else {
@@ -412,6 +412,7 @@
412412
// use tagging function if we have one
413413
if ( ctrl.tagging.fct !== undefined && typeof item === 'string' ) {
414414
item = ctrl.tagging.fct(ctrl.search);
415+
if (!item) return;
415416
// if item type is 'string', apply the tagging label
416417
} else if ( typeof item === 'string' ) {
417418
// trim the trailing space
@@ -675,7 +676,7 @@
675676
if ( ctrl.tagging.fct ) {
676677
newItem = ctrl.tagging.fct( newItem );
677678
}
678-
ctrl.select( newItem, true);
679+
if (newItem) ctrl.select(newItem, true);
679680
});
680681
}
681682
}
@@ -758,6 +759,7 @@
758759
if ( stashArr.filter( function (origItem) { return angular.equals( origItem, ctrl.tagging.fct(ctrl.search) ); } ).length > 0 ) {
759760
return;
760761
}
762+
newItem.isTag = true;
761763
// handle newItem string and stripping dupes in tagging string context
762764
} else {
763765
// find any tagging items already in the ctrl.items array and store them

test/select.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ describe('ui-select tests', function() {
317317
expect(isDropdownOpened(el3)).toEqual(true);
318318
});
319319

320+
it('should allow decline tags when tagging function returns null', function() {
321+
scope.taggingFunc = function (name) {
322+
return null;
323+
};
324+
325+
var el = createUiSelect({tagging: 'taggingFunc'});
326+
clickMatch(el);
327+
328+
$(el).scope().$select.search = 'idontexist';
329+
$(el).scope().$select.activeIndex = 0;
330+
$(el).scope().$select.select('idontexist');
331+
332+
expect($(el).scope().$select.selected).not.toBeDefined();
333+
});
334+
320335
it('should allow tagging if the attribute says so', function() {
321336
var el = createUiSelect({tagging: true});
322337
clickMatch(el);

0 commit comments

Comments
 (0)