Skip to content

Commit

Permalink
MDL-52042 autocomplete: close suggestion box
Browse files Browse the repository at this point in the history
Close the suggestion box when the input loses focus for more
than half a second.
  • Loading branch information
ryanwyllie committed Nov 9, 2015
1 parent 1533fca commit e1db2b4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/form-autocomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 6 additions & 15 deletions lib/amd/src/form-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
UP: 38
};

/** @var {Number} closeSuggestionsTimer - integer used to cancel window.setTimeout. */
var closeSuggestionsTimer = null;

/**
* Make an item in the selection list "active".
*
Expand Down Expand Up @@ -568,15 +565,12 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
createItem(options, state, originalSelect);
}
});
inputElement.on('blur focus', function(e) {
// We may be blurring because we have clicked on the suggestion list. We
// dont want to close the selection list before the click event fires, so
// we have to delay.
if (closeSuggestionsTimer) {
window.clearTimeout(closeSuggestionsTimer);
}
closeSuggestionsTimer = window.setTimeout(function() {
if (e.type == 'blur') {
inputElement.on('blur', function() {
window.setTimeout(function() {
// Get the current element with focus.
var focusElement = $(document.activeElement);
// Only close the menu if the input hasn't regained focus.
if (focusElement.attr('id') != inputElement.attr('id')) {
if (options.tags) {
createItem(options, state, originalSelect);
}
Expand All @@ -589,9 +583,6 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
arrowElement.on('click', function() {
// Prevent the close timer, or we will open, then close the suggestions.
inputElement.focus();
if (closeSuggestionsTimer) {
window.clearTimeout(closeSuggestionsTimer);
}
// Show the suggestions list.
updateSuggestions(options, state, inputElement.val(), originalSelect);
});
Expand Down

0 comments on commit e1db2b4

Please sign in to comment.