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

fix(uiSelectController): Select by click on non-multiple tagging (bis) #1727

Merged
merged 2 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ uis.controller('uiSelectCtrl',
function _isItemDisabled(item) {
return disabledItems.indexOf(item) > -1;
}

ctrl.isDisabled = function(itemScope) {

if (!ctrl.open) return;

var item = itemScope[ctrl.itemProperty];
var itemIndex = ctrl.items.indexOf(item);
var isDisabled = false;

if (itemIndex >= 0 && (angular.isDefined(ctrl.disableChoiceExpression) || ctrl.multiple)) {

if (item.isTag) return false;
Expand All @@ -360,7 +360,7 @@ uis.controller('uiSelectCtrl',
if (!isDisabled && angular.isDefined(ctrl.disableChoiceExpression)) {
isDisabled = !!(itemScope.$eval(ctrl.disableChoiceExpression));
}

_updateItemDisabled(item, isDisabled);
}

Expand All @@ -375,7 +375,11 @@ uis.controller('uiSelectCtrl',
if ( ! ctrl.items && ! ctrl.search && ! ctrl.tagging.isActivated) return;

if (!item || !_isItemDisabled(item)) {
if(ctrl.tagging.isActivated) {
// if click is made on existing item, prevent from tagging, ctrl.search does not matter
var manualSelection = false;
if($event && $event.type === 'click' && item)
Copy link
Contributor

@user378230 user378230 Jul 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a check down below already... (L438)

if ($event && $event.type === 'click') {
   ctrl.clickTriggeredSelect = true;
 }

Can you combine this/assignment into one and then just check for item?

manualSelection = true;
if(ctrl.tagging.isActivated && manualSelection === false) {
// if taggingLabel is disabled and item is undefined we pull from ctrl.search
if ( ctrl.taggingLabel === false ) {
if ( ctrl.activeIndex < 0 ) {
Expand Down Expand Up @@ -472,7 +476,7 @@ uis.controller('uiSelectCtrl',
}
};

// Set default function for locked choices - avoids unnecessary
// Set default function for locked choices - avoids unnecessary
// logic if functionality is not being used
ctrl.isLocked = function () {
return false;
Expand All @@ -484,7 +488,7 @@ uis.controller('uiSelectCtrl',

function _initaliseLockedChoices(doInitalise) {
if(!doInitalise) return;

var lockedItems = [];

function _updateItemLocked(item, isLocked) {
Expand Down Expand Up @@ -518,7 +522,7 @@ uis.controller('uiSelectCtrl',
return isLocked;
};
}


var sizeWatch = null;
var updaterScheduled = false;
Expand Down
29 changes: 27 additions & 2 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,31 @@ describe('ui-select tests', function() {
expect($(el).scope().$select.selected).toEqual(['idontexist']);
});

it('should allow selecting an item (click) in single select mode with tagging enabled', function() {

scope.taggingFunc = function (name) {
return name;
};

var el = compileTemplate(
'<ui-select ng-model="selection.selected" tagging="taggingFunc" tagging-label="false"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name" | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);

clickMatch(el);
setSearchText(el, 'Sam');
clickItem(el, 'Samantha');

expect(scope.selection.selected).toBe(scope.people[5]);
expect(getMatchLabel(el)).toEqual('Samantha');
});


it('should remove a choice when multiple and remove-selected is not given (default is true)', function () {

var el = compileTemplate(
Expand Down Expand Up @@ -2527,7 +2552,7 @@ describe('ui-select tests', function() {
expect(el.scope().$select.items[1]).toEqual(jasmine.objectContaining({name: 'Amalie', email: '[email protected]'}));
});


it('should have tolerance for undefined values', function () {

scope.modelValue = undefined;
Expand Down Expand Up @@ -2563,7 +2588,7 @@ describe('ui-select tests', function() {

expect($(el).scope().$select.selected).toEqual([]);
});

it('should allow paste tag from clipboard', function() {
scope.taggingFunc = function (name) {
return {
Expand Down