Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
feat(tagsInput): Enable ngFocus and ngBlur native directives
Browse files Browse the repository at this point in the history
This adjustment makes sure the outer (directive) element is focussed
when the input field receives focus, and blurred when the input field
loses focus. That way, Angular's native ng-focus and ng-blur can be
used on the directive element.

Relates to #182
  • Loading branch information
fastf0rward authored and mbenford committed Dec 1, 2014
1 parent 003e678 commit 210b86f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,17 @@ tagsInput.directive('tagsInput', function($timeout, $document, tagsInputConfig)
scope.newTag.invalid = null;
})
.on('input-focus', function() {

// focus outer element
element.triggerHandler('focus');

ngModelCtrl.$setValidity('leftoverText', true);
})
.on('input-blur', function() {

// blur outer element
element.triggerHandler('blur');

if (!options.addFromAutocompleteOnly) {
if (options.addOnBlur) {
tagList.addText(scope.newTag.text);
Expand Down
28 changes: 28 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,34 @@ describe('tags-input directive', function() {
// Assert
expect($scope.$digest).not.toHaveBeenCalled();
});

it('focuses the directive element when the input field receives focus', function() {
// Arrange
$scope.callback = jasmine.createSpy();
compile('ng-focus="callback()"');
var input = getInput()[0];

// Act
getInput().triggerHandler('focus');
$timeout.flush();

// Assert
expect($scope.callback).toHaveBeenCalled();
});

it('blurs the directive element when the input field loses focus', function() {
// Arrange
$scope.callback = jasmine.createSpy();
compile('ng-blur="callback()"');
var input = getInput()[0];

// Act
getInput().triggerHandler('blur');
$timeout.flush();

// Assert
expect($scope.callback).toHaveBeenCalled();
});
});

describe('tabindex option', function() {
Expand Down

0 comments on commit 210b86f

Please sign in to comment.