Skip to content

Commit f1d1a49

Browse files
committed
Merge pull request angular-ui#181 from corystevens/feat-tabindex-on-focusser
Add support for tabindex on focusser input
2 parents 3b5c482 + ffa1323 commit f1d1a49

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: src/select.js

+17
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@
549549
var $select = ctrls[0];
550550
var ngModel = ctrls[1];
551551

552+
var searchInput = element.querySelectorAll('input.ui-select-search');
553+
552554
$select.multiple = angular.isDefined(attrs.multiple);
553555

554556
$select.onSelectCallback = $parse(attrs.onSelect);
@@ -624,6 +626,21 @@
624626

625627
//Idea from: https://github.com/ivaynberg/select2/blob/79b5bf6db918d7560bdd959109b7bcfb47edaf43/select2.js#L1954
626628
var focusser = angular.element("<input ng-disabled='$select.disabled' class='ui-select-focusser ui-select-offscreen' type='text' aria-haspopup='true' role='button' />");
629+
630+
if(attrs.tabindex){
631+
//tabindex might be an expression, wait until it contains the actual value before we set the focusser tabindex
632+
attrs.$observe('tabindex', function(value) {
633+
//If we are using multiple, add tabindex to the search input
634+
if($select.multiple){
635+
searchInput.attr("tabindex", value);
636+
} else {
637+
focusser.attr("tabindex", value);
638+
}
639+
//Remove the tabindex on the parent so that it is not focusable
640+
element.removeAttr("tabindex");
641+
});
642+
}
643+
627644
$compile(focusser)(scope);
628645
$select.focusser = focusser;
629646

Diff for: test/select.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('ui-select tests', function() {
6666
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
6767
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
6868
if (attrs.theme !== undefined) { attrsHtml += ' theme="' + attrs.theme + '"'; }
69+
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
6970
}
7071

7172
return compileTemplate(
@@ -204,7 +205,28 @@ describe('ui-select tests', function() {
204205

205206
el.find(".ui-select-toggle").click();
206207
expect($select.open).toEqual(false);
208+
});
209+
210+
it('should pass tabindex to focusser', function() {
211+
var el = createUiSelect({tabindex: 5});
212+
213+
expect($(el).find('.ui-select-focusser').attr('tabindex')).toEqual('5');
214+
expect($(el).attr('tabindex')).toEqual(undefined);
215+
});
216+
217+
it('should pass tabindex to focusser when tabindex is an expression', function() {
218+
scope.tabValue = 22;
219+
var el = createUiSelect({tabindex: '{{tabValue + 10}}'});
207220

221+
expect($(el).find('.ui-select-focusser').attr('tabindex')).toEqual('32');
222+
expect($(el).attr('tabindex')).toEqual(undefined);
223+
});
224+
225+
it('should not give focusser a tabindex when ui-select does not have one', function() {
226+
var el = createUiSelect();
227+
228+
expect($(el).find('.ui-select-focusser').attr('tabindex')).toEqual(undefined);
229+
expect($(el).attr('tabindex')).toEqual(undefined);
208230
});
209231

210232
it('should be disabled if the attribute says so', function() {
@@ -751,6 +773,7 @@ describe('ui-select tests', function() {
751773
if (attrs !== undefined) {
752774
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
753775
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
776+
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
754777
}
755778

756779
return compileTemplate(
@@ -796,6 +819,31 @@ describe('ui-select tests', function() {
796819
// $timeout.flush();
797820
});
798821

822+
it('should pass tabindex to searchInput', function() {
823+
var el = createUiSelectMultiple({tabindex: 5});
824+
var searchInput = el.find('.ui-select-search');
825+
826+
expect(searchInput.attr('tabindex')).toEqual('5');
827+
expect($(el).attr('tabindex')).toEqual(undefined);
828+
});
829+
830+
it('should pass tabindex to searchInput when tabindex is an expression', function() {
831+
scope.tabValue = 22;
832+
var el = createUiSelectMultiple({tabindex: '{{tabValue + 10}}'});
833+
var searchInput = el.find('.ui-select-search');
834+
835+
expect(searchInput.attr('tabindex')).toEqual('32');
836+
expect($(el).attr('tabindex')).toEqual(undefined);
837+
});
838+
839+
it('should not give searchInput a tabindex when ui-select does not have one', function() {
840+
var el = createUiSelectMultiple();
841+
var searchInput = el.find('.ui-select-search');
842+
843+
expect(searchInput.attr('tabindex')).toEqual(undefined);
844+
expect($(el).attr('tabindex')).toEqual(undefined);
845+
});
846+
799847
it('should update size of search input after removing an item', function() {
800848
scope.selection.selectedMultiple = [scope.people[4], scope.people[5]]; //Wladimir & Samantha
801849
var el = createUiSelectMultiple();

0 commit comments

Comments
 (0)