Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(autocomplete): addresses accessibility issues
Browse files Browse the repository at this point in the history
Closes #1473
  • Loading branch information
Robert Messerle committed Feb 25, 2015
1 parent fc223b0 commit 0bd7afb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.module('material.components.autocomplete')
.controller('MdAutocompleteCtrl', MdAutocompleteCtrl);

function MdAutocompleteCtrl ($scope, $element, $timeout, $q, $mdUtil, $mdConstant) {
function MdAutocompleteCtrl ($scope, $element, $q, $mdUtil, $mdConstant) {

//-- private variables
var self = this,
Expand Down Expand Up @@ -32,6 +32,7 @@
self.select = select;
self.getCurrentDisplayValue = getCurrentDisplayValue;
self.fetch = $mdUtil.debounce(fetchResults);
self.messages = [];

return init();

Expand All @@ -52,7 +53,7 @@
function getItemScope (item) {
if (!item) return;
var locals = {};
if (self.itemName) locals[self.itemName] = $scope.selectedItem;
if (self.itemName) locals[self.itemName] = item;
return locals;
}

Expand All @@ -63,12 +64,14 @@
if ($scope.itemChange && selectedItem !== previousSelectedItem) $scope.itemChange(getItemScope(selectedItem));
});
}

function handleSearchText (searchText, previousSearchText) {
self.index = -1;
if (!searchText || searchText.length < Math.max(parseInt($scope.minLength, 10), 1)) {
self.loading = false;
self.matches = [];
self.hidden = shouldHide();
updateMessages();
return;
}
var term = searchText.toLowerCase();
Expand All @@ -78,6 +81,7 @@
}
if (!$scope.noCache && cache[term]) {
self.matches = cache[term];
updateMessages();
} else {
self.fetch(searchText);
}
Expand All @@ -101,9 +105,22 @@
self.loading = false;
self.matches = matches;
self.hidden = shouldHide();
updateMessages();
}
}

function updateMessages () {
switch (self.matches.length) {
case 0: return self.messages.splice(0);

This comment has been minimized.

Copy link
@ajoslin

ajoslin Feb 26, 2015

Contributor

Uh oh, now we're embedding english messages. Make sure that these are configurable through an attribute or similar.

This comment has been minimized.

Copy link
@marcysutton

marcysutton Feb 26, 2015

Contributor

Yep, @robertmesserle and I talked about that today. This was an intermediate step to doing that.

This comment has been minimized.

Copy link
@ThomasBurleson

ThomasBurleson Feb 26, 2015

Contributor

@robertmesserle, @marcysutton - For your final solution, have you considered how we plan to support localization (l10n) of these strings?

This comment has been minimized.

Copy link
@gkalpak

gkalpak Feb 26, 2015

Member

You might also want to consider (somehow) leveraging Angular's ngPluralize directive.

case 1: return self.messages.push({ display: 'There is 1 match available.' });
default: return self.messages.push({ display: 'There are ' + self.matches.length + ' matches available.' });
}
}

function updateSelectionMessage () {
self.messages.push({ display: getCurrentDisplayValue() });
}

function blur () {
self.hidden = true;
}
Expand All @@ -115,12 +132,14 @@
event.preventDefault();
self.index = Math.min(self.index + 1, self.matches.length - 1);
updateScroll();
updateSelectionMessage();
break;
case $mdConstant.KEY_CODE.UP_ARROW:
if (self.loading) return;
event.preventDefault();
self.index = Math.max(0, self.index - 1);
updateScroll();
updateSelectionMessage();
break;
case $mdConstant.KEY_CODE.ENTER:
if (self.loading || self.index < 0) return;
Expand Down
14 changes: 7 additions & 7 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
type="button"\
ng-if="searchText"\
ng-click="$mdAutocompleteCtrl.clear()">\
<md-icon md-svg-icon="cancel"></md-icon>\
<span class="visually-hidden">Clear</span>\
</button>\
<md-progress-linear ng-if="$mdAutocompleteCtrl.loading" md-mode="indeterminate"></md-progress-linear>\
<md-icon md-svg-icon="cancel"></md-icon>\
<span class="visually-hidden">Clear</span>\
</button>\
<md-progress-linear\
ng-if="$mdAutocompleteCtrl.loading"\
md-mode="indeterminate"></md-progress-linear>\
</md-autocomplete-wrap>\
<ul role="presentation">\
<li ng-repeat="(index, item) in $mdAutocompleteCtrl.matches"\
Expand All @@ -74,9 +76,7 @@
class="visually-hidden"\
role="status"\
aria-live="assertive">\
<p ng-if="$mdAutocompleteCtrl.index === -1 && $mdAutocompleteCtrl.matches.length === 1">There is 1 match available.</p>\
<p ng-if="$mdAutocompleteCtrl.index === -1 && $mdAutocompleteCtrl.matches.length > 1">There are {{$mdAutocompleteCtrl.matches.length}} matches available.</p>\
<p ng-if="$mdAutocompleteCtrl.index >= 0">{{ $mdAutocompleteCtrl.getCurrentDisplayValue() }}</p>\
<p ng-repeat="message in $mdAutocompleteCtrl.messages">{{message.display}}</p>\
</aria-status>',
transclude: true,
controller: 'MdAutocompleteCtrl',
Expand Down

0 comments on commit 0bd7afb

Please sign in to comment.