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

Commit 3e876b8

Browse files
deegwesleycho
authored andcommitted
feat(typeahead): add event object to onSelect
- Add support for `$event` in expression with `onSelect` Closes #5165
1 parent 3ef8992 commit 3e876b8

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/typeahead/docs/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ The typeahead directives provide several attributes:
5555
_(Defaults: angular.noop)_ :
5656
Binding to a variable that indicates if no matching results were found
5757

58-
* `typeahead-on-select($item, $model, $label)`
58+
* `typeahead-on-select($item, $model, $label, $event)`
5959
_(Defaults: null)_ :
60-
A callback executed when a match is selected
60+
A callback executed when a match is selected. $event can be undefined if selection not triggered from a user event.
6161

6262
* `typeahead-select-on-exact`
6363
_(Defaults: false)_ :

src/typeahead/test/typeahead.spec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,13 @@ describe('typeahead tests', function() {
478478
});
479479

480480
it('should invoke select callback on select', function() {
481-
$scope.onSelect = function($item, $model, $label) {
481+
$scope.onSelect = function($item, $model, $label, $event) {
482482
$scope.$item = $item;
483483
$scope.$model = $model;
484484
$scope.$label = $label;
485+
$scope.$event = $event;
485486
};
486-
var element = prepareInputEl('<div><input ng-model="result" typeahead-on-select="onSelect($item, $model, $label)" uib-typeahead="state.code as state.name for state in states | filter:$viewValue"></div>');
487+
var element = prepareInputEl('<div><input ng-model="result" typeahead-on-select="onSelect($item, $model, $label, $event)" uib-typeahead="state.code as state.name for state in states | filter:$viewValue"></div>');
487488

488489
changeInputValueTo(element, 'Alas');
489490
triggerKeyDown(element, 13);
@@ -492,6 +493,7 @@ describe('typeahead tests', function() {
492493
expect($scope.$item).toEqual($scope.states[0]);
493494
expect($scope.$model).toEqual('AL');
494495
expect($scope.$label).toEqual('Alaska');
496+
expect($scope.$event.type).toEqual("keydown");
495497
});
496498

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

src/typeahead/typeahead.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
151151
id: popupId,
152152
matches: 'matches',
153153
active: 'activeIdx',
154-
select: 'select(activeIdx)',
154+
select: 'select(activeIdx, evt)',
155155
'move-in-progress': 'moveInProgress',
156156
query: 'query',
157157
position: 'position',
@@ -202,7 +202,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
202202
return false;
203203
};
204204

205-
var getMatchesAsync = function(inputValue) {
205+
var getMatchesAsync = function(inputValue, evt) {
206206
var locals = {$viewValue: inputValue};
207207
isLoadingSetter(originalScope, true);
208208
isNoResultsSetter(originalScope, false);
@@ -238,10 +238,10 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
238238
if (selectOnExact && scope.matches.length === 1 && inputIsExactMatch(inputValue, 0)) {
239239
if (angular.isNumber(scope.debounceUpdate) || angular.isObject(scope.debounceUpdate)) {
240240
$$debounce(function() {
241-
scope.select(0);
241+
scope.select(0, evt);
242242
}, angular.isNumber(scope.debounceUpdate) ? scope.debounceUpdate : scope.debounceUpdate['default']);
243243
} else {
244-
scope.select(0);
244+
scope.select(0, evt);
245245
}
246246
}
247247

@@ -329,7 +329,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
329329
isOpenSetter(originalScope, isOpen);
330330
};
331331

332-
scope.select = function(activeIdx) {
332+
scope.select = function(activeIdx, evt) {
333333
//called from within the $digest() cycle
334334
var locals = {};
335335
var model, item;
@@ -344,7 +344,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
344344
onSelectCallback(originalScope, {
345345
$item: item,
346346
$model: model,
347-
$label: parserResult.viewMapper(originalScope, locals)
347+
$label: parserResult.viewMapper(originalScope, locals),
348+
$event: evt
348349
});
349350

350351
resetMatches();
@@ -378,10 +379,10 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
378379
scope.$apply(function () {
379380
if (angular.isNumber(scope.debounceUpdate) || angular.isObject(scope.debounceUpdate)) {
380381
$$debounce(function() {
381-
scope.select(scope.activeIdx);
382+
scope.select(scope.activeIdx, evt);
382383
}, angular.isNumber(scope.debounceUpdate) ? scope.debounceUpdate : scope.debounceUpdate['default']);
383384
} else {
384-
scope.select(scope.activeIdx);
385+
scope.select(scope.activeIdx, evt);
385386
}
386387
});
387388
break;
@@ -404,25 +405,25 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
404405
}
405406
});
406407

407-
element.bind('focus', function () {
408+
element.bind('focus', function (evt) {
408409
hasFocus = true;
409410
if (minLength === 0 && !modelCtrl.$viewValue) {
410411
$timeout(function() {
411-
getMatchesAsync(modelCtrl.$viewValue);
412+
getMatchesAsync(modelCtrl.$viewValue, evt);
412413
}, 0);
413414
}
414415
});
415416

416-
element.bind('blur', function() {
417+
element.bind('blur', function(evt) {
417418
if (isSelectOnBlur && scope.matches.length && scope.activeIdx !== -1 && !selected) {
418419
selected = true;
419420
scope.$apply(function() {
420421
if (angular.isObject(scope.debounceUpdate) && angular.isNumber(scope.debounceUpdate.blur)) {
421422
$$debounce(function() {
422-
scope.select(scope.activeIdx);
423+
scope.select(scope.activeIdx, evt);
423424
}, scope.debounceUpdate.blur);
424425
} else {
425-
scope.select(scope.activeIdx);
426+
scope.select(scope.activeIdx, evt);
426427
}
427428
});
428429
}
@@ -585,14 +586,14 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
585586
scope.active = matchIdx;
586587
};
587588

588-
scope.selectMatch = function(activeIdx) {
589+
scope.selectMatch = function(activeIdx, evt) {
589590
var debounce = scope.debounce();
590591
if (angular.isNumber(debounce) || angular.isObject(debounce)) {
591592
$$debounce(function() {
592-
scope.select({activeIdx: activeIdx});
593+
scope.select({activeIdx: activeIdx, evt: evt});
593594
}, angular.isNumber(debounce) ? debounce : debounce['default']);
594595
} else {
595-
scope.select({activeIdx: activeIdx});
596+
scope.select({activeIdx: activeIdx, evt: evt});
596597
}
597598
};
598599
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul class="dropdown-menu" ng-show="isOpen() && !moveInProgress" ng-style="{top: position().top+'px', left: position().left+'px'}" style="display: block;" role="listbox" aria-hidden="{{!isOpen()}}">
2-
<li ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index)" role="option" id="{{::match.id}}">
2+
<li ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="{{::match.id}}">
33
<div uib-typeahead-match index="$index" match="match" query="query" template-url="templateUrl"></div>
44
</li>
55
</ul>

0 commit comments

Comments
 (0)