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

Commit aef5a80

Browse files
committed
Added closeOnSelect attribute
1 parent e277a1c commit aef5a80

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

examples/demo-multi-select.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ <h3>Multi select</h3>
113113
<h1>Multi Selection Demos</h1>
114114

115115
<h3>Array of strings</h3>
116-
<ui-select multiple ng-model="multipleDemo.colors" theme="bootstrap" ng-disabled="disabled" style="width: 300px;">
116+
<ui-select multiple ng-model="multipleDemo.colors" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 300px;">
117117
<ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
118118
<ui-select-choices repeat="color in availableColors | filter:$select.search">
119119
{{color}}
@@ -122,7 +122,7 @@ <h3>Array of strings</h3>
122122
<p>Selected: {{multipleDemo.colors}}</p>
123123
<hr>
124124
<h3>Array of objects</h3>
125-
<ui-select multiple ng-model="multipleDemo.selectedPeople" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
125+
<ui-select multiple ng-model="multipleDemo.selectedPeople" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
126126
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
127127
<ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
128128
<div ng-bind-html="person.name | highlight: $select.search"></div>
@@ -136,7 +136,7 @@ <h3>Array of objects</h3>
136136

137137
<hr>
138138
<h3>Deselect callback with single property binding</h3>
139-
<ui-select multiple ng-model="multipleDemo.deSelectedPeople" on-remove="removed($item, $model)" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
139+
<ui-select multiple ng-model="multipleDemo.deSelectedPeople" on-remove="removed($item, $model)" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
140140
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
141141
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
142142
<div ng-bind-html="person.name | highlight: $select.search"></div>
@@ -151,7 +151,7 @@ <h3>Deselect callback with single property binding</h3>
151151

152152
<hr>
153153
<h3>Array of objects with single property binding</h3>
154-
<ui-select multiple ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
154+
<ui-select multiple ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
155155
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
156156
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
157157
<div ng-bind-html="person.name | highlight: $select.search"></div>
@@ -165,7 +165,7 @@ <h3>Array of objects with single property binding</h3>
165165

166166
<hr>
167167
<h3>Array of objects (with groupBy)</h3>
168-
<ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
168+
<ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
169169
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
170170
<ui-select-choices group-by="someGroupFn" repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
171171
<div ng-bind-html="person.name | highlight: $select.search"></div>

src/select.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
theme: 'bootstrap',
6868
searchEnabled: true,
6969
placeholder: '', // Empty by default, like HTML tag <select>
70-
refreshDelay: 1000 // In milliseconds
70+
refreshDelay: 1000, // In milliseconds
71+
closeOnSelect: true
7172
})
7273

7374
// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
@@ -159,6 +160,8 @@
159160
ctrl.refreshDelay = undefined; // Initialized inside uiSelectChoices directive link function
160161
ctrl.multiple = false; // Initialized inside uiSelect directive link function
161162
ctrl.disableChoiceExpression = undefined; // Initialized inside uiSelect directive link function
163+
ctrl.closeOnSelect = true; // Initialized inside uiSelect directive link function
164+
ctrl.clickTriggeredSelect = false;
162165

163166
ctrl.isEmpty = function() {
164167
return angular.isUndefined(ctrl.selected) || ctrl.selected === null || ctrl.selected === '';
@@ -325,8 +328,7 @@
325328
};
326329

327330
// When the user clicks on an item inside the dropdown
328-
ctrl.select = function(item, skipFocusser) {
329-
331+
ctrl.select = function(item, skipFocusser, $event) {
330332
if (item === undefined || !item._uiSelectChoiceDisabled) {
331333
var locals = {};
332334
locals[ctrl.parserResult.itemName] = item;
@@ -336,13 +338,18 @@
336338
$model: ctrl.parserResult.modelMapper($scope, locals)
337339
});
338340

339-
if(ctrl.multiple){
341+
if(ctrl.multiple) {
340342
ctrl.selected.push(item);
341343
ctrl.sizeSearchInput();
342344
} else {
343345
ctrl.selected = item;
344346
}
345-
ctrl.close(skipFocusser);
347+
if (!ctrl.multiple || ctrl.closeOnSelect) {
348+
ctrl.close(skipFocusser);
349+
}
350+
if ($event && $event.type === 'click') {
351+
ctrl.clickTriggeredSelect = true;
352+
}
346353
}
347354
};
348355

@@ -602,7 +609,7 @@
602609
var searchInput = element.querySelectorAll('input.ui-select-search');
603610

604611
$select.multiple = (angular.isDefined(attrs.multiple)) ? (attrs.multiple === '') ? true : (attrs.multiple.toLowerCase() === 'true') : false;
605-
612+
$select.closeOnSelect = (angular.isDefined(attrs.closeOnSelect) && attrs.closeOnSelect.toLowerCase() === 'false') ? false : uiSelectConfig.closeOnSelect;
606613
$select.onSelectCallback = $parse(attrs.onSelect);
607614
$select.onRemoveCallback = $parse(attrs.onRemove);
608615

@@ -805,10 +812,11 @@
805812
contains = element[0].contains(e.target);
806813
}
807814

808-
if (!contains) {
815+
if (!contains && !$select.clickTriggeredSelect) {
809816
$select.close();
810817
scope.$digest();
811818
}
819+
$select.clickTriggeredSelect = false;
812820
}
813821

814822
// See Click everywhere but here event http://stackoverflow.com/questions/12931369
@@ -887,7 +895,7 @@
887895
choices.attr('ng-repeat', RepeatParser.getNgRepeatExpression($select.parserResult.itemName, '$select.items', $select.parserResult.trackByExp, groupByExp))
888896
.attr('ng-if', '$select.open') //Prevent unnecessary watches when dropdown is closed
889897
.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')')
890-
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ')');
898+
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',false,$event)');
891899

892900
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
893901
if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);

test/select.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ describe('ui-select tests', function() {
10141014
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
10151015
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
10161016
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
1017+
if (attrs.closeOnSelect !== undefined) { attrsHtml += ' close-on-select="' + attrs.closeOnSelect + '"'; }
10171018
}
10181019

10191020
return compileTemplate(
@@ -1249,6 +1250,22 @@ describe('ui-select tests', function() {
12491250

12501251
});
12511252

1253+
it('should not close dropdown after selecting if closeOnSelect=false', function() {
1254+
1255+
scope.selection.selectedMultiple = [scope.people[5]]; //Samantha
1256+
var el = createUiSelectMultiple({closeOnSelect: false});
1257+
var searchInput = el.find('.ui-select-search');
1258+
1259+
expect(isDropdownOpened(el)).toEqual(false);
1260+
triggerKeydown(searchInput, Key.Down)
1261+
expect(isDropdownOpened(el)).toEqual(true);
1262+
1263+
clickItem(el, 'Wladimir');
1264+
1265+
expect(isDropdownOpened(el)).toEqual(true);
1266+
1267+
});
1268+
12521269
it('should closes dropdown when pressing ESC key from search input', function() {
12531270

12541271
scope.selection.selectedMultiple = [scope.people[4], scope.people[5], scope.people[6]]; //Wladimir, Samantha & Nicole

0 commit comments

Comments
 (0)