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 showOnEmpty option
Browse files Browse the repository at this point in the history
Add an option to allow the tag list to show when input is empty.
This feature is useful when the user wants to see an unfiltered
list of all available suggestions before typing.
  • Loading branch information
FoxxMD authored and mbenford committed Jul 26, 2014
1 parent c7ae450 commit 28c615f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* @param {boolean=} [loadOnDownArrow=false] Flag indicating that the source option will be evaluated when the down arrow
* key is pressed and the suggestion list is closed. The current input value
* is available as $query.
* @param {boolean=} {showOnEmpty=false} Flag indicating whether the dropdown will show on input focus. When using this
* option the source expression should handle a null query to account for focus on
* an empty input.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInputConfig) {
function SuggestionList(loadFn, options) {
Expand Down Expand Up @@ -109,7 +112,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
minLength: [Number, 3],
highlightMatchedText: [Boolean, true],
maxResultsToShow: [Number, 10],
loadOnDownArrow: [Boolean, false]
loadOnDownArrow: [Boolean, false],
showOnEmpty: [Boolean, false]
});

options = scope.options;
Expand Down Expand Up @@ -167,7 +171,10 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
.on('input-change', function(value) {
if (value && value.length >= options.minLength) {
suggestionList.load(value, tagsInput.getTags());
} else {
} else if (scope.options.showOnEmpty) {
suggestionList.load('', tagsInput.getTags());
}
else {
suggestionList.reset();
}
})
Expand Down Expand Up @@ -224,6 +231,11 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
})
.on('input-blur', function() {
suggestionList.reset();
})
.on('input-focus', function () {
if (scope.options.showOnEmpty) {
suggestionList.load('', tagsInput.getTags(), true);
}
});

documentClick = function() {
Expand All @@ -240,4 +252,4 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
});
}
};
});
});
1 change: 1 addition & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
if (scope.hasFocus) {
return;
}
scope.events.trigger('input-focus');
scope.hasFocus = true;
events.trigger('input-focus');

Expand Down
34 changes: 34 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,40 @@ describe('autoComplete directive', function() {
});
});

describe('show-on-empty option', function(){
it('initialize the option to false', function(){
//Arrange
compile();

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

it('shows the suggestion box when the input field becomes empty', function(){
//Arrange
compile('show-on-empty="true"');

//Act
changeInputValue('');
$timeout.flush();

expect($scope.loadItems).toHaveBeenCalledWith('');
});

it('shows the suggestion box when input is focused and input is empty', function(){
//Arrange
compile('show-on-empty="true"');

//Act
eventHandlers['input-focus']();
$timeout.flush();

//Assert
expect($scope.loadItems).toHaveBeenCalledWith('');
});

});

describe('highlight-matched-text option', function() {
it('initializes the option to true', function() {
// Arrange/Act
Expand Down
3 changes: 2 additions & 1 deletion test/test-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
debounce-delay="0"
min-length="1"
max-results-to-show="10"
load-on-down-arrow="true">
load-on-down-arrow="true"
show-on-empty="true">
</auto-complete>
</tags-input>

Expand Down

0 comments on commit 28c615f

Please sign in to comment.