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

Commit

Permalink
feat(tagsInput): Add newTagText option
Browse files Browse the repository at this point in the history
Add an option for binding to the text of the inner input element.
This feature is important because it allows developers to know when
the input has changed as well the state of the input at the time of
the change. An example use case is doing custom validations that
regular expressions couldn't satisfy.

Closes #197
  • Loading branch information
LoganBarnett authored and mbenford committed May 24, 2015
1 parent 4682577 commit 215fe92
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*
* @param {string} ngModel Assignable angular expression to data-bind to.
* @param {string=} [template=NA] URL or id of a custom template for rendering each tag.
* @param {string} newTagText Assignable angular expression for data-binding to the underlying input for a new tag.
* @param {string=} [displayProperty=text] Property to be rendered as the tag label.
* @param {string=} [keyProperty=text] Property to be used as a unique identifier for the tag.
* @param {string=} [displayProperty=text] Property to be rendered as the tag label.
* @param {string=} [type=text] Type of the input element. Only 'text', 'email' and 'url' are supported values.
Expand Down Expand Up @@ -151,6 +153,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, tagsInpu
require: 'ngModel',
scope: {
tags: '=ngModel',
newTagText: '=?',
onTagAdding: '&',
onTagAdded: '&',
onInvalidTag: '&',
Expand Down Expand Up @@ -262,6 +265,18 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, tagsInpu
}
};

scope.$watch('newTag.text', function(value) {
if(scope.newTag.text !== scope.newTagText) {
scope.newTagText = value;
}
});

scope.$watch('newTagText', function(value) {
if(scope.newTag.text !== scope.newTagText) {
scope.newTag.setText(value);
}
});

scope.track = function(tag) {
return tag[options.keyProperty || options.displayProperty];
};
Expand Down
27 changes: 27 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,33 @@ describe('tags-input directive', function() {
});
});

describe('new-tag-text option', function() {
it('updates as text is typed into the input field', function() {
// Arrange
compile('new-tag-text="innerModel"');

// Act
sendKeyPress('f'.charCodeAt(0));
sendKeyPress('o'.charCodeAt(0));
sendKeyPress('o'.charCodeAt(0));

// Assert
expect($scope.innerModel).toEqual('foo');
});

it('updates the input field as the scope\'s model changes', function() {
// Arrange
compile('new-tag-text="innerModel"');

// Act
$scope.innerModel = 'foo';
$scope.$digest();

// Assert
expect(getInput().val()).toEqual('foo');
});
});

describe('tabindex option', function() {
it('sets the input field tab index', function() {
// Arrange/Act
Expand Down

0 comments on commit 215fe92

Please sign in to comment.