Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
feat(autoComplete): Add autoSelectFirstSuggestion option
Browse files Browse the repository at this point in the history
Add an option to make the first tag suggestion be automatically selected
as soon as it appears. The default value is false.

Closes #136
  • Loading branch information
hamchapman authored and mbenford committed Dec 1, 2014
1 parent 60bd441 commit 0993bbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* becomes empty. The $query variable will be passed to the expression as an empty string.
* @param {boolean=} {loadOnFocus=false} Flag indicating that the source option will be evaluated when the input element
* gains focus. The current input value is available as $query.
* @param {boolean=} [autoSelectFirstSuggestion=false] Flag indicating that the first suggestion will not be
* automatically selected once the suggestion box is shown.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tagsInputConfig) {
function SuggestionList(loadFn, options) {
Expand All @@ -48,7 +50,11 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
$timeout.cancel(debouncedLoadId);
};
self.show = function() {
self.selected = null;
if (self.items.length > 0 && options.autoSelectFirstSuggestion) {
self.select(0);
} else {
self.selected = null;
}
self.visible = true;
};
self.load = function(query, tags) {
Expand Down Expand Up @@ -115,7 +121,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
maxResultsToShow: [Number, 10],
loadOnDownArrow: [Boolean, false],
loadOnEmpty: [Boolean, false],
loadOnFocus: [Boolean, false]
loadOnFocus: [Boolean, false],
autoSelectFirstSuggestion: [Boolean, false]
});

options = scope.options;
Expand Down
21 changes: 21 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,25 @@ describe('autoComplete directive', function() {
expect(getSuggestionText(2)).toBe('Item3');
});
});

describe('auto-select-first-suggestion option', function() {
it('initializes the option to false', function() {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.autoSelectFirstSuggestion).toBe(false);
});

it('selects the first suggestion after the suggestion box is shown if true', function() {
// Arrange
compile('auto-select-first-suggestion="true"');

//Act
loadSuggestions(2);

// Assert
expect(getSuggestion(0).hasClass('selected')).toBe(true);
});
});
});

0 comments on commit 0993bbd

Please sign in to comment.