From c4e169cbb7b1b4c33cc092e85f63e6e8745e3393 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sat, 29 Jun 2013 21:47:51 +0200 Subject: [PATCH] fix(typeahead): properly render initial input value Closes #591 --- src/typeahead/test/typeahead.spec.js | 28 +++++++++++----------------- src/typeahead/typeahead.js | 10 +++++++--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index e1e001f814..331fe153cc 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -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) { @@ -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("
"); @@ -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; @@ -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("
"); var inputEl = findInput(element); @@ -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("
")); + 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("
"), @@ -298,4 +291,5 @@ describe('typeahead tests', function () { expect(inputEl.val()).toEqual('formatted' + $scope.result.name); }); }); + }); \ No newline at end of file diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 103917978b..21ee014031 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -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) {