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

Commit

Permalink
feat(typeahead): Add docs, change name, use param
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Rued authored and Christopher Rued committed Apr 29, 2016
1 parent c0102e1 commit 1dd7c8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/typeahead/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ This directive works with promises, meaning you can retrieve matches using the `
_(Default: `angular.noop`)_ -
Binding to a variable that indicates if no matching results were found.

* `typeahead-should-select($event)`
<small class="badge">$</small>
_(Default: `null`)_ -
A callback executed when an event occurs that might trigger a selection. Selection will only occur if this function returns true.

* `typeahead-on-select($item, $model, $label, $event)`
<small class="badge">$</small>
_(Default: `null`)_ -
Expand Down
18 changes: 9 additions & 9 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,12 @@ describe('typeahead tests', function() {
});
});

describe('isSelectTrigger', function() {
describe('shouldSelect', function() {
it('should select a match when function returns true', function() {
$scope.isSelectTrigger = function() {
$scope.shouldSelectFn = function() {
return true;
};
var element = prepareInputEl('<div><input ng-model="result" typeahead-is-select-trigger="isSelectTrigger" uib-typeahead="item for item in source | filter:$viewValue"></div>');
var element = prepareInputEl('<div><input ng-model="result" typeahead-should-select="shouldSelectFn($event)" uib-typeahead="item for item in source | filter:$viewValue"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
Expand All @@ -473,10 +473,10 @@ describe('typeahead tests', function() {
expect(element).toBeClosed();
});
it('should not select a match when function returns false', function() {
$scope.isSelectTrigger = function() {
$scope.shouldSelectFn = function() {
return false;
};
var element = prepareInputEl('<div><input ng-model="result" typeahead-is-select-trigger="isSelectTrigger" uib-typeahead="item for item in source | filter:$viewValue"></div>');
var element = prepareInputEl('<div><input ng-model="result" typeahead-should-select="shouldSelectFn($event)" uib-typeahead="item for item in source | filter:$viewValue"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
Expand All @@ -487,15 +487,15 @@ describe('typeahead tests', function() {
expect(inputEl.val()).toEqual('b');
});
it('should pass key event into select trigger function', function() {
$scope.isSelectTrigger = jasmine.createSpy('isSelectTrigger');//.and.returnValue(true);
var element = prepareInputEl('<div><input ng-model="result" typeahead-is-select-trigger="isSelectTrigger" uib-typeahead="item for item in source | filter:$viewValue"></div>');
$scope.shouldSelectFn = jasmine.createSpy('shouldSelectFn');//.and.returnValue(true);
var element = prepareInputEl('<div><input ng-model="result" typeahead-should-select="shouldSelectFn($event)" uib-typeahead="item for item in source | filter:$viewValue"></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'b');
triggerKeyDown(element, 13);

expect($scope.isSelectTrigger.calls.count()).toEqual(1);
expect($scope.isSelectTrigger.calls.argsFor(0)[0].which).toEqual(13);
expect($scope.shouldSelectFn.calls.count()).toEqual(1);
expect($scope.shouldSelectFn.calls.argsFor(0)[0].which).toEqual(13);
});
});

Expand Down
12 changes: 6 additions & 6 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
//binding to a variable that indicates if matches are being retrieved asynchronously
var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;

//a function to determine if an event should trigger selection
var isSelectTrigger = attrs.typeaheadIsSelectTrigger ? originalScope.$eval(attrs.typeaheadIsSelectTrigger) : function(evt) {
//a function to determine if an event should cause selection
var isSelectEvent = attrs.typeaheadShouldSelect ? $parse(attrs.typeaheadShouldSelect) : function(scope, vals) {
var evt = vals.$event;
return evt.which === 13 || evt.which === 9;
};

Expand Down Expand Up @@ -375,15 +376,15 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
return;
}

var triggerKey = isSelectTrigger(evt);
var shouldSelect = isSelectEvent(originalScope, {$event: evt});

/**
* if there's nothing selected (i.e. focusFirst) and enter or tab is hit
* or
* shift + tab is pressed to bring focus to the previous element
* then clear the results
*/
if (scope.activeIdx === -1 && triggerKey || evt.which === 9 && !!evt.shiftKey) {
if (scope.activeIdx === -1 && shouldSelect || evt.which === 9 && !!evt.shiftKey) {
resetMatches();
scope.$digest();
return;
Expand Down Expand Up @@ -411,7 +412,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
target.parentNode.scrollTop = target.offsetTop;
break;
default:
if (triggerKey) {
if (shouldSelect) {
scope.$apply(function() {
if (angular.isNumber(scope.debounceUpdate) || angular.isObject(scope.debounceUpdate)) {
$$debounce(function() {
Expand All @@ -422,7 +423,6 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
}
});
}
break;
}
});

Expand Down

0 comments on commit 1dd7c8a

Please sign in to comment.