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

Commit

Permalink
feat(events): add open-close callback
Browse files Browse the repository at this point in the history
Adds a new `uisOpenClose` directive which allows a callback to be 
defined that is called whenever the dropdown is opened or closed.

Callback is passed an isOpen parameter which is set to true if the
dropdown has been opened, otherwise false.

Closes #432, closes #1153
  • Loading branch information
Den-dp authored and user378230 committed Jul 30, 2016
1 parent 3dfde71 commit 21bcd5e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/uisOpenCloseDirective.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
uis.directive('uisOpenClose', ['$parse', '$timeout', function ($parse, $timeout) {
return {
restrict: 'A',
require: 'uiSelect',
link: function (scope, element, attrs, $select) {
$select.onOpenCloseCallback = $parse(attrs.uisOpenClose);

scope.$watch('$select.open', function (isOpen, previousState) {
if (isOpen !== previousState) {
$timeout(function () {
$select.onOpenCloseCallback(scope, {
isOpen: isOpen
});
});
}
});
}
};
}]);
26 changes: 26 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,32 @@ describe('ui-select tests', function() {
expect(scope.$model).toBe(scope.$item);
});

it('should call open-close callback with isOpen state as first argument on open and on close', function () {

var el = compileTemplate(
'<ui-select uis-open-close="onOpenCloseFn(isOpen)" ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);

scope.onOpenCloseFn = function(){};
spyOn(scope, 'onOpenCloseFn');

openDropdown(el);
$timeout.flush();
expect(scope.onOpenCloseFn).toHaveBeenCalledWith(true);

closeDropdown(el);
$timeout.flush();
expect(scope.onOpenCloseFn).toHaveBeenCalledWith(false);

expect(scope.onOpenCloseFn.calls.count()).toBe(2);
});

it('should allow creating tag in single select mode with tagging enabled', function() {

scope.taggingFunc = function (name) {
Expand Down

0 comments on commit 21bcd5e

Please sign in to comment.