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

Commit acca7dc

Browse files
fix(typeahead): close matches popup on click outside typeahead
Closes #231
1 parent 0228b06 commit acca7dc

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/typeahead/test/typeahead.spec.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ describe('typeahead tests', function () {
144144

145145
describe('typeahead', function () {
146146

147-
var $scope, $compile;
147+
var $scope, $compile, $document;
148148
var changeInputValueTo;
149149

150-
beforeEach(inject(function (_$rootScope_, _$compile_, $sniffer) {
150+
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, $sniffer) {
151151
$scope = _$rootScope_;
152152
$scope.source = ['foo', 'bar', 'baz'];
153153
$compile = _$compile_;
154-
154+
$document = _$document_;
155155
changeInputValueTo = function (element, value) {
156156
var inputEl = findInput(element);
157157
inputEl.val(value);
@@ -318,5 +318,21 @@ describe('typeahead tests', function () {
318318
});
319319
});
320320

321+
describe('regressions tests', function () {
322+
323+
it('issue 231 - closes matches popup on click outside typeahead', function () {
324+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
325+
var inputEl = findInput(element);
326+
327+
changeInputValueTo(element, 'b');
328+
var dropdown = findDropDown(element);
329+
330+
$document.find('body').click();
331+
$scope.$digest();
332+
333+
expect(dropdown).not.toHaveClass('open');
334+
});
335+
});
336+
321337
});
322338
});

src/typeahead/typeahead.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ angular.module('ui.bootstrap.typeahead', [])
3030
}])
3131

3232
//options - min length
33-
.directive('typeahead', ['$compile', '$q', 'typeaheadParser', function ($compile, $q, typeaheadParser) {
33+
.directive('typeahead', ['$compile', '$q', '$document', 'typeaheadParser', function ($compile, $q, $document, typeaheadParser) {
3434

3535
var HOT_KEYS = [9, 13, 27, 38, 40];
3636

@@ -155,6 +155,11 @@ angular.module('ui.bootstrap.typeahead', [])
155155
}
156156
});
157157

158+
$document.find('body').bind('click', function(){
159+
scope.matches = [];
160+
scope.$digest();
161+
});
162+
158163
var tplElCompiled = $compile("<typeahead-popup matches='matches' active='activeIdx' select='select(activeIdx)' "+
159164
"query='query'></typeahead-popup>")(scope);
160165
element.after(tplElCompiled);

0 commit comments

Comments
 (0)