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

Commit

Permalink
fix(removeSelected): fix incorrect removal of preselected item
Browse files Browse the repository at this point in the history
Ensure that `removeSelected` only affects `multiple`s, include unit test, and add comment in demo examples.

Closes #1672
  • Loading branch information
dondi authored and user378230 committed Jun 28, 2016
1 parent a7210c4 commit 32b7924
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/assets/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ app.controller('DemoCtrl', function ($scope, $http, $timeout, $interval) {
vm.person.selectedValue = vm.peopleObj[3];
vm.person.selectedSingle = 'Samantha';
vm.person.selectedSingleKey = '5';
// To run the demos with a preselected person object, uncomment the line below.
//vm.person.selected = vm.person.selectedValue;

vm.people = [
{ name: 'Adam', email: 'adam@email.com', age: 12, country: 'United States' },
Expand Down
2 changes: 1 addition & 1 deletion src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ uis.controller('uiSelectCtrl',
data = data || ctrl.parserResult.source($scope);
var selectedItems = ctrl.selected;
//TODO should implement for single mode removeSelected
if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.removeSelected) {
if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.multiple || !ctrl.removeSelected) {
ctrl.setItemsFn(data);
}else{
if ( data !== undefined && data !== null ) {
Expand Down
27 changes: 26 additions & 1 deletion test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ describe('ui-select tests', function() {
expect($(el).scope().$select.selected).toEqual(['idontexist']);
});

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

var el = compileTemplate(
'<ui-select multiple ng-model="selection.selected"> \
Expand All @@ -1453,6 +1453,31 @@ describe('ui-select tests', function() {
});
});

it('should not remove a pre-selected choice when not multiple and remove-selected is not given (default is true)', function () {
scope.selection.selected = scope.people[5]; // Samantha

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

expect(getMatchLabel(el)).toEqual("Samantha");
openDropdown(el);

var choicesEls = $(el).find('.ui-select-choices-row');
expect(choicesEls.length).toEqual(8);

['Adam', 'Amalie', 'Estefanía', 'Adrian', 'Wladimir', 'Samantha', 'Nicole', 'Natasha'].forEach(function (name, index) {
expect($(choicesEls[index]).hasClass('disabled')).toBeFalsy();
expect($(choicesEls[index]).find('.person-name').text()).toEqual(name);
});
});

it('should disable a choice instead of removing it when remove-selected is false', function () {

var el = compileTemplate(
Expand Down

0 comments on commit 32b7924

Please sign in to comment.