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

Commit 1df38df

Browse files
EladBezalelThomasBurleson
authored andcommitted
fix(autocomplete): fixed integration with dialog
ENTER key on autocomplete with unselected list item didn't prevented the event to bubble up to the dialog which caused the dialog to be rejected. Separated TAB and ENTER keys behavior to prevent event bubbling and ensuring that no matter what, TAB should always move to the next element. Fixes #3979. closes #5154
1 parent 2017127 commit 1df38df

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,43 @@ describe('<md-autocomplete>', function() {
168168

169169
element.remove();
170170
}));
171+
172+
it('should not close list on ENTER key if nothing is selected', inject(function($timeout, $mdConstant, $material) {
173+
var scope = createScope();
174+
var template = '\
175+
<md-autocomplete\
176+
md-selected-item="selectedItem"\
177+
md-search-text="searchText"\
178+
md-items="item in match(searchText)"\
179+
md-item-text="item.display"\
180+
placeholder="placeholder">\
181+
<span md-highlight-text="searchText">{{item.display}}</span>\
182+
</md-autocomplete>';
183+
var element = compile(template, scope);
184+
var ctrl = element.controller('mdAutocomplete');
185+
var ul = element.find('ul');
186+
187+
$material.flushInterimElement();
188+
189+
// Update the scope
190+
element.scope().searchText = 'fo';
191+
waitForVirtualRepeat(element);
192+
193+
// Focus the input
194+
ctrl.focus();
195+
$timeout.flush();
196+
197+
expect(ctrl.hidden).toBe(false);
198+
199+
// Run our key events
200+
ctrl.keydown(keydownEvent($mdConstant.KEY_CODE.ENTER));
201+
$timeout.flush();
202+
203+
// Check expectations again
204+
expect(ctrl.hidden).toBe(false);
205+
206+
element.remove();
207+
}));
171208
});
172209

173210
describe('basic functionality with template', function() {

src/components/autocomplete/js/autocompleteController.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,13 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
389389
updateMessages();
390390
break;
391391
case $mdConstant.KEY_CODE.TAB:
392-
case $mdConstant.KEY_CODE.ENTER:
393392
if (ctrl.hidden || ctrl.loading || ctrl.index < 0 || ctrl.matches.length < 1) return;
393+
select(ctrl.index);
394+
break;
395+
case $mdConstant.KEY_CODE.ENTER:
394396
event.stopPropagation();
395397
event.preventDefault();
398+
if (ctrl.hidden || ctrl.loading || ctrl.index < 0 || ctrl.matches.length < 1) return;
396399
select(ctrl.index);
397400
break;
398401
case $mdConstant.KEY_CODE.ESCAPE:

0 commit comments

Comments
 (0)