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

Commit 00a4c05

Browse files
devversionhansl
authored andcommitted
fix(autocomplete): remove autofocus ambiguity. (#9438)
* The autocomplete opens the dropdown when the `md-autofocus` attribute is specified. * This wasn't docmented for a long time, since the global `md-autofocus` is a key attribute, to mark an element to be focused upon opening. (e.g `md-sidenav`, `md-bottom-sheet`, `md-dialog`. * This also fixes an issue where the autocomplete dropdown shows up twice, when having the autocomplete inside of a dialog.
1 parent 69607e0 commit 00a4c05

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,36 @@ describe('<md-autocomplete>', function() {
278278
element.remove();
279279
}));
280280

281+
it('should forward focus to the input element with md-autofocus', inject(function($timeout) {
282+
283+
var scope = createScope();
284+
285+
var template =
286+
'<md-autocomplete ' +
287+
' md-selected-item="selectedItem" ' +
288+
' md-search-text="searchText" ' +
289+
' md-items="item in match(searchText)" ' +
290+
' md-item-text="item.display" ' +
291+
' placeholder="placeholder"' +
292+
' md-autofocus>' +
293+
' <span md-highlight-text="searchText">{{item.display}}</span>' +
294+
'</md-autocomplete>';
295+
296+
var element = compile(template, scope);
297+
var input = element.find('input');
298+
299+
document.body.appendChild(element[0]);
300+
301+
// Initial timeout for gathering elements
302+
$timeout.flush();
303+
304+
element.triggerHandler('focus');
305+
306+
expect(document.activeElement).toBe(input[0]);
307+
308+
element.remove();
309+
}));
310+
281311
it('allows using an empty readonly attribute', inject(function() {
282312
var scope = createScope(null, {inputId: 'custom-input-id'});
283313
var template = '\

src/components/autocomplete/demoInsideDialog/dialog.tmpl.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ <h2>Autocomplete Dialog Example</h2>
2020
md-items="item in ctrl.querySearch(ctrl.searchText)"
2121
md-item-text="item.display"
2222
md-min-length="0"
23-
placeholder="What is your favorite US state?">
23+
placeholder="What is your favorite US state?"
24+
md-autofocus="">
2425
<md-item-template>
2526
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
2627
</md-item-template>

src/components/autocomplete/js/autocompleteController.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
7070
$mdTheming($element);
7171
configureWatchers();
7272
$mdUtil.nextTick(function () {
73+
7374
gatherElements();
7475
moveDropdown();
75-
focusElement();
76-
$element.on('focus', focusElement);
76+
77+
// Forward all focus events to the input element when autofocus is enabled
78+
if ($scope.autofocus) {
79+
$element.on('focus', focusInputElement);
80+
}
7781
});
7882
}
7983

@@ -166,8 +170,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
166170
/**
167171
* Sends focus to the input element.
168172
*/
169-
function focusElement () {
170-
if ($scope.autofocus) elements.input.focus();
173+
function focusInputElement () {
174+
elements.input.focus();
171175
}
172176

173177
/**

0 commit comments

Comments
 (0)