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

Fix: remove tagging label when clicked in dropdown #1943

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '"'; }
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2730,6 +2740,17 @@ describe('ui-select tests', function () {
expect(el.scope().$select.items[1]).toEqual(jasmine.objectContaining({ name: 'Amalie', email: '[email protected]' }));
});

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 () {

Expand Down