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

fix(removeSelected): fix incorrect removal of preselected item #1690

Merged
merged 1 commit into from
Jun 28, 2016
Merged
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
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: '[email protected]', 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