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

Commit 531e4b3

Browse files
Jefiozieaaronroberson
authored andcommitted
fix(uiSelectController) ActiveIndex should skip disabled choises (#1848)
* fix(uiSelectController) ActiveIndex should skip disabled choises on key up/down. * Shortend the check for activeIndex. * Added test for multiple and tagging/ * fix(uiSelectController) ActiveIndex should skip disabled choises on key up/down. * Shortend the check for activeIndex. * Added test for multiple and tagging/ * Revert "Added the changes for merge." This reverts commit 46de564, reversing changes made to 143c6d9. * Some mistakes in commit. Now test should be good. * Made the requested changes and added some extra tests.
1 parent 7aabdc4 commit 531e4b3

File tree

2 files changed

+142
-7
lines changed

2 files changed

+142
-7
lines changed

src/uiSelectController.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ uis.controller('uiSelectCtrl',
432432
}
433433
_resetSearchInput();
434434
$scope.$broadcast('uis:select', item);
435-
435+
436436
if (ctrl.closeOnSelect) {
437437
ctrl.close(skipFocusser);
438438
}
@@ -565,11 +565,22 @@ uis.controller('uiSelectCtrl',
565565
switch (key) {
566566
case KEY.DOWN:
567567
if (!ctrl.open && ctrl.multiple) ctrl.activate(false, true); //In case its the search input in 'multiple' mode
568-
else if (ctrl.activeIndex < ctrl.items.length - 1) { ctrl.activeIndex++; }
568+
else if (ctrl.activeIndex < ctrl.items.length - 1) {
569+
var idx = ++ctrl.activeIndex;
570+
while(_isItemDisabled(ctrl.items[idx]) && idx < ctrl.items.length) {
571+
ctrl.activeIndex = ++idx;
572+
}
573+
}
569574
break;
570575
case KEY.UP:
576+
var minActiveIndex = (ctrl.search.length === 0 && ctrl.tagging.isActivated) ? -1 : 0;
571577
if (!ctrl.open && ctrl.multiple) ctrl.activate(false, true); //In case its the search input in 'multiple' mode
572-
else if (ctrl.activeIndex > 0 || (ctrl.search.length === 0 && ctrl.tagging.isActivated && ctrl.activeIndex > -1)) { ctrl.activeIndex--; }
578+
else if (ctrl.activeIndex > minActiveIndex) {
579+
var idxmin = --ctrl.activeIndex;
580+
while(_isItemDisabled(ctrl.items[idxmin]) && idxmin > minActiveIndex) {
581+
ctrl.activeIndex = --idxmin;
582+
}
583+
}
573584
break;
574585
case KEY.TAB:
575586
if (!ctrl.multiple || ctrl.open) ctrl.select(ctrl.items[ctrl.activeIndex], true);

test/select.spec.js

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ describe('ui-select tests', function() {
168168
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
169169
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
170170
if (attrs.backspaceReset !== undefined) { attrsHtml += ' backspace-reset="' + attrs.backspaceReset + '"';}
171+
if (attrs.uiDisableChoice !== undefined) { choicesAttrsHtml += ' ui-disable-choice="' + attrs.uiDisableChoice + '"';}
172+
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"';}
171173
}
172174

173175
return compileTemplate(
@@ -1837,11 +1839,13 @@ describe('ui-select tests', function() {
18371839
if (attrs.taggingLabel !== undefined) { attrsHtml += ' tagging-label="' + attrs.taggingLabel + '"'; }
18381840
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
18391841
if (attrs.groupBy !== undefined) { choicesAttrsHtml += ' group-by="' + attrs.groupBy + '"'; }
1842+
if (attrs.uiDisableChoice !== undefined) { choicesAttrsHtml += ' ui-disable-choice="' + attrs.uiDisableChoice + '"'; }
18401843
if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
18411844
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
18421845
if (attrs.resetSearchInput !== undefined) { attrsHtml += ' reset-search-input="' + attrs.resetSearchInput + '"'; }
18431846
if (attrs.limit !== undefined) { attrsHtml += ' limit="' + attrs.limit + '"'; }
18441847
if (attrs.onSelect !== undefined) { attrsHtml += ' on-select="' + attrs.onSelect + '"'; }
1848+
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"';}
18451849
}
18461850

18471851
return compileTemplate(
@@ -2832,6 +2836,61 @@ describe('ui-select tests', function() {
28322836
expect(el.scope().$select.selected.length).toEqual(2);
28332837
});
28342838

2839+
describe('Test key down key up and activeIndex should skip disabled choice for uiMultipleSelect',function(){
2840+
it('should ignored disabled items going up', function() {
2841+
var el = createUiSelect({uiDisableChoice:"person.age == 12"});
2842+
openDropdown(el);
2843+
var searchInput = el.find('.ui-select-search');
2844+
expect(el.scope().$select.activeIndex).toBe(0);
2845+
triggerKeydown(searchInput, Key.Down);
2846+
expect(el.scope().$select.activeIndex).toBe(2);
2847+
triggerKeydown(searchInput, Key.Up);
2848+
expect(el.scope().$select.activeIndex).toBe(0);
2849+
});
2850+
2851+
it('should ignored disabled items going up with tagging on', function() {
2852+
var el = createUiSelectMultiple({uiDisableChoice:"person.age == 12", tagging:true});
2853+
openDropdown(el);
2854+
var searchInput = el.find('.ui-select-search');
2855+
expect(el.scope().$select.activeIndex).toBe(-1);
2856+
triggerKeydown(searchInput, Key.Down);
2857+
expect(el.scope().$select.activeIndex).toBe(2);
2858+
triggerKeydown(searchInput, Key.Up);
2859+
expect(el.scope().$select.activeIndex).toBe(-1);
2860+
});
2861+
2862+
it('should ignored disabled items going down', function() {
2863+
var el = createUiSelectMultiple({uiDisableChoice:"person.age == 12"});
2864+
openDropdown(el);
2865+
var searchInput = el.find('.ui-select-search');
2866+
expect(el.scope().$select.activeIndex).toBe(0);
2867+
triggerKeydown(searchInput, Key.Down);
2868+
triggerKeydown(searchInput, Key.Enter);
2869+
expect(el.scope().$select.activeIndex).toBe(2);
2870+
});
2871+
2872+
it('should ignored disabled items going down with tagging on', function() {
2873+
var el = createUiSelectMultiple({uiDisableChoice:"person.age == 12", tagging:true});
2874+
openDropdown(el);
2875+
var searchInput = el.find('.ui-select-search');
2876+
expect(el.scope().$select.activeIndex).toBe(-1);
2877+
triggerKeydown(searchInput, Key.Down);
2878+
expect(el.scope().$select.activeIndex).toBe(2);
2879+
triggerKeydown(searchInput, Key.Up);
2880+
expect(el.scope().$select.activeIndex).toBe(-1);
2881+
});
2882+
2883+
it('should ignore disabled items, going down with remove-selected on false', function() {
2884+
var el = createUiSelectMultiple({uiDisableChoice:"person.age == 12", removeSelected:false});
2885+
openDropdown(el);
2886+
var searchInput = el.find('.ui-select-search');
2887+
expect(el.scope().$select.activeIndex).toBe(0);
2888+
triggerKeydown(searchInput, Key.Down);
2889+
triggerKeydown(searchInput, Key.Enter);
2890+
expect(el.scope().$select.activeIndex).toBe(2);
2891+
});
2892+
});
2893+
28352894
describe('resetSearchInput option multiple', function () {
28362895
it('should be true by default', function () {
28372896
expect(createUiSelectMultiple().scope().$select.resetSearchInput).toBe(true);
@@ -2842,6 +2901,7 @@ describe('ui-select tests', function() {
28422901
});
28432902
});
28442903

2904+
28452905
describe('Reset the search value', function () {
28462906
it('should clear the search input when resetSearchInput is true', function () {
28472907
var el = createUiSelectMultiple();
@@ -3107,7 +3167,7 @@ describe('ui-select tests', function() {
31073167

31083168
describe('Test Spinner for promises',function(){
31093169
var deferred;
3110-
3170+
31113171
function getFromServer(){
31123172
deferred = $q.defer();
31133173
return deferred.promise;
@@ -3130,14 +3190,14 @@ describe('ui-select tests', function() {
31303190
it('should have set a custom class value of randomclass', function () {
31313191
var control = createUiSelect({spinnerClass: 'randomclass'});
31323192
expect(control.scope().$select.spinnerClass).toEqual('randomclass');
3133-
});
3193+
});
31343194

31353195
it('should not display spinner when disabled', function() {
31363196
scope.getFromServer = getFromServer;
31373197
var el = createUiSelect({theme: 'bootstrap', refresh:"getFromServer($select.search)", refreshDelay:0});
31383198
openDropdown(el);
31393199
var spinner = el.find('.ui-select-refreshing');
3140-
expect(spinner.hasClass('ng-hide')).toBe(true);
3200+
expect(spinner.hasClass('ng-hide')).toBe(true);
31413201
setSearchText(el, 'a');
31423202
expect(spinner.hasClass('ng-hide')).toBe(true);
31433203
deferred.resolve();
@@ -3150,7 +3210,7 @@ describe('ui-select tests', function() {
31503210
var el = createUiSelect({spinnerEnabled: true,theme: 'bootstrap', refresh:"getFromServer($select.search)", refreshDelay:0});
31513211
openDropdown(el);
31523212
var spinner = el.find('.ui-select-refreshing');
3153-
expect(spinner.hasClass('ng-hide')).toBe(true);
3213+
expect(spinner.hasClass('ng-hide')).toBe(true);
31543214
setSearchText(el, 'a');
31553215
expect(spinner.hasClass('ng-hide')).toBe(false);
31563216
deferred.resolve();
@@ -3167,4 +3227,68 @@ describe('ui-select tests', function() {
31673227
});
31683228
});
31693229

3230+
describe('Test key down key up and activeIndex should skip disabled choice',function(){
3231+
it('should ignore disabled items, going down', function() {
3232+
var el = createUiSelect({uiDisableChoice:"person.age == 12"});
3233+
openDropdown(el);
3234+
var searchInput = el.find('.ui-select-search');
3235+
expect(el.scope().$select.activeIndex).toBe(0);
3236+
triggerKeydown(searchInput, Key.Down);
3237+
triggerKeydown(searchInput, Key.Enter);
3238+
expect(el.scope().$select.activeIndex).toBe(2);
3239+
});
3240+
3241+
it('should ignore disabled items, going up', function() {
3242+
var el = createUiSelect({uiDisableChoice:"person.age == 12"});
3243+
openDropdown(el);
3244+
var searchInput = el.find('.ui-select-search');
3245+
expect(el.scope().$select.activeIndex).toBe(0);
3246+
triggerKeydown(searchInput, Key.Down);
3247+
expect(el.scope().$select.activeIndex).toBe(2);
3248+
triggerKeydown(searchInput, Key.Up);
3249+
expect(el.scope().$select.activeIndex).toBe(0);
3250+
});
3251+
3252+
it('should ignored disabled items going up with tagging on', function() {
3253+
var el = createUiSelect({uiDisableChoice:"person.age == 12", tagging:true});
3254+
openDropdown(el);
3255+
var searchInput = el.find('.ui-select-search');
3256+
expect(el.scope().$select.activeIndex).toBe(-1);
3257+
triggerKeydown(searchInput, Key.Down);
3258+
expect(el.scope().$select.activeIndex).toBe(2);
3259+
triggerKeydown(searchInput, Key.Up);
3260+
expect(el.scope().$select.activeIndex).toBe(-1);
3261+
});
3262+
3263+
it('should ignored disabled items in the down direction with tagging on', function() {
3264+
var el = createUiSelect({uiDisableChoice:"person.age == 12", tagging:true});
3265+
openDropdown(el);
3266+
var searchInput = el.find('.ui-select-search');
3267+
expect(el.scope().$select.activeIndex).toBe(-1);
3268+
triggerKeydown(searchInput, Key.Down);
3269+
triggerKeydown(searchInput, Key.Enter);
3270+
expect(el.scope().$select.activeIndex).toBe(2);
3271+
});
3272+
3273+
it('should ignored disabled items going up with tagging on and custom tag', function() {
3274+
var el = createUiSelect({uiDisableChoice:"person.age == 12", tagging:true, taggingLabel:'custom tag'});
3275+
openDropdown(el);
3276+
var searchInput = el.find('.ui-select-search');
3277+
expect(el.scope().$select.activeIndex).toBe(-1);
3278+
triggerKeydown(searchInput, Key.Down);
3279+
triggerKeydown(searchInput, Key.Enter);
3280+
expect(el.scope().$select.activeIndex).toBe(2);
3281+
});
3282+
3283+
it('should ignore disabled items, going down with remove-selected on false', function() {
3284+
var el = createUiSelect({uiDisableChoice:"person.age == 12", removeSelected:false});
3285+
openDropdown(el);
3286+
var searchInput = el.find('.ui-select-search');
3287+
expect(el.scope().$select.activeIndex).toBe(0);
3288+
triggerKeydown(searchInput, Key.Down);
3289+
triggerKeydown(searchInput, Key.Enter);
3290+
expect(el.scope().$select.activeIndex).toBe(2);
3291+
});
3292+
});
3293+
31703294
});

0 commit comments

Comments
 (0)