diff --git a/src/uiSelectController.js b/src/uiSelectController.js index d6023d0b9..13604a378 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -388,6 +388,9 @@ uis.controller('uiSelectCtrl', return isDisabled; }; + function _replaceTaggingLabelAndTrim(item, taggingLabel) { + return item.replace(taggingLabel, '').trim(); + } // When the user selects an item with ENTER or clicks the dropdown ctrl.select = function(item, skipFocusser, $event) { @@ -431,7 +434,7 @@ uis.controller('uiSelectCtrl', // if item type is 'string', apply the tagging label } else if ( typeof item === 'string' ) { // trim the trailing space - item = item.replace(ctrl.taggingLabel,'').trim(); + item = _replaceTaggingLabelAndTrim(item, ctrl.taggingLabel); } } } @@ -440,6 +443,8 @@ uis.controller('uiSelectCtrl', ctrl.close(skipFocusser); return; } + } else if ( typeof item === 'string' ) { + item = _replaceTaggingLabelAndTrim(item, ctrl.taggingLabel); } _resetSearchInput(); $scope.$broadcast('uis:select', item); diff --git a/test/select.spec.js b/test/select.spec.js index 7add0ade5..88f32a057 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -161,6 +161,7 @@ describe('ui-select tests', function () { if (attrs.theme !== undefined) { attrsHtml += ' theme="' + attrs.theme + '"'; } if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; } if (attrs.tagging !== undefined) { attrsHtml += ' tagging="' + attrs.tagging + '"'; } + if (attrs.taggingLabel !== undefined) { attrsHtml += ' tagging-label="' + attrs.taggingLabel + '"'; } if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; } if (attrs.title !== undefined) { attrsHtml += ' title="' + attrs.title + '"'; } if (attrs.appendToBody !== undefined) { attrsHtml += ' append-to-body="' + attrs.appendToBody + '"'; } @@ -682,6 +683,15 @@ describe('ui-select tests', function () { expect($(el).scope().$select.selected).toEqual("I don't exist"); }); + it('should not include tagging label if the selection is clicked', function () { + var el = createUiSelect({tagging: true, taggingLabel: ' (default)'}); + clickMatch(el); + + $(el).scope().$select.select("I don't exist (default)", undefined, { type: 'click' }); + + expect($(el).scope().$select.selected).toEqual("I don't exist"); + }); + it('should format new items using the tagging function when the attribute is a function', function () { scope.taggingFunc = function (name) { return { @@ -2730,6 +2740,17 @@ describe('ui-select tests', function () { expect(el.scope().$select.items[1]).toEqual(jasmine.objectContaining({ name: 'Amalie', email: 'amalie@email.com' })); }); + it('should not include tagging label if the selection is clicked', function () { + var el = createUiSelectMultiple({tagging: true, taggingLabel: ' (default)'}); + clickMatch(el); + + $(el).scope().$select.select("I don't exist (default)", undefined, { type: 'click' }); + $(el).scope().$select.select("I also don't exist (default)", undefined, { type: 'click' }); + + expect($(el).scope().$select.selected[0]).toEqual("I don't exist"); + expect($(el).scope().$select.selected[1]).toEqual("I also don't exist"); + }); + it('should have tolerance for undefined values', function () {