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

Commit 0257665

Browse files
author
Luke Hutton
committed
feat(typeahead): add supported attribute to show results on focus
1 parent 57ed7e4 commit 0257665

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/typeahead/test/typeahead.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,28 @@ describe('typeahead tests', function() {
15211521
});
15221522
});
15231523

1524+
describe('showResultsOnFocus set to true', function() {
1525+
it('should open typeahead when input is focused and input is empty string', function() {
1526+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-show-results-on-focus="true"></div>');
1527+
var inputEl = findInput(element);
1528+
changeInputValueTo(element, '');
1529+
inputEl.focus();
1530+
$timeout.flush();
1531+
$scope.$digest();
1532+
expect(element).toBeOpenWithActive(3, 0);
1533+
});
1534+
1535+
it('should open typeahead when input is focused and input is not empty string', function() {
1536+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-show-results-on-focus="true"></div>');
1537+
var inputEl = findInput(element);
1538+
changeInputValueTo(element, 'b');
1539+
inputEl.focus();
1540+
$timeout.flush();
1541+
$scope.$digest();
1542+
expect(element).toBeOpenWithActive(2, 0);
1543+
});
1544+
});
1545+
15241546
describe('event listeners', function() {
15251547
afterEach(function() {
15261548
angular.element($window).off('resize');

src/typeahead/typeahead.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
8787

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

90+
//always show results on focus of input element, regardless of minimum length
91+
var showResultsOnFocus = attrs.typeaheadShowResultsOnFocus ? originalScope.$eval(attrs.typeaheadShowResultsOnFocus) : false;
92+
9093
//INTERNAL VARIABLES
9194

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

433436
element.on('focus', function (evt) {
434437
hasFocus = true;
435-
if (minLength === 0 && !modelCtrl.$viewValue) {
438+
if (minLength === 0 && !modelCtrl.$viewValue || showResultsOnFocus) {
436439
$timeout(function() {
437440
getMatchesAsync(modelCtrl.$viewValue, evt);
438441
}, 0);

0 commit comments

Comments
 (0)