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

Commit

Permalink
fix(typeahead): close matches popup on click outside typeahead
Browse files Browse the repository at this point in the history
Closes #231
  • Loading branch information
pkozlowski-opensource committed Mar 16, 2013
1 parent 0228b06 commit acca7dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ describe('typeahead tests', function () {

describe('typeahead', function () {

var $scope, $compile;
var $scope, $compile, $document;
var changeInputValueTo;

beforeEach(inject(function (_$rootScope_, _$compile_, $sniffer) {
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, $sniffer) {
$scope = _$rootScope_;
$scope.source = ['foo', 'bar', 'baz'];
$compile = _$compile_;

$document = _$document_;
changeInputValueTo = function (element, value) {
var inputEl = findInput(element);
inputEl.val(value);
Expand Down Expand Up @@ -318,5 +318,21 @@ describe('typeahead tests', function () {
});
});

describe('regressions tests', function () {

it('issue 231 - closes matches popup on click outside typeahead', function () {
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
var dropdown = findDropDown(element);

$document.find('body').click();
$scope.$digest();

expect(dropdown).not.toHaveClass('open');
});
});

});
});
7 changes: 6 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ angular.module('ui.bootstrap.typeahead', [])
}])

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

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

Expand Down Expand Up @@ -155,6 +155,11 @@ angular.module('ui.bootstrap.typeahead', [])
}
});

$document.find('body').bind('click', function(){
scope.matches = [];
scope.$digest();
});

var tplElCompiled = $compile("<typeahead-popup matches='matches' active='activeIdx' select='select(activeIdx)' "+
"query='query'></typeahead-popup>")(scope);
element.after(tplElCompiled);
Expand Down

0 comments on commit acca7dc

Please sign in to comment.