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

Commit

Permalink
fix(typeahead): set validity flag for non-editable inputs
Browse files Browse the repository at this point in the history
Closes #806
  • Loading branch information
pkozlowski-opensource committed Aug 15, 2013
1 parent d34f2de commit 366e0c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,28 @@ describe('typeahead tests', function () {
});

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>");
var element = prepareInputEl("<div>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);
});

it('should set validation erros for non-editable inputs', function () {

var element = prepareInputEl(
"<div><form name='form'>" +
"<input name='input' ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'>" +
"</form></div>");

changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
expect($scope.form.input.$error.editable).toBeTruthy();

changeInputValueTo(element, 'foo');
triggerKeyDown(element, 13);
expect($scope.result).toEqual('foo');
expect($scope.form.input.$error.editable).toBeFalsy();
});

it('should bind loading indicator expression', inject(function ($timeout) {

$scope.isLoading = false;
Expand Down
14 changes: 11 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
};
}])

.directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser', function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) {
.directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser',
function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) {

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

Expand Down Expand Up @@ -158,7 +159,12 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
}
}

return isEditable ? inputValue : undefined;
if (isEditable) {
return inputValue;
} else {
modelCtrl.$setValidity('editable', false);
return undefined;
}
});

modelCtrl.$formatters.push(function (modelValue) {
Expand Down Expand Up @@ -192,15 +198,17 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
locals[parserResult.itemName] = item = scope.matches[activeIdx].model;
model = parserResult.modelMapper(originalScope, locals);
$setModelValue(originalScope, model);
modelCtrl.$setValidity('editable', true);

onSelectCallback(originalScope, {
$item: item,
$model: model,
$label: parserResult.viewMapper(originalScope, locals)
});

//return focus to the input element if a mach was selected via a mouse click event
resetMatches();

//return focus to the input element if a mach was selected via a mouse click event
element[0].focus();
};

Expand Down

0 comments on commit 366e0c8

Please sign in to comment.