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

Commit cfb5acb

Browse files
devversionThomasBurleson
authored andcommitted
fix(chips): fix chips focus functionality
- 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. Closes #5941
1 parent 338e0bf commit cfb5acb

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/components/chips/chips.spec.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ describe('<md-chips>', function() {
501501
scope.items = [];
502502
});
503503

504-
it('should not add a new chip if the max-chips limit is reached', function() {
504+
it('should not add a new chip if the max-chips limit is reached', function () {
505505
var element = buildChips('<md-chips ng-model="items" md-max-chips="1"></md-chips>');
506506
var ctrl = element.controller('mdChips');
507507

@@ -556,6 +556,58 @@ describe('<md-chips>', function() {
556556
expect(ctrl.chipBuffer).toBe('Test 2');
557557
expect(scope.items.length).not.toBe(2);
558558
});
559+
});
560+
561+
describe('focus functionality', function() {
562+
var element, ctrl;
563+
564+
beforeEach(function() {
565+
element = buildChips(CHIP_SELECT_TEMPLATE);
566+
ctrl = element.controller('mdChips');
567+
document.body.appendChild(element[0]);
568+
});
569+
570+
afterEach(function() {
571+
element.remove();
572+
element = ctrl = null;
573+
});
574+
575+
it('should focus the chip when clicking / touching on the chip', function() {
576+
ctrl.focusChip = jasmine.createSpy('focusChipSpy');
577+
578+
var chips = getChipElements(element);
579+
expect(chips.length).toBe(3);
580+
581+
chips.children().eq(0).triggerHandler('click');
582+
583+
expect(ctrl.focusChip).toHaveBeenCalledTimes(1);
584+
});
585+
586+
it('should focus the chip through normal content focus', function() {
587+
scope.selectChip = jasmine.createSpy('focusChipSpy');
588+
var chips = getChipElements(element);
589+
expect(chips.length).toBe(3);
590+
591+
chips.children().eq(0).triggerHandler('focus');
592+
593+
expect(scope.selectChip).toHaveBeenCalledTimes(1);
594+
});
595+
596+
it('should blur the chip correctly', function() {
597+
var chips = getChipElements(element);
598+
expect(chips.length).toBe(3);
599+
600+
var chipContent = chips.children().eq(0);
601+
chipContent.triggerHandler('focus');
602+
603+
expect(ctrl.selectedChip).toBe(0);
604+
605+
chipContent.eq(0).triggerHandler('blur');
606+
607+
scope.$digest();
608+
609+
expect(ctrl.selectedChip).toBe(-1);
610+
});
559611

560612
});
561613

src/components/chips/js/chipDirective.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function MdChip($mdTheming, $mdUtil) {
5151

5252
if (ctrl) angular.element(element[0].querySelector('._md-chip-content'))
5353
.on('blur', function () {
54-
ctrl.selectedChip = -1;
54+
ctrl.resetSelectedChip();
55+
ctrl.$scope.$applyAsync();
5556
});
5657
};
5758
}

src/components/chips/js/chipsDirective.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<div class="_md-chip-content"\
130130
tabindex="-1"\
131131
aria-hidden="true"\
132+
ng-click="!$mdChipsCtrl.readonly && $mdChipsCtrl.focusChip($index)"\
132133
ng-focus="!$mdChipsCtrl.readonly && $mdChipsCtrl.selectChip($index)"\
133134
md-chip-transclude="$mdChipsCtrl.chipContentsTemplate"></div>\
134135
<div ng-if="!$mdChipsCtrl.readonly"\

0 commit comments

Comments
 (0)