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

fix(autocomplete): remove autofocus ambiguity. #9438

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,36 @@ describe('<md-autocomplete>', function() {
element.remove();
}));

it('should forward focus to the input element with md-autofocus', inject(function($timeout) {

var scope = createScope();

var template =
'<md-autocomplete ' +
' md-selected-item="selectedItem" ' +
' md-search-text="searchText" ' +
' md-items="item in match(searchText)" ' +
' md-item-text="item.display" ' +
' placeholder="placeholder"' +
' md-autofocus>' +
' <span md-highlight-text="searchText">{{item.display}}</span>' +
'</md-autocomplete>';

var element = compile(template, scope);
var input = element.find('input');

document.body.appendChild(element[0]);

// Initial timeout for gathering elements
$timeout.flush();

element.triggerHandler('focus');

expect(document.activeElement).toBe(input[0]);

element.remove();
}));

it('allows using an empty readonly attribute', inject(function() {
var scope = createScope(null, {inputId: 'custom-input-id'});
var template = '\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ <h2>Autocomplete Dialog Example</h2>
md-items="item in ctrl.querySearch(ctrl.searchText)"
md-item-text="item.display"
md-min-length="0"
placeholder="What is your favorite US state?">
placeholder="What is your favorite US state?"
md-autofocus="">
<md-item-template>
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
</md-item-template>
Expand Down
12 changes: 8 additions & 4 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
$mdTheming($element);
configureWatchers();
$mdUtil.nextTick(function () {

gatherElements();
moveDropdown();
focusElement();
$element.on('focus', focusElement);

// Forward all focus events to the input element when autofocus is enabled
if ($scope.autofocus) {
$element.on('focus', focusInputElement);
}
});
}

Expand Down Expand Up @@ -166,8 +170,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
/**
* Sends focus to the input element.
*/
function focusElement () {
if ($scope.autofocus) elements.input.focus();
function focusInputElement () {
elements.input.focus();
}

/**
Expand Down