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

Commit

Permalink
fix(chips): fix chips focus functionality
Browse files Browse the repository at this point in the history
- The normal mobile devices won't trigger the focus on the element.
Focusing the element on `ng-click` fixes that issue.
Through keeping and redirecting to `ng-focus` it's still possible to select chips through keyboard
- At the moment, we only reset the selectedChip variable but we don't
  apply that to the view, so we need to run an async evaluation.

Fixes #5897 Fixes #5662
  • Loading branch information
devversion committed Jan 5, 2016
1 parent 8ef798f commit 6cbf6ae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,59 @@ describe('<md-chips>', function() {
}));
});

describe('focus functionality', function() {
var element, ctrl;

beforeEach(function() {
element = buildChips(CHIP_SELECT_TEMPLATE);
ctrl = element.controller('mdChips');
document.body.appendChild(element[0]);
});

afterEach(function() {
element.remove();
element = ctrl = null;
});

it('should focus the chip when clicking / touching on the chip', function() {
scope.selectChip = jasmine.createSpy('clickChipSpy');

var chips = getChipElements(element);
expect(chips.length).toBe(3);

chips[0].children[0].click();

expect(scope.selectChip).toHaveBeenCalledTimes(1);
});

it('should focus the chip through normal content focus', function() {
scope.selectChip = jasmine.createSpy('focusChipSpy');
var chips = getChipElements(element);
expect(chips.length).toBe(3);

chips[0].children[0].focus();

expect(scope.selectChip).toHaveBeenCalledTimes(1);
});

it('should blur the chip correctly', function() {
var chips = getChipElements(element);
expect(chips.length).toBe(3);

var chipContent = chips[0].children[0];
chipContent.focus();

expect(ctrl.selectedChip).toBe(0);

chipContent.blur();

scope.$digest();

expect(ctrl.selectedChip).toBe(-1);
});

});

describe('md-autocomplete', function() {
var AUTOCOMPLETE_CHIPS_TEMPLATE = '\
<md-chips ng-model="items">\
Expand Down
4 changes: 3 additions & 1 deletion src/components/chips/js/chipDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function MdChip($mdTheming, $mdUtil) {

if (ctrl) angular.element(element[0].querySelector('.md-chip-content'))
.on('blur', function () {
ctrl.selectedChip = -1;
ctrl.$scope.$evalAsync(function() {
ctrl.resetSelectedChip();
});
});
};
}
Expand Down
1 change: 1 addition & 0 deletions src/components/chips/js/chipsDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<div class="md-chip-content"\
tabindex="-1"\
aria-hidden="true"\
ng-click="!$mdChipsCtrl.readonly && $mdChipsCtrl.focusChip($index)"\
ng-focus="!$mdChipsCtrl.readonly && $mdChipsCtrl.selectChip($index)"\
md-chip-transclude="$mdChipsCtrl.chipContentsTemplate"></div>\
<div ng-if="!$mdChipsCtrl.readonly"\
Expand Down

0 comments on commit 6cbf6ae

Please sign in to comment.