Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(autocomplete): announce changes with ARIA
Browse files Browse the repository at this point in the history
WIP: needs to announce match count change when user re-filters list by deleting a character.

Closes #1473
  • Loading branch information
Marcy Sutton committed Feb 13, 2015
1 parent 800af31 commit 81db937
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,33 @@
self.fetch(searchText);
}
});

$scope.$watch('matchCount', function(matchCount) {
if (matchCount > 0) {
var matchWords = {
verb: 'are',
noun: 'matches'
};
if (matchCount === 1) {
matchWords.verb = 'is';
matchWords.noun = 'match';
}
var string = 'There '+matchWords.verb+ ' '+matchCount +' '+matchWords.noun+ ' available.';
setAriaStatus(string);
}
});
}

function fetchResults (searchText) {
var items = $scope.$parent.$eval(itemExpr),
term = searchText.toLowerCase();
promise = $q.when(items).then(function (matches) {
cache[term] = matches;

// how can we announce a change in matches
// even with the cache?
$scope.matchCount = matches.length;

if (searchText !== $scope.searchText) return; //-- just cache the results if old request
promise = null;
self.loading = false;
Expand All @@ -86,12 +106,14 @@
if (self.loading) return;
event.preventDefault();
self.index = Math.min(self.index + 1, self.matches.length - 1);
setAriaStatus(getSelectedItemText());
updateScroll();
break;
case $mdConstant.KEY_CODE.UP_ARROW:
if (self.loading) return;
event.preventDefault();
self.index = Math.max(0, self.index - 1);
setAriaStatus(getSelectedItemText());
updateScroll();
break;
case $mdConstant.KEY_CODE.ENTER:
Expand Down Expand Up @@ -129,11 +151,22 @@
function select (index) {
$scope.selectedItem = self.matches[index];
$scope.searchText = getDisplayValue($scope.selectedItem) || $scope.searchText;
if ($scope.searchText) {
setAriaStatus($scope.searchText);
}
self.hidden = true;
self.index = -1;
self.matches = [];
}

function getSelectedItemText () {
return self.matches[self.index].display;
}

function setAriaStatus (text) {
$scope.hintText = text;
}

function updateScroll () {
var top = 41 * self.index,
bot = top + 41,
Expand Down
5 changes: 2 additions & 3 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@
</ul>\
<aria-status\
class="visually-hidden"\
aria-atomic="true"\
role="status"\
aria-live="polite">\
<p ng-repeat="item in $mdAutocompleteCtrl.matches">{{item.display}}</p>\
aria-live="assertive">\
<p>{{hintText}}</p>\
</aria-status>',
transclude: true,
controller: 'MdAutocompleteCtrl',
Expand Down

0 comments on commit 81db937

Please sign in to comment.