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

Commit

Permalink
fix(typeahead): properly render initial input value
Browse files Browse the repository at this point in the history
Closes #591
  • Loading branch information
pkozlowski-opensource committed Jun 29, 2013
1 parent bb67a97 commit 990d1b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
28 changes: 11 additions & 17 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe('typeahead tests', function () {
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, $sniffer) {
$scope = _$rootScope_;
$scope.source = ['foo', 'bar', 'baz'];
$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];
$compile = _$compile_;
$document = _$document_;
changeInputValueTo = function (element, value) {
Expand Down Expand Up @@ -89,10 +93,6 @@ describe('typeahead tests', function () {

it('should correctly render initial state if the "as" keyword is used', function () {

$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];
$scope.result = $scope.states[0];

var element = prepareInputEl("<div><input ng-model='result' typeahead='state as state.name for state in states'></div>");
Expand Down Expand Up @@ -228,10 +228,6 @@ describe('typeahead tests', function () {

it('should invoke select callback on select', function () {

$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];
$scope.onSelect = function ($item, $model, $label) {
$scope.$item = $item;
$scope.$model = $model;
Expand All @@ -251,11 +247,6 @@ describe('typeahead tests', function () {

xit('should correctly update inputs value on mapping where label is not derived from the model', function () {

$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];

var element = prepareInputEl("<div><input ng-model='result' typeahead='state.code as state.name for state in states | filter:$viewValue'></div>");
var inputEl = findInput(element);

Expand All @@ -280,16 +271,18 @@ describe('typeahead tests', function () {

expect(element).toBeClosed();
});

it('issue 591 - initial formatting for un-selected match and complex label expression', function () {

var inputEl = findInput(prepareInputEl("<div><input ng-model='result' typeahead='state as state.name + \" \" + state.code for state in states | filter:$viewValue'></div>"));
expect(inputEl.val()).toEqual('');
});
});

describe('integration with existing formatters', function () {

it('should co-operate with existing formatters', function () {

$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];
$scope.result = $scope.states[0];

var element = prepareInputEl("<div><input ng-model='result.name' formatter typeahead='state.name for state in states | filter:$viewValue'></div>"),
Expand All @@ -298,4 +291,5 @@ describe('typeahead tests', function () {
expect(inputEl.val()).toEqual('formatted' + $scope.result.name);
});
});

});
10 changes: 7 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
});

modelCtrl.$formatters.push(function (modelValue) {
var locals = {}, viewValue;
var candidateViewValue, emptyViewValue;
var locals = {};
locals[parserResult.itemName] = modelValue;

viewValue = parserResult.viewMapper(originalScope, locals);
//it might happen that we don't have enough info to properly render input value
//we need to check for this
candidateViewValue = parserResult.viewMapper(originalScope, locals);
emptyViewValue = parserResult.viewMapper(originalScope, {});

return viewValue ? viewValue : modelValue;
return candidateViewValue!== emptyViewValue ? candidateViewValue : modelValue;
});

scope.select = function (activeIdx) {
Expand Down

0 comments on commit 990d1b1

Please sign in to comment.