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

fix(typeahead): clear typeahead input when value not from popup entered and editable is false #4752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@ describe('typeahead tests', function() {
expect($scope.form.input.$error.editable).toBeFalsy();
});

it('should clear view value after blur for typeahead-editable="false"', function () {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('not in matches');
inputEl.blur(); // input loses focus
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('');
});

it('should clear view value when no value selected for typeahead-editable="false" typeahead-select-on-blur="false"', function () {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" typeahead-select-on-blur="false"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('b');
inputEl.blur(); // input loses focus
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('');
});

it('should not clear view value when there is match but no value selected for typeahead-editable="false" typeahead-select-on-blur="true"', function () {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" typeahead-select-on-blur="true"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('b');
inputEl.blur(); // input loses focus
expect($scope.result).toEqual('bar');
expect(inputEl.val()).toEqual('bar');
});

it('should bind loading indicator expression', inject(function($timeout) {
$scope.isLoading = false;
$scope.loadMatches = function(viewValue) {
Expand Down
3 changes: 3 additions & 0 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
scope.select(scope.activeIdx);
});
}
if (!isEditable && modelCtrl.$error.editable) {
element.val('');
}
hasFocus = false;
selected = false;
});
Expand Down