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

Commit 3c89a15

Browse files
committed
Added extra check and onSelectcallback moved to directives.
1 parent f25af8f commit 3c89a15

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

src/uiSelectController.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ uis.controller('uiSelectCtrl',
2121
ctrl.refreshing = false;
2222
ctrl.spinnerEnabled = uiSelectConfig.spinnerEnabled;
2323
ctrl.spinnerClass = uiSelectConfig.spinnerClass;
24-
24+
ctrl.limitReached = false;
2525
ctrl.removeSelected = uiSelectConfig.removeSelected; //If selected item(s) should be removed from dropdown list
2626
ctrl.closeOnSelect = true; //Initialized inside uiSelect directive link function
2727
ctrl.skipFocusser = false; //Set to true to avoid returning focus to ctrl when item is selected
@@ -433,17 +433,7 @@ uis.controller('uiSelectCtrl',
433433
}
434434
_resetSearchInput();
435435
$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-
436+
447437
if (ctrl.closeOnSelect) {
448438
ctrl.close(skipFocusser);
449439
}

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

+45
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,8 @@ describe('ui-select tests', function() {
18191819
if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
18201820
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
18211821
if (attrs.resetSearchInput !== undefined) { attrsHtml += ' reset-search-input="' + attrs.resetSearchInput + '"'; }
1822+
if (attrs.limit !== undefined) { attrsHtml += ' limit="' + attrs.limit + '"'; }
1823+
if (attrs.onSelect !== undefined) { attrsHtml += ' on-select="' + attrs.onSelect + '"'; }
18221824
}
18231825

18241826
return compileTemplate(
@@ -2764,6 +2766,49 @@ describe('ui-select tests', function() {
27642766
expect(el.scope().$select.selected.length).toBe(2);
27652767
});
27662768

2769+
it('should set only 1 item in the selected items when limit = 1', function () {
2770+
var el = createUiSelectMultiple({limit: 1});
2771+
clickItem(el, 'Wladimir');
2772+
clickItem(el, 'Natasha');
2773+
expect(el.scope().$select.selected.length).toEqual(1);
2774+
});
2775+
2776+
it('should only have 1 item selected and onSelect function should only be handled once.',function(){
2777+
scope.onSelectFn = function ($item, $model) {
2778+
scope.$item = $item;
2779+
scope.$model = $model;
2780+
};
2781+
var el = createUiSelectMultiple({limit:1,onSelect:'onSelectFn($item, $model)'});
2782+
2783+
expect(scope.$item).toBeFalsy();
2784+
expect(scope.$model).toBeFalsy();
2785+
2786+
clickItem(el, 'Samantha');
2787+
$timeout.flush();
2788+
clickItem(el, 'Natasha');
2789+
$timeout.flush();
2790+
expect(scope.selection.selectedMultiple[0].name).toBe('Samantha');
2791+
expect(scope.$model.name).toEqual('Samantha');
2792+
expect(el.scope().$select.selected.length).toEqual(1);
2793+
});
2794+
2795+
it('should only have 2 items selected and onSelect function should be handeld.',function(){
2796+
scope.onSelectFn = function ($item, $model) {
2797+
scope.$item = $item;
2798+
scope.$model = $model;
2799+
};
2800+
var el = createUiSelectMultiple({onSelect:'onSelectFn($item, $model)'});
2801+
2802+
expect(scope.$item).toBeFalsy();
2803+
expect(scope.$model).toBeFalsy();
2804+
2805+
clickItem(el, 'Samantha');
2806+
$timeout.flush();
2807+
clickItem(el, 'Natasha');
2808+
$timeout.flush();
2809+
expect(el.scope().$select.selected.length).toEqual(2);
2810+
});
2811+
27672812
describe('resetSearchInput option multiple', function () {
27682813
it('should be true by default', function () {
27692814
expect(createUiSelectMultiple().scope().$select.resetSearchInput).toBe(true);

0 commit comments

Comments
 (0)