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

Commit

Permalink
feat(typeahead): support the editable property
Browse files Browse the repository at this point in the history
Closes #269
  • Loading branch information
pkozlowski-opensource committed Mar 26, 2013
1 parent 929a46f commit a40c3fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ describe('typeahead tests', function () {
var matchHighlight = findMatches(element).find('a').html();
expect(matchHighlight).toEqual('prefix<strong>fo</strong>o');
});

it('should by default bind view value to model even if not part of matches', function () {
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual('not in matches');
});

it('should support the editable property to limit model bindings to matches only', function () {
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'></div>");
changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
});
});

describe('selecting a match', function () {
Expand Down
11 changes: 8 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ angular.module('ui.bootstrap.typeahead', [])
//expressions used by typeahead
var parserResult = typeaheadParser.parse(attrs.typeahead);

//should it restrict model values to the ones selected from the popup only?
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;

//create a child scope for the typeahead directive so we are not polluting original scope
//with typeahead-specific data (matches, query etc.)
var scope = originalScope.$new();
Expand Down Expand Up @@ -107,7 +110,7 @@ angular.module('ui.bootstrap.typeahead', [])
}
}

return undefined;
return isEditable ? inputValue : undefined;
});

modelCtrl.$render = function () {
Expand Down Expand Up @@ -151,13 +154,15 @@ angular.module('ui.bootstrap.typeahead', [])

} else if (evt.which === 27) {
evt.stopPropagation();
scope.matches = [];

resetMatches();
scope.$digest();
}
});

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

resetMatches();
scope.$digest();
});

Expand Down

0 comments on commit a40c3fb

Please sign in to comment.