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

Commit b7ac99a

Browse files
Jefiozieuser378230
authored andcommitted
fix(uiSelectMultiple): Don't call onSelectCallback if limit already reached
Previously if a limit was defined for a multiple selection ui-select and that limit was reached, the onSelectCallback would still be fired even though the item wasn't actually selected. This commit moves the firing of the callback to after the select has actually taken place to ensure it is only fired on the correct occassions. Fixes #1794 Closes #1836
1 parent 040eb7c commit b7ac99a

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

src/uiSelectController.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ uis.controller('uiSelectCtrl',
2121
ctrl.refreshing = false;
2222
ctrl.spinnerEnabled = uiSelectConfig.spinnerEnabled;
2323
ctrl.spinnerClass = uiSelectConfig.spinnerClass;
24-
2524
ctrl.removeSelected = uiSelectConfig.removeSelected; //If selected item(s) should be removed from dropdown list
2625
ctrl.closeOnSelect = true; //Initialized inside uiSelect directive link function
2726
ctrl.skipFocusser = false; //Set to true to avoid returning focus to ctrl when item is selected
@@ -433,17 +432,7 @@ uis.controller('uiSelectCtrl',
433432
}
434433
_resetSearchInput();
435434
$scope.$broadcast('uis:select', item);
436-
437-
var locals = {};
438-
locals[ctrl.parserResult.itemName] = item;
439-
440-
$timeout(function(){
441-
ctrl.onSelectCallback($scope, {
442-
$item: item,
443-
$model: ctrl.parserResult.modelMapper($scope, locals)
444-
});
445-
});
446-
435+
447436
if (ctrl.closeOnSelect) {
448437
ctrl.close(skipFocusser);
449438
}

src/uiSelectMultipleDirective.js

+9
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
178178
return;
179179
}
180180
$select.selected.push(item);
181+
var locals = {};
182+
locals[$select.parserResult.itemName] = item;
183+
184+
$timeout(function(){
185+
$select.onSelectCallback(scope, {
186+
$item: item,
187+
$model: $select.parserResult.modelMapper(scope, locals)
188+
});
189+
});
181190
$selectMultiple.updateModel();
182191
});
183192

src/uiSelectSingleDirective.js

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
5151

5252
scope.$on('uis:select', function (event, item) {
5353
$select.selected = item;
54+
var locals = {};
55+
locals[$select.parserResult.itemName] = item;
56+
57+
$timeout(function(){
58+
$select.onSelectCallback(scope, {
59+
$item: item,
60+
$model: $select.parserResult.modelMapper(scope, locals)
61+
});
62+
});
5463
});
5564

5665
scope.$on('uis:close', function (event, skipFocusser) {

test/select.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,8 @@ describe('ui-select tests', function() {
18401840
if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
18411841
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
18421842
if (attrs.resetSearchInput !== undefined) { attrsHtml += ' reset-search-input="' + attrs.resetSearchInput + '"'; }
1843+
if (attrs.limit !== undefined) { attrsHtml += ' limit="' + attrs.limit + '"'; }
1844+
if (attrs.onSelect !== undefined) { attrsHtml += ' on-select="' + attrs.onSelect + '"'; }
18431845
}
18441846

18451847
return compileTemplate(
@@ -2785,6 +2787,51 @@ describe('ui-select tests', function() {
27852787
expect(el.scope().$select.selected.length).toBe(2);
27862788
});
27872789

2790+
it('should set only 1 item in the selected items when limit = 1', function () {
2791+
var el = createUiSelectMultiple({limit: 1});
2792+
clickItem(el, 'Wladimir');
2793+
clickItem(el, 'Natasha');
2794+
expect(el.scope().$select.selected.length).toEqual(1);
2795+
});
2796+
2797+
it('should only have 1 item selected and onSelect function should only be handled once.',function(){
2798+
scope.onSelectFn = function ($item, $model) {
2799+
scope.$item = $item;
2800+
scope.$model = $model;
2801+
};
2802+
var el = createUiSelectMultiple({limit:1,onSelect:'onSelectFn($item, $model)'});
2803+
2804+
expect(scope.$item).toBeFalsy();
2805+
expect(scope.$model).toBeFalsy();
2806+
2807+
clickItem(el, 'Samantha');
2808+
$timeout.flush();
2809+
clickItem(el, 'Natasha');
2810+
$timeout.flush();
2811+
expect(scope.selection.selectedMultiple[0].name).toBe('Samantha');
2812+
expect(scope.$model.name).toEqual('Samantha');
2813+
expect(el.scope().$select.selected.length).toEqual(1);
2814+
});
2815+
2816+
it('should only have 2 items selected and onSelect function should be handeld.',function(){
2817+
scope.onSelectFn = function ($item, $model) {
2818+
scope.$item = $item;
2819+
scope.$model = $model;
2820+
};
2821+
var el = createUiSelectMultiple({onSelect:'onSelectFn($item, $model)'});
2822+
2823+
expect(scope.$item).toBeFalsy();
2824+
expect(scope.$model).toBeFalsy();
2825+
2826+
clickItem(el, 'Samantha');
2827+
$timeout.flush();
2828+
expect(scope.$model.name).toEqual('Samantha');
2829+
clickItem(el, 'Natasha');
2830+
$timeout.flush();
2831+
expect(scope.$model.name).toEqual('Natasha');
2832+
expect(el.scope().$select.selected.length).toEqual(2);
2833+
});
2834+
27882835
describe('resetSearchInput option multiple', function () {
27892836
it('should be true by default', function () {
27902837
expect(createUiSelectMultiple().scope().$select.resetSearchInput).toBe(true);

0 commit comments

Comments
 (0)