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

Commit 32b7924

Browse files
dondiuser378230
authored andcommitted
fix(removeSelected): fix incorrect removal of preselected item
Ensure that `removeSelected` only affects `multiple`s, include unit test, and add comment in demo examples. Closes #1672
1 parent a7210c4 commit 32b7924

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Diff for: docs/assets/demo.js

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ app.controller('DemoCtrl', function ($scope, $http, $timeout, $interval) {
150150
vm.person.selectedValue = vm.peopleObj[3];
151151
vm.person.selectedSingle = 'Samantha';
152152
vm.person.selectedSingleKey = '5';
153+
// To run the demos with a preselected person object, uncomment the line below.
154+
//vm.person.selected = vm.person.selectedValue;
153155

154156
vm.people = [
155157
{ name: 'Adam', email: '[email protected]', age: 12, country: 'United States' },

Diff for: src/uiSelectController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ uis.controller('uiSelectCtrl',
235235
data = data || ctrl.parserResult.source($scope);
236236
var selectedItems = ctrl.selected;
237237
//TODO should implement for single mode removeSelected
238-
if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.removeSelected) {
238+
if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.multiple || !ctrl.removeSelected) {
239239
ctrl.setItemsFn(data);
240240
}else{
241241
if ( data !== undefined && data !== null ) {

Diff for: test/select.spec.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ describe('ui-select tests', function() {
14271427
expect($(el).scope().$select.selected).toEqual(['idontexist']);
14281428
});
14291429

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

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

1456+
it('should not remove a pre-selected choice when not multiple and remove-selected is not given (default is true)', function () {
1457+
scope.selection.selected = scope.people[5]; // Samantha
1458+
1459+
var el = compileTemplate(
1460+
'<ui-select ng-model="selection.selected"> \
1461+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
1462+
<ui-select-choices repeat="person in people | filter: $select.search"> \
1463+
<div class="person-name" ng-bind-html="person.name" | highlight: $select.search"></div> \
1464+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1465+
</ui-select-choices> \
1466+
</ui-select>'
1467+
);
1468+
1469+
expect(getMatchLabel(el)).toEqual("Samantha");
1470+
openDropdown(el);
1471+
1472+
var choicesEls = $(el).find('.ui-select-choices-row');
1473+
expect(choicesEls.length).toEqual(8);
1474+
1475+
['Adam', 'Amalie', 'Estefanía', 'Adrian', 'Wladimir', 'Samantha', 'Nicole', 'Natasha'].forEach(function (name, index) {
1476+
expect($(choicesEls[index]).hasClass('disabled')).toBeFalsy();
1477+
expect($(choicesEls[index]).find('.person-name').text()).toEqual(name);
1478+
});
1479+
});
1480+
14561481
it('should disable a choice instead of removing it when remove-selected is false', function () {
14571482

14581483
var el = compileTemplate(

0 commit comments

Comments
 (0)