Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

feat(typeahead): add supported attribute to show results on focus #6353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.multiMap', 'ui.bootstrap.
var closeDropdown = function(evt) {
// This method may still be called during the same mouse event that
// unbound this event handler. So check openScope before proceeding.
if (!openScope) { return; }
if (!openScope || !openScope.isOpen) { return; }

if (evt && openScope.getAutoClose() === 'disabled') { return; }

Expand Down
23 changes: 23 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,29 @@ describe('typeahead tests', function() {
});
});

describe('showResultsOnFocus set to true', function() {
it('should open typeahead when input is focused and input is set and longer than min length threshold', function() {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="1" typeahead-show-results-on-focus="true"></div>');
var inputEl = findInput(element);
changeInputValueTo(element, 'ba');
inputEl.focus();
$timeout.flush();
$scope.$digest();
expect(element).toBeOpenWithActive(3, 0);
});

it('should not open typeahead when input is focused and input is set and not longer than min length threshold', function() {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-min-length="2" typeahead-show-results-on-focus="true"></div>');
var inputEl = findInput(element);
changeInputValueTo(element, 'b');
inputEl.focus();
$timeout.flush();
$scope.$digest();
expect(element).toBeClosed();
});

});

describe('event listeners', function() {
afterEach(function() {
angular.element($window).off('resize');
Expand Down
6 changes: 5 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap

var showHint = originalScope.$eval(attrs.typeaheadShowHint) || false;

//always show results on focus of input element, regardless of minimum length
var showResultsOnFocus = attrs.typeaheadShowResultsOnFocus ? originalScope.$eval(attrs.typeaheadShowResultsOnFocus) : false;

//INTERNAL VARIABLES

//model setter executed upon match selection
Expand Down Expand Up @@ -432,7 +435,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap

element.on('focus', function (evt) {
hasFocus = true;
if (minLength === 0 && !modelCtrl.$viewValue) {
if (minLength === 0 && !modelCtrl.$viewValue ||
showResultsOnFocus && modelCtrl.$viewValue && modelCtrl.$viewValue.length >= minLength) {
$timeout(function() {
getMatchesAsync(modelCtrl.$viewValue, evt);
}, 0);
Expand Down